Skip to content

Commit 333b955

Browse files
committedFeb 11, 2022
feat: update solutions to lc/lcof2 problems
* lc No.0515 & lcof2 No.044.Find Largest Value in Each Tree Row * lc No.0424.Longest Repeating Character Replacement
1 parent d238f0d commit 333b955

File tree

18 files changed

+245
-56
lines changed

18 files changed

+245
-56
lines changed
 

‎lcof2/剑指 Offer II 044. 二叉树每层的最大值/README.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ class Solution:
9696
q = deque([root])
9797
ans = []
9898
while q:
99-
n = len(q)
10099
t = float('-inf')
101-
for _ in range(n):
100+
for _ in range(len(q), 0, -1):
102101
node = q.popleft()
103102
t = max(t, node.val)
104103
if node.left:
@@ -131,15 +130,15 @@ class Solution:
131130
*/
132131
class Solution {
133132
public List<Integer> largestValues(TreeNode root) {
133+
List<Integer> ans = new ArrayList<>();
134134
if (root == null) {
135-
return Collections.emptyList();
135+
return ans;
136136
}
137137
Deque<TreeNode> q = new ArrayDeque<>();
138138
q.offer(root);
139-
List<Integer> ans = new ArrayList<>();
140139
while (!q.isEmpty()) {
141140
int t = Integer.MIN_VALUE;
142-
for (int i = 0, n = q.size(); i < n; ++i) {
141+
for (int i = q.size(); i > 0; --i) {
143142
TreeNode node = q.poll();
144143
t = Math.max(t, node.val);
145144
if (node.left != null) {
@@ -179,7 +178,7 @@ public:
179178
while (!q.empty())
180179
{
181180
int t = INT_MIN;
182-
for (int i = 0, n = q.size(); i < n; ++i)
181+
for (int i = q.size(); i > 0; --i)
183182
{
184183
auto node = q.front();
185184
q.pop();
@@ -212,9 +211,8 @@ func largestValues(root *TreeNode) []int {
212211
}
213212
var q = []*TreeNode{root}
214213
for len(q) > 0 {
215-
n := len(q)
216214
t := math.MinInt32
217-
for i := 0; i < n; i++ {
215+
for i := len(q); i > 0; i-- {
218216
node := q[0]
219217
q = q[1:]
220218
t = max(t, node.Val)

‎lcof2/剑指 Offer II 044. 二叉树每层的最大值/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Solution {
1818
while (!q.empty())
1919
{
2020
int t = INT_MIN;
21-
for (int i = 0, n = q.size(); i < n; ++i)
21+
for (int i = q.size(); i > 0; --i)
2222
{
2323
auto node = q.front();
2424
q.pop();

‎lcof2/剑指 Offer II 044. 二叉树每层的最大值/Solution.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ func largestValues(root *TreeNode) []int {
1313
}
1414
var q = []*TreeNode{root}
1515
for len(q) > 0 {
16-
n := len(q)
1716
t := math.MinInt32
18-
for i := 0; i < n; i++ {
17+
for i := len(q); i > 0; i-- {
1918
node := q[0]
2019
q = q[1:]
2120
t = max(t, node.Val)

‎lcof2/剑指 Offer II 044. 二叉树每层的最大值/Solution.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
*/
1616
class Solution {
1717
public List<Integer> largestValues(TreeNode root) {
18+
List<Integer> ans = new ArrayList<>();
1819
if (root == null) {
19-
return Collections.emptyList();
20+
return ans;
2021
}
2122
Deque<TreeNode> q = new ArrayDeque<>();
2223
q.offer(root);
23-
List<Integer> ans = new ArrayList<>();
2424
while (!q.isEmpty()) {
2525
int t = Integer.MIN_VALUE;
26-
for (int i = 0, n = q.size(); i < n; ++i) {
26+
for (int i = q.size(); i > 0; --i) {
2727
TreeNode node = q.poll();
2828
t = Math.max(t, node.val);
2929
if (node.left != null) {

‎lcof2/剑指 Offer II 044. 二叉树每层的最大值/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ def largestValues(self, root: TreeNode) -> List[int]:
1111
q = deque([root])
1212
ans = []
1313
while q:
14-
n = len(q)
1514
t = float('-inf')
16-
for _ in range(n):
15+
for _ in range(len(q), 0, -1):
1716
node = q.popleft()
1817
t = max(t, node.val)
1918
if node.left:

‎solution/0400-0499/0424.Longest Repeating Character Replacement/README.md

+80-1
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,101 @@
3434

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

37+
我们维护一个数组 `int[26]` 来存储当前窗口中各个字母的出现次数,j 表示窗口的左边界,i 表示窗口右边界。
38+
39+
- 窗口扩张:j 不变,i++
40+
- 窗口滑动:j++,i++
41+
42+
`maxCnt` 保存滑动窗口内相同字母出现次数的**历史最大值**,通过判断窗口宽度 `i - j + 1` 是否大于 `maxCnt + k` 来决定窗口是否做滑动,否则窗口就扩张。
43+
3744
<!-- tabs:start -->
3845

3946
### **Python3**
4047

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

4350
```python
44-
51+
class Solution:
52+
def characterReplacement(self, s: str, k: int) -> int:
53+
counter = [0] * 26
54+
i = j = maxCnt = 0
55+
while i < len(s):
56+
counter[ord(s[i]) - ord('A')] += 1
57+
maxCnt = max(maxCnt, counter[ord(s[i]) - ord('A')])
58+
if i - j + 1 > maxCnt + k:
59+
counter[ord(s[j]) - ord('A')] -= 1
60+
j += 1
61+
i += 1
62+
return i - j
4563
```
4664

4765
### **Java**
4866

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

5169
```java
70+
class Solution {
71+
public int characterReplacement(String s, int k) {
72+
int[] counter = new int[26];
73+
int i = 0;
74+
int j = 0;
75+
for (int maxCnt = 0; i < s.length(); ++i) {
76+
char c = s.charAt(i);
77+
++counter[c - 'A'];
78+
maxCnt = Math.max(maxCnt, counter[c - 'A']);
79+
if (i - j + 1 - maxCnt > k) {
80+
--counter[s.charAt(j) - 'A'];
81+
++j;
82+
}
83+
}
84+
return i - j;
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
int characterReplacement(string s, int k) {
95+
vector<int> counter(26);
96+
int i = 0, j = 0, maxCnt = 0;
97+
for (char& c : s)
98+
{
99+
++counter[c - 'A'];
100+
maxCnt = max(maxCnt, counter[c - 'A']);
101+
if (i - j + 1 > maxCnt + k)
102+
{
103+
--counter[s[j] - 'A'];
104+
++j;
105+
}
106+
++i;
107+
}
108+
return i - j;
109+
}
110+
};
111+
```
52112
113+
### **Go**
114+
115+
```go
116+
func characterReplacement(s string, k int) int {
117+
counter := make([]int, 26)
118+
j, maxCnt := 0, 0
119+
for i := range s {
120+
c := s[i] - 'A'
121+
counter[c]++
122+
if maxCnt < counter[c] {
123+
maxCnt = counter[c]
124+
}
125+
if i-j+1 > maxCnt+k {
126+
counter[s[j]-'A']--
127+
j++
128+
}
129+
}
130+
return len(s) - j
131+
}
53132
```
54133

55134
### **...**

‎solution/0400-0499/0424.Longest Repeating Character Replacement/README_EN.md

+73-1
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,85 @@ The substring &quot;BBBB&quot; has the longest repeating letters, which is 4.
4242
### **Python3**
4343

4444
```python
45-
45+
class Solution:
46+
def characterReplacement(self, s: str, k: int) -> int:
47+
counter = [0] * 26
48+
i = j = maxCnt = 0
49+
while i < len(s):
50+
counter[ord(s[i]) - ord('A')] += 1
51+
maxCnt = max(maxCnt, counter[ord(s[i]) - ord('A')])
52+
if i - j + 1 > maxCnt + k:
53+
counter[ord(s[j]) - ord('A')] -= 1
54+
j += 1
55+
i += 1
56+
return i - j
4657
```
4758

4859
### **Java**
4960

5061
```java
62+
class Solution {
63+
public int characterReplacement(String s, int k) {
64+
int[] counter = new int[26];
65+
int i = 0;
66+
int j = 0;
67+
for (int maxCnt = 0; i < s.length(); ++i) {
68+
char c = s.charAt(i);
69+
++counter[c - 'A'];
70+
maxCnt = Math.max(maxCnt, counter[c - 'A']);
71+
if (i - j + 1 - maxCnt > k) {
72+
--counter[s.charAt(j) - 'A'];
73+
++j;
74+
}
75+
}
76+
return i - j;
77+
}
78+
}
79+
```
80+
81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
int characterReplacement(string s, int k) {
87+
vector<int> counter(26);
88+
int i = 0, j = 0, maxCnt = 0;
89+
for (char& c : s)
90+
{
91+
++counter[c - 'A'];
92+
maxCnt = max(maxCnt, counter[c - 'A']);
93+
if (i - j + 1 > maxCnt + k)
94+
{
95+
--counter[s[j] - 'A'];
96+
++j;
97+
}
98+
++i;
99+
}
100+
return i - j;
101+
}
102+
};
103+
```
51104
105+
### **Go**
106+
107+
```go
108+
func characterReplacement(s string, k int) int {
109+
counter := make([]int, 26)
110+
j, maxCnt := 0, 0
111+
for i := range s {
112+
c := s[i] - 'A'
113+
counter[c]++
114+
if maxCnt < counter[c] {
115+
maxCnt = counter[c]
116+
}
117+
if i-j+1 > maxCnt+k {
118+
counter[s[j]-'A']--
119+
j++
120+
}
121+
}
122+
return len(s) - j
123+
}
52124
```
53125

54126
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int characterReplacement(string s, int k) {
4+
vector<int> counter(26);
5+
int i = 0, j = 0, maxCnt = 0;
6+
for (char& c : s)
7+
{
8+
++counter[c - 'A'];
9+
maxCnt = max(maxCnt, counter[c - 'A']);
10+
if (i - j + 1 > maxCnt + k)
11+
{
12+
--counter[s[j] - 'A'];
13+
++j;
14+
}
15+
++i;
16+
}
17+
return i - j;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func characterReplacement(s string, k int) int {
2+
counter := make([]int, 26)
3+
j, maxCnt := 0, 0
4+
for i := range s {
5+
c := s[i] - 'A'
6+
counter[c]++
7+
if maxCnt < counter[c] {
8+
maxCnt = counter[c]
9+
}
10+
if i-j+1 > maxCnt+k {
11+
counter[s[j]-'A']--
12+
j++
13+
}
14+
}
15+
return len(s) - j
16+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
class Solution {
22
public int characterReplacement(String s, int k) {
3-
char[] cs = s.toCharArray();
4-
int[] map = new int[26];
5-
int res = 0;
6-
int max = 0;
7-
for (int l = 0, r = 0; r < cs.length; ) {
8-
max = Math.max(max, ++map[cs[r++] - 'A']);
9-
while (r - l - max > k) {
10-
--map[cs[l++] - 'A'];
3+
int[] counter = new int[26];
4+
int i = 0;
5+
int j = 0;
6+
for (int maxCnt = 0; i < s.length(); ++i) {
7+
char c = s.charAt(i);
8+
++counter[c - 'A'];
9+
maxCnt = Math.max(maxCnt, counter[c - 'A']);
10+
if (i - j + 1 - maxCnt > k) {
11+
--counter[s.charAt(j) - 'A'];
12+
++j;
1113
}
12-
res = Math.max(res, r - l);
1314
}
14-
return res;
15+
return i - j;
1516
}
16-
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def characterReplacement(self, s: str, k: int) -> int:
3+
counter = [0] * 26
4+
i = j = maxCnt = 0
5+
while i < len(s):
6+
counter[ord(s[i]) - ord('A')] += 1
7+
maxCnt = max(maxCnt, counter[ord(s[i]) - ord('A')])
8+
if i - j + 1 > maxCnt + k:
9+
counter[ord(s[j]) - ord('A')] -= 1
10+
j += 1
11+
i += 1
12+
return i - j

0 commit comments

Comments
 (0)