2. To Lower Case

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

时间: 2024-11-24 03:14:11

2. To Lower Case的相关文章

LeetCode--To Lower Case &amp; Remove Outermost Parentheses (Easy)

709. To Lower Case(Easy) 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"

[LeetCode]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" Examp

[LeetCode] 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: "L

709. To Lower Case

Algorithm to-lower-case https://leetcode.com/problems/to-lower-case/ 1)problem Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Exampl

LeetCode算法题-To Lower Case(Java实现)

这是悦乐书的第301次更新,第320篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第169题(顺位题号是709).实现具有字符串参数str的函数ToLowerCase():以小写形式返回相同的字符串.例如: 输入:"Hello" 输出:"hello" 输入:"here" 输出:"here" 输入:"LOVELY" 输出:"lovely" 本次解题使用的开发工

(Easy) To Lower Case LeetCode

Description 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: In

LintCode-Sort Letters by Case

Given a string which contains only letters. Sort it by lower case first and upper case second. Note It's not necessary to keep the original order of lower-case letters and upper case letters. Example For "abAcD", a reasonable answer is "acb

Sort Letters by Case

Given a string which contains only letters. Sort it by lower case first and upper case second. Example For "abAcD", a reasonable answer is "acbAD" 与将负数都放在前面,正数都放在后面的题目一样. 时间复杂度为O(n) 找到第一大写字母,记录其下标为i,则小写字母必定在i之后出现,在i之后找到第一个出现的小写字母j.交换i,

lintcode-medium-Sort Letters by Case

Given a string which contains only letters. Sort it by lower case first and upper case second. Notice It's NOT necessary to keep the original order of lower-case letters and upper case letters. Example For "abAcD", a reasonable answer is "a