-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathToLowerCase.java
More file actions
72 lines (66 loc) · 1.79 KB
/
Copy pathToLowerCase.java
File metadata and controls
72 lines (66 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* <p>给你一个字符串 <code>s</code> ,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。</p>
*
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
*
* <pre>
* <strong>输入:</strong>s = "Hello"
* <strong>输出:</strong>"hello"
* </pre>
*
* <p><strong>示例 2:</strong></p>
*
* <pre>
* <strong>输入:</strong>s = "here"
* <strong>输出:</strong>"here"
* </pre>
*
* <p><strong>示例 3:</strong></p>
*
* <pre>
* <strong>输入:</strong>s = "LOVELY"
* <strong>输出:</strong>"lovely"
* </pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li><code>1 <= s.length <= 100</code></li>
* <li><code>s</code> 由 ASCII 字符集中的可打印字符组成</li>
* </ul>
* <div><div>Related Topics</div><div><li>字符串</li></div></div><br><div><li>👍 198</li><li>👎 0</li></div>
*/
package leetcode9;
public class ToLowerCase {
public static void main(String[] args) {
Solution solution = new ToLowerCase().new Solution();
}
class Solution {
public String toLowerCase(String s) {
char[] arr = s.toCharArray();
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= 'A' && arr[i] <= 'Z') {
arr[i] += 0x20;
}
}
return new String(arr);
}
}
class Solution2 {
public String toLowerCase(String str) {
StringBuilder builder = new StringBuilder();
for (char c : str.toCharArray()) {
if (c >= 'A' && c <= 'Z') {
builder.append((char) (c - 'A' + 'a'));
} else {
builder.append(c);
}
}
return builder.toString();
}
}
}