709. To Lower Case(python+cpp)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21275321/article/details/82710660

题目:

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”

解释:
大写转小写
python代码:

class Solution(object):
    def toLowerCase(self, str):
        """
        :type str: str
        :rtype: str
        """
        return str.lower()    

python代码2:

class Solution(object):
    def toLowerCase(self, str):
        """
        :type str: str
        :rtype: str
        """
        result=[]
        SET="QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"
        for s in str:
            if s in SET:
                if ord(s)<ord('a'):
                    result.append(chr(ord(s)+ord('a')-ord('A')))
                else:
                    result.append(s)
            else:
                result.append(s)
        return ''.join(result)

c++代码:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class Solution {
public:
    string toLowerCase(string str) {
        transform(str.begin(),str.end(),str.begin(),::tolower);
        return str;
    }
};

总结:
#include<algorithm>

猜你喜欢

转载自blog.csdn.net/qq_21275321/article/details/82710660
今日推荐