码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 709. To Lower Case

时间:2018-12-21 21:18:17      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:描述   asc   cti   int   leetcode   lower   param   lov   分析   

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"

Example 3:

Input: "LOVELY"
Output: "lovely"

题目描述:实现一个 ToLowerCase 函数,函数功能是将字符串中的大写字母变成小写字母。

题目分析:很简单,我们可以利用 ASCII 的性质, A-ZASCII 的范围是 65~90 ,所以我们只需要将字符串中出现如上字母加上32即可。

python 代码:

class Solution(object):
    def toLowerCase(self, str):
        """
        :type str: str
        :rtype: str
        """
        str_length = len(str)
        s = ''
        for i in range(str_length):
            if(ord(str[i]) >= 65 and ord(str[i]) <= 90):
                s = s + chr(ord(str[i]) + 32)
            else:
                s = s + str[i]
                
        return s

C++ 代码:

class Solution {
public:
    string toLowerCase(string str) {
        int len = str.length();
        for(int i = 0; i < len; i++){
            if(str[i] >= 'A' && str[i] <= 'Z'){
                str[i] += 32;
            }
        }
        return str;
    }
};

LeetCode 709. To Lower Case

标签:描述   asc   cti   int   leetcode   lower   param   lov   分析   

原文地址:https://www.cnblogs.com/ECJTUACM-873284962/p/10158758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!