Skip to content

Commit 372cb84

Browse files
committed
feat: add solutions to lc problem: No.0443.String Compression
1 parent e1b9902 commit 372cb84

File tree

8 files changed

+249
-24
lines changed

8 files changed

+249
-24
lines changed

lcof/面试题48. 最长不含重复字符的子字符串/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@
3535

3636
## 解法
3737

38+
“滑动窗口 + 哈希表”。
39+
40+
定义一个哈希表记录当前窗口内出现的字符,i、j 分别表示不重复子串的结束位置和开始位置,res 表示无重复字符子串的最大长度。
41+
42+
遍历 i,若 `[j, i - 1]` 窗口内存在 `s[i]`,则 j 循环向右移动,更新哈希表,直至 `[j, i - 1]` 窗口不存在 `s[i]`,循环结束。将 `s[i]` 加入哈希表中,此时 `[j, i]` 窗口内不含重复元素,更新 res 的最大值:`res = max(res, i - j + 1)`
43+
44+
最后返回 res 即可。
45+
3846
<!-- tabs:start -->
3947

4048
### **Python3**

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59-
双指针 + 滑动窗口”。
59+
滑动窗口 + 哈希表”。
6060

6161
定义一个哈希表记录当前窗口内出现的字符,i、j 分别表示不重复子串的结束位置和开始位置,res 表示无重复字符子串的最大长度。
6262

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

+87-1
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,108 @@
7272

7373
<!-- 这里可写通用的实现逻辑 -->
7474

75+
双指针。
76+
7577
<!-- tabs:start -->
7678

7779
### **Python3**
7880

7981
<!-- 这里可写当前语言的特殊实现逻辑 -->
8082

8183
```python
82-
84+
class Solution:
85+
def compress(self, chars: List[str]) -> int:
86+
i, k, n = 0, 0, len(chars)
87+
while i < n:
88+
j = i + 1
89+
while j < n and chars[j] == chars[i]:
90+
j += 1
91+
chars[k] = chars[i]
92+
k += 1
93+
if j - i > 1:
94+
cnt = str(j - i)
95+
for c in cnt:
96+
chars[k] = c
97+
k += 1
98+
i = j
99+
return k
83100
```
84101

85102
### **Java**
86103

87104
<!-- 这里可写当前语言的特殊实现逻辑 -->
88105

89106
```java
107+
class Solution {
108+
public int compress(char[] chars) {
109+
int k = 0, n = chars.length;
110+
for (int i = 0, j = i + 1; i < n;) {
111+
while (j < n && chars[j] == chars[i]) {
112+
++j;
113+
}
114+
chars[k++] = chars[i];
115+
if (j - i > 1) {
116+
String cnt = String.valueOf(j - i);
117+
for (char c : cnt.toCharArray()) {
118+
chars[k++] = c;
119+
}
120+
}
121+
i = j;
122+
}
123+
return k;
124+
}
125+
}
126+
```
127+
128+
### **C++**
129+
130+
```cpp
131+
class Solution {
132+
public:
133+
int compress(vector<char> &chars) {
134+
int k = 0, n = chars.size();
135+
for (int i = 0, j = i + 1; i < n;)
136+
{
137+
while (j < n && chars[j] == chars[i])
138+
++j;
139+
chars[k++] = chars[i];
140+
if (j - i > 1)
141+
{
142+
for (char c : to_string(j - i))
143+
{
144+
chars[k++] = c;
145+
}
146+
}
147+
i = j;
148+
}
149+
return k;
150+
}
151+
};
152+
```
90153
154+
### **Go**
155+
156+
```go
157+
func compress(chars []byte) int {
158+
i, k, n := 0, 0, len(chars)
159+
for i < n {
160+
j := i + 1
161+
for j < n && chars[j] == chars[i] {
162+
j++
163+
}
164+
chars[k] = chars[i]
165+
k++
166+
if j-i > 1 {
167+
cnt := strconv.Itoa(j - i)
168+
for _, c := range cnt {
169+
chars[k] = byte(c)
170+
k++
171+
}
172+
}
173+
i = j
174+
}
175+
return k
176+
}
91177
```
92178

93179
### **...**

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

+85-1
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,97 @@ Could you solve it using only <code>O(1)</code> extra space?</p>
6969
### **Python3**
7070

7171
```python
72-
72+
class Solution:
73+
def compress(self, chars: List[str]) -> int:
74+
i, k, n = 0, 0, len(chars)
75+
while i < n:
76+
j = i + 1
77+
while j < n and chars[j] == chars[i]:
78+
j += 1
79+
chars[k] = chars[i]
80+
k += 1
81+
if j - i > 1:
82+
cnt = str(j - i)
83+
for c in cnt:
84+
chars[k] = c
85+
k += 1
86+
i = j
87+
return k
7388
```
7489

