题目
给你一个字符串 s ,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。
示例 1:
输入:s = “Hello”
输出:“hello”
示例 2:
输入:s = “here”
输出:“here”
示例 3:
输入:s = “LOVELY”
输出:“lovely”
题解
1. 库函数
class Solution(object):
def toLowerCase(self, s):
"""
:type s: str
:rtype: str
"""
return s.lower()
2. ASCII + 32
class Solution(object):
def toLowerCase(self, s):
"""
:type s: str
:rtype: str
"""
result = []
for ch in s:
if 'A' <= ch <= 'Z':
result.append((chr(ord(ch) + 32)))
else:
result.append(ch)
return "".join(result)
3. ASCII 或 32
class Solution(object):
def toLowerCase(self, s):
"""
:type s: str
:rtype: str
"""
result = []
for ch in s:
if 'A' <= ch <= 'Z':
result.append((chr(ord(ch) | 32)))
else:
result.append(ch)
return "".join(result)