Title:
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"
Note:
None
Analysis of Title:
There is nothing to explain.
Test case:
"Hello"
Python:
class Solution(object):
def toLowerCase(self, str):
"""
:type str: str
:rtype: str
"""
res = ""
for c in str:
bse = ord(c)
if bse<91 and bse>64:
res+=chr(bse + 32)
else:
res+=c
return res
Analysis of Code:
1.ord() return the ASCII number
2.chr() return the ASCII char
3. (64,91) This is the Capital letters’s range, A = 65, Z = 90
4. The capital letters add 32 equals Lowercase letters.
So, go through the str, if it‘s a capital, add 32, else it‘s a lowercase letters originally.
Notice: type(res)=str, and str+int=str, so "else:res+=c" is return str-type successful although c is int-type.
原文地址:https://www.cnblogs.com/sxuer/p/10628692.html