Skip to content

Commit 2136f51

Browse files
authored
feat: add solutions to lc problems: No.0767,2244
* No.0767.Reorganize String * No.2244.Minimum Rounds to Complete All Tasks
1 parent e470171 commit 2136f51

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+874
-63
lines changed

solution/0000-0099/0006.ZigZag Conversion/README.md solution/0000-0099/0006.Zigzag Conversion/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# [6. Z 字形变换](https://leetcode.cn/problems/zigzag-conversion)
22

3-
[English Version](/solution/0000-0099/0006.ZigZag%20Conversion/README_EN.md)
3+
[English Version](/solution/0000-0099/0006.Zigzag%20Conversion/README_EN.md)
44

55
## 题目描述
66

@@ -30,7 +30,6 @@ string convert(string s, int numRows);</pre>
3030
<strong>输入:</strong>s = "PAYPALISHIRING", numRows = 3
3131
<strong>输出:</strong>"PAHNAPLSIIGYIR"
3232
</pre>
33-
3433
<strong>示例 2:</strong>
3534

3635
<pre>
@@ -60,6 +59,7 @@ P I
6059
<li><code>1 <= numRows <= 1000</code></li>
6160
</ul>
6261

62+
6363
## 解法
6464

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

solution/0000-0099/0006.ZigZag Conversion/README_EN.md solution/0000-0099/0006.Zigzag Conversion/README_EN.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# [6. ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion)
1+
# [6. Zigzag Conversion](https://leetcode.com/problems/zigzag-conversion)
22

3-
[中文文档](/solution/0000-0099/0006.ZigZag%20Conversion/README.md)
3+
[中文文档](/solution/0000-0099/0006.Zigzag%20Conversion/README.md)
44

55
## Description
66

@@ -56,6 +56,7 @@ P I
5656
<li><code>1 &lt;= numRows &lt;= 1000</code></li>
5757
</ul>
5858

59+
5960
## Solutions
6061

6162
<!-- tabs:start -->

