1. 程式人生 > >Leetcode——709. To Lower Case

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”

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); } }