Skip to content

Commit 430b827

Browse files
committed
feat: add solutions to lc problems: No.0709, 1309
- No.0709.To Lower Case - No.1309.Decrypt String from Alphabet to Integer Mapping
1 parent c7122a7 commit 430b827

File tree

10 files changed

+173
-45
lines changed

10 files changed

+173
-45
lines changed

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

+32-7
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ func toLowerCase(s string) string {
108108
}
109109
```
110110

111+
### **TypeScript**
112+
113+
```ts
114+
function toLowerCase(s: string): string {
115+
return s.toLowerCase();
116+
}
117+
```
118+
119+
```ts
120+
function toLowerCase(s: string): string {
121+
return [...s].map(c => String.fromCharCode(c.charCodeAt(0) | 32)).join('');
122+
}
123+
```
124+
111125
### **Rust**
112126

113127
```rust
@@ -121,14 +135,25 @@ impl Solution {
121135
```rust
122136
impl Solution {
123137
pub fn to_lower_case(s: String) -> String {
124-
String::from_utf8(
125-
s.as_bytes()
126-
.iter()
127-
.map(|&c| c + if c >= b'A' && c <= b'Z' { 32 } else { 0 })
128-
.collect(),
129-
)
130-
.unwrap()
138+
s.as_bytes()
139+
.iter()
140+
.map(|&c| char::from(if c >= b'A' && c <= b'Z' { c | 32 } else { c }))
141+
.collect()
142+
}
143+
}
144+
```
145+
146+
### **C**
147+
148+
```c
149+
char *toLowerCase(char *s) {
150+
int n = strlen(s);
151+
for (int i = 0; i < n; i++) {
152+
if (s[i] >= 'A' && s[i] <= 'Z') {
153+
s[i] |= 32;
154+
}
131155
}
156+
return s;
132157
}
133158
```
134159

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

+32-7
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ func toLowerCase(s string) string {
9696
}
9797
```
9898

99+
### **TypeScript**
100+
101+
```ts
102+
function toLowerCase(s: string): string {
103+
return s.toLowerCase();
104+
}
105+
```
106+
107+
```ts
108+
function toLowerCase(s: string): string {
109+
return [...s].map(c => String.fromCharCode(c.charCodeAt(0) | 32)).join('');
110+
}
111+
```
112+
99113
### **Rust**
100114

101115
```rust
@@ -109,14 +123,25 @@ impl Solution {
109123
```rust
110124
impl Solution {
111125
pub fn to_lower_case(s: String) -> String {
112-
String::from_utf8(
113-
s.as_bytes()
114-
.iter()
115-
.map(|&c| c + if c >= b'A' && c <= b'Z' { 32 } else { 0 })
116-
.collect(),
117-
)
118-
.unwrap()
126+
s.as_bytes()
127+
.iter()
128+
.map(|&c| char::from(if c >= b'A' && c <= b'Z' { c | 32 } else { c }))
129+
.collect()
130+
}
131+
}
132+
```
133+
134+
### **C**
135+
136+
```c
137+
char *toLowerCase(char *s) {
138+
int n = strlen(s);
139+
for (int i = 0; i < n; i++) {
140+
if (s[i] >= 'A' && s[i] <= 'Z') {
141+
s[i] |= 32;
142+
}
119143
}
144+
return s;
120145
}
121146
```
122147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
impl Solution {
2+
pub fn to_lower_case(s: String) -> String {
3+
String::from_utf8(
4+
s.as_bytes()
5+
.iter()
6+
.map(|&c| c + if c >= b'A' && c <= b'Z' { 32 } else { 0 })
7+
.collect(),
8+
)
9+
.unwrap()
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
impl Solution {
22
pub fn to_lower_case(s: String) -> String {
3-
String::from_utf8(
4-
s.as_bytes()
5-
.iter()
6-
.map(|&c| c + if c >= b'A' && c <= b'Z' { 32 } else { 0 })
7-
.collect(),
8-
)
9-
.unwrap()
3+
s.as_bytes()
4+
.iter()
5+
.map(|&c| char::from(if c >= b'A' && c <= b'Z' { c | 32 } else { c }))
6+
.collect()
107
}
118
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function toLowerCase(s: string): string {
2+
return [...s].map(c => String.fromCharCode(c.charCodeAt(0) | 32)).join('');
3+
}

solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/README.md

+32-8
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,20 @@ class Solution {
104104
```ts
105105
function freqAlphabets(s: string): string {
106106
const n = s.length;
107-
const res = [];
107+
const ans = [];
108108
let i = 0;
109109
while (i < n) {
110-
let code: string;
111-
if (s[i + 2] === '#') {
112-
code = s.slice(i, i + 2);
110+
if (s[i + 2] == '#') {
111+
ans.push(s.slice(i, i + 2));
113112
i += 3;
114113
} else {
115-
code = s[i];
114+
ans.push(s[i]);
116115
i += 1;
117116
}
118-
res.push(code);
119117
}
120-
return res.map(v => String.fromCharCode(96 + Number(v))).join('');
118+
return ans
119+
.map(c => String.fromCharCode('a'.charCodeAt(0) + Number(c) - 1))
120+
.join('');
121121
}
122122
```
123123

@@ -139,13 +139,37 @@ impl Solution {
139139
code = s[i];
140140
i += 1;
141141
}
142-
res.push(char::from(97 + code - b'1'));
142+
res.push(char::from('a' as u8 + code - b'1'));
143143
}
144144
res
145145
}
146146
}
147147
```
148148

149+
### **C**
150+
151+
```c
152+
char *freqAlphabets(char *s) {
153+
int n = strlen(s);
154+
int i = 0;
155+
int j = 0;
156+
char *ans = malloc(sizeof(s) * n);
157+
while (i < n) {
158+
int t;
159+
if (i + 2 < n && s[i + 2] == '#') {
160+
t = (s[i] - '0') * 10 + s[i + 1];
161+
i += 3;
162+
} else {
163+
t = s[i];
164+
i += 1;
165+
}
166+
ans[j++] = 'a' + t - '1';
167+
}
168+
ans[j] = '\0';
169+
return ans;
170+
}
171+
```
172+
149173
### **...**
150174
151175
```

solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/README_EN.md

+32-8
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,20 @@ class Solution {
9494
```ts
9595
function freqAlphabets(s: string): string {
9696
const n = s.length;
97-
const res = [];
97+
const ans = [];
9898
let i = 0;
9999
while (i < n) {
100-
let code: string;
101-
if (s[i + 2] === '#') {
102-
code = s.slice(i, i + 2);
100+
if (s[i + 2] == '#') {
101+
ans.push(s.slice(i, i + 2));
103102
i += 3;
104103
} else {
105-
code = s[i];
104+
ans.push(s[i]);
106105
i += 1;
107106
}
108-
res.push(code);
109107
}
110-
return res.map(v => String.fromCharCode(96 + Number(v))).join('');
108+
return ans
109+
.map(c => String.fromCharCode('a'.charCodeAt(0) + Number(c) - 1))
110+
.join('');
111111
}
112112
```
113113

@@ -129,13 +129,37 @@ impl Solution {
129129
code = s[i];
130130
i += 1;
131131
}
132-
res.push(char::from(97 + code - b'1'));
132+
res.push(char::from('a' as u8 + code - b'1'));
133133
}
134134
res
135135
}
136136
}
137137
```
138138

139+
### **C**
140+
141+
```c
142+
char *freqAlphabets(char *s) {
143+
int n = strlen(s);
144+
int i = 0;
145+
int j = 0;
146+
char *ans = malloc(sizeof(s) * n);
147+
while (i < n) {
148+
int t;
149+
if (i + 2 < n && s[i + 2] == '#') {
150+
t = (s[i] - '0') * 10 + s[i + 1];
151+
i += 3;
152+
} else {
153+
t = s[i];
154+
i += 1;
155+
}
156+
ans[j++] = 'a' + t - '1';
157+
}
158+
ans[j] = '\0';
159+
return ans;
160+
}
161+
```
162+
139163
### **...**
140164
141165
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
char *freqAlphabets(char *s) {
2+
int n = strlen(s);
3+
int i = 0;
4+
int j = 0;
5+
char *ans = malloc(sizeof(s) * n);
6+
while (i < n) {
7+
int t;
8+
if (i + 2 < n && s[i + 2] == '#') {
9+
t = (s[i] - '0') * 10 + s[i + 1];
10+
i += 3;
11+
} else {
12+
t = s[i];
13+
i += 1;
14+
}
15+
ans[j++] = 'a' + t - '1';
16+
}
17+
ans[j] = '\0';
18+
return ans;
19+
}

solution/1300-1399/1309.Decrypt String from Alphabet to Integer Mapping/Solution.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl Solution {
1313
code = s[i];
1414
i += 1;
1515
}
16-
res.push(char::from(97 + code - b'1'));
16+
res.push(char::from('a' as u8 + code - b'1'));
1717
}
1818
res
1919
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
function freqAlphabets(s: string): string {
22
const n = s.length;
3-
const res = [];
3+
const ans = [];
44
let i = 0;
55
while (i < n) {
6-
let code: string;
7-
if (s[i + 2] === '#') {
8-
code = s.slice(i, i + 2);
6+
if (s[i + 2] == '#') {
7+
ans.push(s.slice(i, i + 2));
98
i += 3;
109
} else {
11-
code = s[i];
10+
ans.push(s[i]);
1211
i += 1;
1312
}
14-
res.push(code);
1513
}
16-
return res.map(v => String.fromCharCode(96 + Number(v))).join('');
14+
return ans
15+
.map(c => String.fromCharCode('a'.charCodeAt(0) + Number(c) - 1))
16+
.join('');
1717
}

0 commit comments

Comments
 (0)