LeetCode 709. To Lower Case

简介: LeetCode 709. To Lower Case

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-Easy 709. To Lower Case
Leetcode-Easy 709. To Lower Case
83 0
Leetcode-Easy 709. To Lower Case
|
2月前
|
机器学习/深度学习 算法
力扣刷题日常(一)
力扣刷题日常(一)
20 2
|
2月前
|
存储 索引
《LeetCode》—— LeetCode刷题日记
《LeetCode》—— LeetCode刷题日记
|
2月前
|
搜索推荐
《LeetCode》——LeetCode刷题日记3
《LeetCode》——LeetCode刷题日记3
|
2月前
|
容器
《LeetCode》——LeetCode刷题日记1
《LeetCode》——LeetCode刷题日记1
|
2月前
|
算法
LeetCode刷题---21.合并两个有序链表(双指针)
LeetCode刷题---21.合并两个有序链表(双指针)
|
2月前
|
算法
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
|
2月前
|
算法 测试技术
LeetCode刷题--- 430. 扁平化多级双向链表(深度优先搜索)
LeetCode刷题--- 430. 扁平化多级双向链表(深度优先搜索)
|
2月前
|
存储
实现单链表的基本操作(力扣、牛客刷题的基础&笔试题常客)
实现单链表的基本操作(力扣、牛客刷题的基础&笔试题常客)
144 38
|
10天前
刷题之Leetcode160题(超级详细)
刷题之Leetcode160题(超级详细)
11 0

热门文章

最新文章