ARTS:
- Algrothm: leetcode算法题目
- Review: 阅读并且点评一篇英文技术文章
- Tip/Techni: 学习一个技术技巧
- Share: 分享一篇有观点和思考的技术文章
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"
Example 2:
Input: "here"
Output: "here"
Example 3:
Input: "LOVELY"
Output: "lovely"
2)answer
声明一个新的字符串变量,对传入的字符串的字符逐个读取,如果是大写字母就取小写与大写之间的差值,得到大写字符对应的小写字母ASCII码存进字符串中。处理完后返回结果。
3)solution
Cpp:
#include <stdio.h>
#include <string>
using std::string;
class Solution {
public:
string toLowerCase(string str) {
string re_val = "";
for (char str_val : str)
{
if (str_val >='A'&& str_val <='Z')
{
// 取小写与大写之间的差值,得到字符对应的小写ASCII码对应是什么存进字符串中
re_val += (str_val + ('a' - 'A'));
}
else
{
// 如果是小写就不处理
re_val += str_val;
}
}
return re_val;
}