Skip to content

Commit 67521d7

Browse files
committed
feat: add solutions to lc problem: No.0709
No.0709.To Lower Case
1 parent b419d00 commit 67521d7

File tree

6 files changed

+101
-66
lines changed

6 files changed

+101
-66
lines changed

solution/0700-0799/0709.To Lower Case/README.md

+37-18
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
<strong>输出: </strong>&quot;lovely&quot;
3030
</pre>
3131

32-
3332
## 解法
3433

3534
<!-- 这里可写通用的实现逻辑 -->
@@ -44,17 +43,8 @@
4443

4544
```python
4645
class Solution:
47-
def toLowerCase(self, str: str) -> str:
48-
if not str:
49-
return str
50-
n = len(str)
51-
res = []
52-
for i in range(n):
53-
c = ord(str[i])
54-
if c >= 65 and c <= 90:
55-
c += 32
56-
res.append(chr(c))
57-
return ''.join(res)
46+
def toLowerCase(self, s: str) -> str:
47+
return ''.join([chr(ord(c) | 32) if ord('A') <= ord(c) <= ord('Z') else c for c in s])
5848
```
5949

6050
### **Java**
@@ -63,19 +53,48 @@ class Solution:
6353

6454
```java
6555
class Solution {
66-
public String toLowerCase(String str) {
67-
int n;
68-
if (str == null || (n = str.length()) == 0) return str;
69-
char[] chars = str.toCharArray();
56+
public String toLowerCase(String s) {
57+
char[] chars = s.toCharArray();
7058
for (int i = 0; i < chars.length; ++i) {
71-
boolean isUpper = chars[i] >= 'A' && chars[i] <= 'Z';
72-
if (isUpper) chars[i] += 32;
59+
if (chars[i] >= 'A' && chars[i] <= 'Z') {
60+
chars[i] |= 32;
61+
}
7362
}
7463
return new String(chars);
7564
}
7665
}
7766
```
7867

68+
### **C++**
69+
70+
```cpp
71+
class Solution {
72+
public:
73+
string toLowerCase(string s) {
74+
for (char& c : s)
75+
if (c >= 'A' && c <= 'Z')
76+
c |= 32;
77+
return s;
78+
}
79+
};
80+
```
81+
82+
### **Go**
83+
84+
```go
85+
func toLowerCase(s string) string {
86+
sb := &strings.Builder{}
87+
sb.Grow(len(s))
88+
for _, c := range s {
89+
if c >= 'A' && c <= 'Z' {
90+
c |= 32
91+
}
92+
sb.WriteRune(c)
93+
}
94+
return sb.String()
95+
}
96+
```
97+
7998
### **...**
8099

81100
```

solution/0700-0799/0709.To Lower Case/README_EN.md

+37-31
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,12 @@
66

77
<p>Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.</p>
88

9-
10-
119
<p>&nbsp;</p>
1210

13-
14-
1511
<div>
1612

1713
<p><strong>Example 1:</strong></p>
1814

19-
20-
2115
<pre>
2216

2317
<strong>Input: </strong><span id="example-input-1-1">&quot;Hello&quot;</span>
@@ -26,14 +20,10 @@
2620

2721
</pre>
2822

29-
30-
3123
<div>
3224

3325
<p><strong>Example 2:</strong></p>
3426

35-
36-
3727
<pre>
3828

3929
<strong>Input: </strong><span id="example-input-2-1">&quot;here&quot;</span>
@@ -42,14 +32,10 @@
4232

4333
</pre>
4434

45-
46-
4735
<div>
4836

4937
<p><strong>Example 3:</strong></p>
5038

51-
52-
5339
<pre>
5440

5541
<strong>Input: </strong><span id="example-input-3-1">&quot;LOVELY&quot;</span>
@@ -72,36 +58,56 @@
7258

7359
```python
7460
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])
8663
```
8764

8865
### **Java**
8966

9067
```java
9168
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();
9671
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+
}
9975
}
10076
return new String(chars);
10177
}
10278
}
10379
```
10480

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+
105111
### **...**
106112

107113
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public:
3+
string toLowerCase(string s) {
4+
for (char& c : s)
5+
if (c >= 'A' && c <= 'Z')
6+
c |= 32;
7+
return s;
8+
}
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func toLowerCase(s string) string {
2+
sb := &strings.Builder{}
3+
sb.Grow(len(s))
4+
for _, c := range s {
5+
if c >= 'A' && c <= 'Z' {
6+
c |= 32
7+
}
8+
sb.WriteRune(c)
9+
}
10+
return sb.String()
11+
}

solution/0700-0799/0709.To Lower Case/Solution.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
class Solution {
2-
public String toLowerCase(String str) {
3-
int n;
4-
if (str == null || (n = str.length()) == 0) return str;
5-
char[] chars = str.toCharArray();
2+
public String toLowerCase(String s) {
3+
char[] chars = s.toCharArray();
64
for (int i = 0; i < chars.length; ++i) {
7-
boolean isUpper = chars[i] >= 'A' && chars[i] <= 'Z';
8-
if (isUpper) chars[i] += 32;
5+
if (chars[i] >= 'A' && chars[i] <= 'Z') {
6+
chars[i] += 32;
7+
}
98
}
109
return new String(chars);
1110
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
11
class Solution:
2-
def toLowerCase(self, str: str) -> str:
3-
if not str:
4-
return str
5-
n = len(str)
6-
res = []
7-
for i in range(n):
8-
c = ord(str[i])
9-
if c >= 65 and c <= 90:
10-
c += 32
11-
res.append(chr(c))
12-
return ''.join(res)
2+
def toLowerCase(self, s: str) -> str:
3+
return ''.join([chr(ord(c) | 32) if ord('A') <= ord(c) <= ord('Z') else c for c in s])

0 commit comments

Comments
 (0)