|
6 | 6 |
|
7 | 7 | <p>Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.</p>
|
8 | 8 |
|
9 |
| - |
10 |
| - |
11 | 9 | <p> </p>
|
12 | 10 |
|
13 |
| - |
14 |
| - |
15 | 11 | <div>
|
16 | 12 |
|
17 | 13 | <p><strong>Example 1:</strong></p>
|
18 | 14 |
|
19 |
| - |
20 |
| - |
21 | 15 | <pre>
|
22 | 16 |
|
23 | 17 | <strong>Input: </strong><span id="example-input-1-1">"Hello"</span>
|
|
26 | 20 |
|
27 | 21 | </pre>
|
28 | 22 |
|
29 |
| - |
30 |
| - |
31 | 23 | <div>
|
32 | 24 |
|
33 | 25 | <p><strong>Example 2:</strong></p>
|
34 | 26 |
|
35 |
| - |
36 |
| - |
37 | 27 | <pre>
|
38 | 28 |
|
39 | 29 | <strong>Input: </strong><span id="example-input-2-1">"here"</span>
|
|
42 | 32 |
|
43 | 33 | </pre>
|
44 | 34 |
|
45 |
| - |
46 |
| - |
47 | 35 | <div>
|
48 | 36 |
|
49 | 37 | <p><strong>Example 3:</strong></p>
|
50 | 38 |
|
51 |
| - |
52 |
| - |
53 | 39 | <pre>
|
54 | 40 |
|
55 | 41 | <strong>Input: </strong><span id="example-input-3-1">"LOVELY"</span>
|
|
72 | 58 |
|
73 | 59 | ```python
|
74 | 60 | class Solution:
|
75 |
| - def toLowerCase(self, str: str) -> str: |
76 |
| - if not str: |
77 |
| - return str |
78 |
| - n = len(str) |
79 |
| - res = [] |
80 |
| - for i in range(n): |
81 |
| - c = ord(str[i]) |
82 |
| - if c >= 65 and c <= 90: |
83 |
| - c += 32 |
84 |
| - res.append(chr(c)) |
85 |
| - return ''.join(res) |
| 61 | + def toLowerCase(self, s: str) -> str: |
| 62 | + return ''.join([chr(ord(c) | 32) if ord('A') <= ord(c) <= ord('Z') else c for c in s]) |
86 | 63 | ```
|
87 | 64 |
|
88 | 65 | ### **Java**
|
89 | 66 |
|
90 | 67 | ```java
|
91 | 68 | class Solution {
|
92 |
| - public String toLowerCase(String str) { |
93 |
| - int n; |
94 |
| - if (str == null || (n = str.length()) == 0) return str; |
95 |
| - char[] chars = str.toCharArray(); |
| 69 | + public String toLowerCase(String s) { |
| 70 | + char[] chars = s.toCharArray(); |
96 | 71 | for (int i = 0; i < chars.length; ++i) {
|
97 |
| - boolean isUpper = chars[i] >= 'A' && chars[i] <= 'Z'; |
98 |
| - if (isUpper) chars[i] += 32; |
| 72 | + if (chars[i] >= 'A' && chars[i] <= 'Z') { |
| 73 | + chars[i] |= 32; |
| 74 | + } |
99 | 75 | }
|
100 | 76 | return new String(chars);
|
101 | 77 | }
|
102 | 78 | }
|
103 | 79 | ```
|
104 | 80 |
|
| 81 | +### **C++** |
| 82 | + |
| 83 | +```cpp |
| 84 | +class Solution { |
| 85 | +public: |
| 86 | + string toLowerCase(string s) { |
| 87 | + for (char& c : s) |
| 88 | + if (c >= 'A' && c <= 'Z') |
| 89 | + c |= 32; |
| 90 | + return s; |
| 91 | + } |
| 92 | +}; |
| 93 | +``` |
| 94 | +
|
| 95 | +### **Go** |
| 96 | +
|
| 97 | +```go |
| 98 | +func toLowerCase(s string) string { |
| 99 | + sb := &strings.Builder{} |
| 100 | + sb.Grow(len(s)) |
| 101 | + for _, c := range s { |
| 102 | + if c >= 'A' && c <= 'Z' { |
| 103 | + c |= 32 |
| 104 | + } |
| 105 | + sb.WriteRune(c) |
| 106 | + } |
| 107 | + return sb.String() |
| 108 | +} |
| 109 | +``` |
| 110 | + |
105 | 111 | ### **...**
|
106 | 112 |
|
107 | 113 | ```
|
|
0 commit comments