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

2. To Lower Case

时间:2019-03-30 20:00:50      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:class   sam   output   obj   sci   plain   range   ret   fun   

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.

2. To Lower Case

标签:class   sam   output   obj   sci   plain   range   ret   fun   

原文地址:https://www.cnblogs.com/sxuer/p/10628692.html

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