709. To Lower Case

Posted wentiliangkaihua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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"

class Solution {
    public String toLowerCase(String str) {
        int k = (int) (‘a‘);
        StringBuilder sb = new StringBuilder();
        for(char c: str.toCharArray()){
            int t = (int) (c);
            if(!Character.isLetter(c)) sb.append(c);
            else if(t >= k) sb.append(c);
            else sb.append((char) (t + 32));
        }
        return sb.toString();
    }
}
    public String toLowerCase(String str) {
        char[] a = str.toCharArray();
        for (int i = 0; i < a.length; i++)
            if (‘A‘ <= a[i] && a[i] <= ‘Z‘)
                a[i] = (char) (a[i] - ‘A‘ + ‘a‘);
        return new String(a);
    }

 

以上是关于709. To Lower Case的主要内容,如果未能解决你的问题,请参考以下文章

709. To Lower Case

709. To Lower Case

709. To Lower Case

709. To Lower Case

python leetcode practice(709. To Lower Case)

Leetcode709. To Lower Case