solution/0100-0199/0146.Lru Cache/README.md solution/0100-0199/0146.LRU Cache/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [146. LRU 缓存机制](https://leetcode.cn/problems/lru-cache)
1+
# [146. LRU 缓存](https://leetcode.cn/problems/lru-cache)
22

33
[English Version](/solution/0100-0199/0146.LRU%20Cache/README_EN.md)
44

@@ -57,10 +57,13 @@ lRUCache.get(4); // 返回 4
5757
<li>最多调用 <code>2 * 10<sup>5</sup></code> 次 <code>get</code> 和 <code>put</code></li>
5858
</ul>
5959

60+
6061
## 解法
6162

6263
<!-- 这里可写通用的实现逻辑 -->
6364

65+
**方法一:哈希表 + 双向链表**
66+
6467
“哈希表 + 双向链表”实现。其中:
6568

6669
- 双向链表按照被使用的顺序存储 kv 键值对,靠近头部的 kv 键值对是最近使用的,而靠近尾部的键值对是最久未使用的。

solution/0100-0199/0146.Lru Cache/README_EN.md solution/0100-0199/0146.LRU Cache/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ lRUCache.get(4); // return 4
4949
<li>At most 2<code>&nbsp;* 10<sup>5</sup></code>&nbsp;calls will be made to <code>get</code> and <code>put</code>.</li>
5050
</ul>
5151

52+
5253
## Solutions
5354

5455
<!-- tabs:start -->

solution/0100-0199/0158.Read N Characters Given Read4 II - Call multiple times/README.md solution/0100-0199/0158.Read N Characters Given read4 II - Call Multiple Times/README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# [158. 用 Read4 读取 N 个字符 II](https://leetcode.cn/problems/read-n-characters-given-read4-ii-call-multiple-times)
22

3-
[English Version](/solution/0100-0199/0158.Read%20N%20Characters%20Given%20Read4%20II%20-%20Call%20multiple%20times/README_EN.md)
3+
[English Version](/solution/0100-0199/0158.Read%20N%20Characters%20Given%20read4%20II%20-%20Call%20Multiple%20Times/README_EN.md)
44

55
## 题目描述
66

@@ -56,7 +56,7 @@ read4(buf4); // read4 返回 0。现在 buf4 = "",fp 指向文件末尾</code>
5656
<ul>
5757
<li>你 <strong>不能</strong> 直接操作该文件,文件只能通过 <code>read4</code> 获取而 <strong>不能</strong> 通过 <code>read</code>。</li>
5858
<li><code>read</code>&nbsp; 函数可以被调用&nbsp;<strong>多次</strong>。</li>
59-
<li>请记得&nbsp;<strong>重置&nbsp;</strong>在 Solution 中声明的类变量(静态变量),因为类变量会&nbsp;<strong>在多个测试用例中保持不变</strong>,影响判题准确。请 <a href="https://support.leetcode.cn/hc/kb/section/1071534/" target="_blank">查阅</a> 这里。</li>
59+
<li>请记得&nbsp;<strong>重置&nbsp;</strong>在 Solution 中声明的类变量(静态变量),因为类变量会&nbsp;<strong>在多个测试用例中保持不变</strong>,影响判题准确。请 <a href="https://support.leetcode-cn.com/hc/kb/section/1071534/" target="_blank">查阅</a> 这里。</li>
6060
<li>你可以假定目标缓存数组&nbsp;<code>buf</code> 保证有足够的空间存下 n 个字符。&nbsp;</li>
6161
<li>保证在一个给定测试用例中,<code>read</code> 函数使用的是同一个 <code>buf</code>。</li>
6262
</ul>
@@ -102,6 +102,7 @@ sol.read (buf, 1); // 我们已经到达文件的末尾,不能读取更多的
102102
<li><code>1 &lt;= queries[i] &lt;= 500</code></li>
103103
</ul>
104104

105+
105106
## 解法
106107

107108
<!-- 这里可写通用的实现逻辑 -->
@@ -124,6 +125,12 @@ sol.read (buf, 1); // 我们已经到达文件的末尾,不能读取更多的
124125

125126
```
126127

128+
### **TypeScript**
129+
130+
```ts
131+
132+
```
133+
127134
### **...**
128135

129136
```

solution/0100-0199/0158.Read N Characters Given Read4 II - Call multiple times/README_EN.md solution/0100-0199/0158.Read N Characters Given read4 II - Call Multiple Times/README_EN.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# [158. Read N Characters Given Read4 II - Call multiple times](https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times)
1+
# [158. Read N Characters Given read4 II - Call Multiple Times](https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times)
22

3-
[中文文档](/solution/0100-0199/0158.Read%20N%20Characters%20Given%20Read4%20II%20-%20Call%20multiple%20times/README.md)
3+
[中文文档](/solution/0100-0199/0158.Read%20N%20Characters%20Given%20read4%20II%20-%20Call%20Multiple%20Times/README.md)
44

55
## Description
66

@@ -97,6 +97,7 @@ sol.read(buf, 1); // We have reached the end of file, no more characters can be
9797
<li><code>1 &lt;= queries[i] &lt;= 500</code></li>
9898
</ul>
9999

100+
100101
## Solutions
101102

102103
<!-- tabs:start -->
@@ -113,6 +114,12 @@ sol.read(buf, 1); // We have reached the end of file, no more characters can be
113114

114115
```
115116

117+
### **TypeScript**
118+
119+
```ts
120+
121+
```
122+
116123
### **...**
117124

118125
```

solution/0700-0799/0767.Reorganize String/README.md

+149-1
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,170 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
**方法一:哈希表**
43+
44+
利用哈希表 cnt 统计字符串 s 中每个字符出现的次数。
45+
46+
若最大的出现次数 mx 大于 `(n + 1) / 2`,说明一定会存在两个相同字符相邻,直接返回 ''。
47+
48+
否则,按字符出现频率从大到小遍历,依次间隔 1 个位置填充字符。若位置大于等于 n,则重置为 1 继续填充。
49+
4250
<!-- tabs:start -->
4351

4452
### **Python3**
4553

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

4856
```python
49-
57+
class Solution:
58+
def reorganizeString(self, s: str) -> str:
59+
n = len(s)
60+
cnt = Counter(s)
61+
mx = max(cnt.values())
62+
if mx > (n + 1) // 2:
63+
return ''
64+
i = 0
65+
ans = [None] * n
66+
for k, v in cnt.most_common():
67+
while v:
68+
ans[i] = k
69+
v -= 1
70+
i += 2
71+
if i >= n:
72+
i = 1
73+
return ''.join(ans)
5074
```
5175

5276
### **Java**
5377

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

5680
```java
81+
class Solution {
82+
public String reorganizeString(String s) {
83+
int[] cnt = new int[26];
84+
int mx = 0;
85+
for (char c : s.toCharArray()) {
86+
int t = c - 'a';
87+
++cnt[t];
88+
mx = Math.max(mx, cnt[t]);
89+
}
90+
int n = s.length();
91+
if (mx > (n + 1) / 2) {
92+
return "";
93+
}
94+
int k = 0;
95+
for (int v : cnt) {
96+
if (v > 0) {
97+
++k;
98+
}
99+
}
100+
int[][] m = new int[k][2];
101+
k = 0;
102+
for (int i = 0; i < 26; ++i) {
103+
if (cnt[i] > 0) {
104+
m[k++] = new int[]{cnt[i], i};
105+
}
106+
}
107+
Arrays.sort(m, (a, b) -> b[0] - a[0]);
108+
k = 0;
109+
StringBuilder ans = new StringBuilder(s);
110+
for (int[] e : m) {
111+
int v = e[0], i = e[1];
112+
while (v-- > 0) {
113+
ans.setCharAt(k, (char) ('a' + i));
114+
k += 2;
115+
if (k >= n) {
116+
k = 1;
117+
}
118+
}
119+
}
120+
return ans.toString();
121+
}
122+
}
123+
```
124+
125+
### **C++**
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
string reorganizeString(string s) {
131+
vector<int> cnt(26);
132+
for (char& c : s) ++cnt[c - 'a'];
133+
int mx = *max_element(cnt.begin(), cnt.end());
134+
int n = s.size();
135+
if (mx > (n + 1) / 2) return "";
136+
vector<vector<int>> m;
137+
for (int i = 0; i < 26; ++i)
138+
{
139+
if (cnt[i]) m.push_back({cnt[i], i});
140+
}
141+
sort(m.begin(), m.end());
142+
reverse(m.begin(), m.end());
143+
string ans = s;
144+
int k = 0;
145+
for (auto& e : m)
146+
{
147+
int v = e[0], i = e[1];
148+
while (v--)
149+
{
150+
ans[k] = 'a' + i;
151+
k += 2;
152+
if (k >= n) k = 1;
153+
}
154+
}
155+
return ans;
156+
}
157+
};
158+
```
57159
160+
### **Go**
161+
162+
```go
163+
func reorganizeString(s string) string {
164+
cnt := make([]int, 26)
165+
mx := 0
166+
for _, c := range s {
167+
t := c - 'a'
168+
cnt[t]++
169+
mx = max(mx, cnt[t])
170+
}
171+
n := len(s)
172+
if mx > (n+1)/2 {
173+
return ""
174+
}
175+
m := [][]int{}
176+
for i, v := range cnt {
177+
if v > 0 {
178+
m = append(m, []int{v, i})
179+
}
180+
}
181+
sort.Slice(m, func(i, j int) bool {
182+
return m[i][0] > m[j][0]
183+
})
184+
ans := make([]byte, n)
185+
k := 0
186+
for _, e := range m {
187+
v, i := e[0], e[1]
188+
for v > 0 {
189+
ans[k] = byte('a' + i)
190+
k += 2
191+
if k >= n {
192+
k = 1
193+
}
194+
v--
195+
}
196+
}
197+
return string(ans)
198+
}
199+
200+
func max(a, b int) int {
201+
if a > b {
202+
return a
203+
}
204+
return b
205+
}
58206
```
59207

60208
### **...**

0 commit comments

Comments
 (0)