【LeetCode】709(Java)To Lower Case

Question:

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.

实现函数ToLowerCase str()有一个字符串参数,并返回相同的字符串小写。

Example 1:

Input: "Hello"
Output: "hello"

Example 2:

Input: "here"
Output: "here"

Example 3:

Input: "LOVELY"
Output: "lovely"

方法一:

    public String toLowerCase(String str) {
		char[] s = str.toCharArray();
		for(int i = 0 ; i < str.length() ; i++) {
			if(s[i] >= 'A' && s[i] <= 'Z') {
				s[i] += 32;
			}
		}
		return new String(s);
    }

方法二:

public String toLowerCase(String str) {
		StringBuilder sb = new StringBuilder();
		for(int i=0 ; i < str.length() ; i++) {
			char s = str.charAt(i);
			if(s >= 'A' && s <= 'Z') {
				s += 32;
				sb.append(Character.toString((char)s));
				continue;
			}
			sb.append(s);
		}
		return sb.toString();
    }

猜你喜欢

转载自blog.csdn.net/weixin_40849588/article/details/81462629
今日推荐