[LeetCode] To Lower Case 转为小写

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.

Example 1:

Input: "Hello"
Output: "hello"

Example 2:

Input: "here"
Output: "here"

Example 3:

Input: "LOVELY"
Output: "lovely"

这道题让我们将单词转为小写,是一道比较简单的题目,我们都知道小写字母比其对应的大写字母的ASCII码大32,所以我们只需要遍历字符串,对于所有的大写字母,统统加上32即可,参见代码如下:

class Solution {
public:
    string toLowerCase(string str) {
        for (char &c : str) {
            if (c >= ‘A‘ && c <= ‘Z‘) c += 32;
        }
        return str;
    }
};

参考资料:

https://leetcode.com/problems/to-lower-case/

LeetCode All in One 题目讲解汇总(持续更新中...)

原文地址:https://www.cnblogs.com/grandyang/p/10018055.html

时间: 2024-11-03 23:43:30

[LeetCode] To Lower Case 转为小写的相关文章

LeetCode算法题-To Lower Case(Java实现)

这是悦乐书的第301次更新,第320篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第169题(顺位题号是709).实现具有字符串参数str的函数ToLowerCase():以小写形式返回相同的字符串.例如: 输入:"Hello" 输出:"hello" 输入:"here" 输出:"here" 输入:"LOVELY" 输出:"lovely" 本次解题使用的开发工

LeetCode--To Lower Case &amp; Remove Outermost Parentheses (Easy)

709. To Lower Case(Easy) Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here"

[LeetCode]709. To Lower Case

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. 将字符串的大写字母转成小写字母字符串. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here" Examp

(Easy) To Lower Case LeetCode

Description Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here" Example 3: In

709. To Lower Case

Algorithm to-lower-case https://leetcode.com/problems/to-lower-case/ 1)problem Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Exampl

Leetcode篇:字符串转为整数

@author: ZZQ @software: PyCharm @file: myAtoi.py @time: 2018/9/20 20:54 要求:实现 atoi,将字符串转为整数. 1)根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止 2)如果第一个非空字符是正号或负号,选取该符号: 3) 将其与后面尽可能多的连续的数字组合起来,这部分字符即为整数的值. 4) 如果第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数. 5) 字符串可以在形成整数的字符后面包括多余

2. To Lower Case

Title: Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here" Note: None Analysi

过滤指令:number currency lowercase(转为小写)/uppercase(转为大写)

<!DOCTYPE html> <html ng-app> <head lang="en"> <meta charset="UTF-8"> <link rel="stylesheet" href="css/bootstrap.css"/> <title></title> </head> <body> <div cl

可以一直接收键盘字符,并将大写转为小写,小写转为大写,数字不作处理

#include<stdio.h>int main(){  char  ch;  char ch1;  while((ch=getchar()) != EOF)  {   if(ch>='a'&&ch<='z')   {  ch1=ch-32;   putchar(ch1);           continue;   }   if(ch>='A'&&ch<='Z')   {  ch1=ch+32;     putchar(ch1);