前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 709. To Lower Case

LeetCode 709. To Lower Case

作者头像
Angel_Kitty
发布2018-12-28 16:29:33
3720
发布2018-12-28 16:29:33
举报

709. To Lower Case

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

Example 1:

代码语言:javascript
复制
Input: "Hello"
Output: "hello"

Example 2:

代码语言:javascript
复制
Input: "here"
Output: "here"

Example 3:

代码语言:javascript
复制
Input: "LOVELY"
Output: "lovely"

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

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

python 代码:

代码语言:javascript
复制
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++ 代码:

代码语言:javascript
复制
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;
    }
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-12-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 709. To Lower Case
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档