Leetcode——709. To Lower Case

题目原址

https://leetcode.com/problems/to-lower-case/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:

Input: “LOVELY”
Output: “lovely”

解题思路

这个题有点。。。简单。。。是为了不要打击我的自尊心隔两个月做的第一道题吗!!!

AC代码

class Solution {
     
    public String toLowerCase(String str) {
        StringBuilder ret = new StringBuilder();
        int length = str.length();

        for(int i = 0; i < length; i++) {
            char c = str.charAt(i);

            if(c >= 'a' && c <= 'z')
                ret.append(c);
            else if(c >= 'A' && c <= 'Z'){
                ret.append((char)(c - 'A' + 'a'));
            }else
                ret.append(c);
        }
        return new String(ret);
    }
}

你可能感兴趣的:(LeetCode_String,709.,To,Lower,Case,Leetcode)