7590
### **Java**
7691

7792
```java
93+
class Solution {
94+
public int compress(char[] chars) {
95+
int k = 0, n = chars.length;
96+
for (int i = 0, j = i + 1; i < n;) {
97+
while (j < n && chars[j] == chars[i]) {
98+
++j;
99+
}
100+
chars[k++] = chars[i];
101+
if (j - i > 1) {
102+
String cnt = String.valueOf(j - i);
103+
for (char c : cnt.toCharArray()) {
104+
chars[k++] = c;
105+
}
106+
}
107+
i = j;
108+
}
109+
return k;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
int compress(vector<char> &chars) {
120+
int k = 0, n = chars.size();
121+
for (int i = 0, j = i + 1; i < n;)
122+
{
123+
while (j < n && chars[j] == chars[i])
124+
++j;
125+
chars[k++] = chars[i];
126+
if (j - i > 1)
127+
{
128+
for (char c : to_string(j - i))
129+
{
130+
chars[k++] = c;
131+
}
132+
}
133+
i = j;
134+
}
135+
return k;
136+
}
137+
};
138+
```
78139
140+
### **Go**
141+
142+
```go
143+
func compress(chars []byte) int {
144+
i, k, n := 0, 0, len(chars)
145+
for i < n {
146+
j := i + 1
147+
for j < n && chars[j] == chars[i] {
148+
j++
149+
}
150+
chars[k] = chars[i]
151+
k++
152+
if j-i > 1 {
153+
cnt := strconv.Itoa(j - i)
154+
for _, c := range cnt {
155+
chars[k] = byte(c)
156+
k++
157+
}
158+
}
159+
i = j
160+
}
161+
return k
162+
}
79163
```
80164

81165
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
class Solution {
22
public:
3-
int compress(vector<char>& chars) {
4-
char pre = chars[0] ;
5-
int cnt = 1 ;
6-
int len = 0 ;
7-
for (int i = 1; ; ++i)
3+
int compress(vector<char> &chars) {
4+
int k = 0, n = chars.size();
5+
for (int i = 0, j = i + 1; i < n;)
86
{
9-
if (i < chars.size() && chars[i] == pre)
10-
++cnt ;
11-
else
7+
while (j < n && chars[j] == chars[i])
8+
++j;
9+
chars[k++] = chars[i];
10+
if (j - i > 1)
1211
{
13-
chars[len++] = pre ;
14-
15-
if (cnt > 1)
16-
for (auto c: to_string(cnt))
17-
chars[len++] = c ;
18-
19-
if (i >= chars.size())
20-
break ;
21-
22-
pre = chars[i] ;
23-
cnt = 1 ;
12+
for (char c : to_string(j - i))
13+
{
14+
chars[k++] = c;
15+
}
2416
}
17+
i = j;
2518
}
26-
27-
return len ;
19+
return k;
2820
}
2921
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func compress(chars []byte) int {
2+
i, k, n := 0, 0, len(chars)
3+
for i < n {
4+
j := i + 1
5+
for j < n && chars[j] == chars[i] {
6+
j++
7+
}
8+
chars[k] = chars[i]
9+
k++
10+
if j-i > 1 {
11+
cnt := strconv.Itoa(j - i)
12+
for _, c := range cnt {
13+
chars[k] = byte(c)
14+
k++
15+
}
16+
}
17+
i = j
18+
}
19+
return k
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int compress(char[] chars) {
3+
int k = 0, n = chars.length;
4+
for (int i = 0, j = i + 1; i < n;) {
5+
while (j < n && chars[j] == chars[i]) {
6+
++j;
7+
}
8+
chars[k++] = chars[i];
9+
if (j - i > 1) {
10+
String cnt = String.valueOf(j - i);
11+
for (char c : cnt.toCharArray()) {
12+
chars[k++] = c;
13+
}
14+
}
15+
i = j;
16+
}
17+
return k;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def compress(self, chars: List[str]) -> int:
3+
i, k, n = 0, 0, len(chars)
4+
while i < n:
5+
j = i + 1
6+
while j < n and chars[j] == chars[i]:
7+
j += 1
8+
chars[k] = chars[i]
9+
k += 1
10+
if j - i > 1:
11+
cnt = str(j - i)
12+
for c in cnt:
13+
chars[k] = c
14+
k += 1
15+
i = j
16+
return k

0 commit comments

Comments
 (0)