Skip to content

Commit c0a9580

Browse files
authored
feat: add rust solution to lc problem: No.0443 (doocs#1606)
No.0443.String Compression
1 parent 7601322 commit c0a9580

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

solution/0400-0499/0443.String Compression/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,34 @@ func compress(chars []byte) int {
161161
}
162162
```
163163

164+
### **Rust**
165+
166+
```rust
167+
impl Solution {
168+
pub fn compress(chars: &mut Vec<char>) -> i32 {
169+
let (mut i, mut k, n) = (0, 0, chars.len());
170+
while i < n {
171+
let mut j = i + 1;
172+
while j < n && chars[j] == chars[i] {
173+
j += 1;
174+
}
175+
chars[k] = chars[i];
176+
k += 1;
177+
178+
if j - i > 1 {
179+
let cnt = (j - i).to_string();
180+
for c in cnt.chars() {
181+
chars[k] = c;
182+
k += 1;
183+
}
184+
}
185+
i = j;
186+
}
187+
k as i32
188+
}
189+
}
190+
```
191+
164192
### **...**
165193

166194
```

solution/0400-0499/0443.String Compression/README_EN.md

+28
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,34 @@ func compress(chars []byte) int {
148148
}
149149
```
150150

151+
### **Rust**
152+
153+
```rust
154+
impl Solution {
155+
pub fn compress(chars: &mut Vec<char>) -> i32 {
156+
let (mut i, mut k, n) = (0, 0, chars.len());
157+
while i < n {
158+
let mut j = i + 1;
159+
while j < n && chars[j] == chars[i] {
160+
j += 1;
161+
}
162+
chars[k] = chars[i];
163+
k += 1;
164+
165+
if j - i > 1 {
166+
let cnt = (j - i).to_string();
167+
for c in cnt.chars() {
168+
chars[k] = c;
169+
k += 1;
170+
}
171+
}
172+
i = j;
173+
}
174+
k as i32
175+
}
176+
}
177+
```
178+
151179
### **...**
152180

153181
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn compress(chars: &mut Vec<char>) -> i32 {
3+
let (mut i, mut k, n) = (0, 0, chars.len());
4+
while i < n {
5+
let mut j = i + 1;
6+
while j < n && chars[j] == chars[i] {
7+
j += 1;
8+
}
9+
chars[k] = chars[i];
10+
k += 1;
11+
12+
if j - i > 1 {
13+
let cnt = (j - i).to_string();
14+
for c in cnt.chars() {
15+
chars[k] = c;
16+
k += 1;
17+
}
18+
}
19+
i = j;
20+
}
21+
k as i32
22+
}
23+
}

0 commit comments

Comments
 (0)