Skip to content

Commit 6af8ef1

Browse files
committed
feat: add solutions to lc problem: No.0830
No.0830.Positions of Large Groups
1 parent 8bbb79d commit 6af8ef1

File tree

8 files changed

+223
-9
lines changed

8 files changed

+223
-9
lines changed

solution/0800-0899/0830.Positions of Large Groups/README.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,100 @@
5959

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

62+
**方法一:双指针**
63+
64+
我们用双指针 $i$ 和 $j$ 找到每个分组的起始位置和终止位置,然后判断分组长度是否大于等于 $3$,若是则将其加入结果数组。
65+
66+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。
67+
6268
<!-- tabs:start -->
6369

6470
### **Python3**
6571

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

6874
```python
69-
75+
class Solution:
76+
def largeGroupPositions(self, s: str) -> List[List[int]]:
77+
i, n = 0, len(s)
78+
ans = []
79+
while i < n:
80+
j = i
81+
while j < n and s[j] == s[i]:
82+
j += 1
83+
if j - i >= 3:
84+
ans.append([i, j - 1])
85+
i = j
86+
return ans
7087
```
7188

7289
### **Java**
7390

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

7693
```java
94+
class Solution {
95+
public List<List<Integer>> largeGroupPositions(String s) {
96+
int n = s.length();
97+
int i = 0;
98+
List<List<Integer>> ans = new ArrayList<>();
99+
while (i < n) {
100+
int j = i;
101+
while (j < n && s.charAt(j) == s.charAt(i)) {
102+
++j;
103+
}
104+
if (j - i >= 3) {
105+
ans.add(Arrays.asList(i, j - 1));
106+
}
107+
i = j;
108+
}
109+
return ans;
110+
}
111+
}
112+
```
113+
114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
vector<vector<int>> largeGroupPositions(string s) {
120+
int n = s.size();
121+
int i = 0;
122+
vector<vector<int>> ans;
123+
while (i < n) {
124+
int j = i;
125+
while (j < n && s[j] == s[i]) {
126+
++j;
127+
}
128+
if (j - i >= 3) {
129+
ans.push_back({i, j - 1});
130+
}
131+
i = j;
132+
}
133+
return ans;
134+
}
135+
};
136+
```
77137
138+
### **Go**
139+
140+
```go
141+
func largeGroupPositions(s string) [][]int {
142+
i, n := 0, len(s)
143+
ans := [][]int{}
144+
for i < n {
145+
j := i
146+
for j < n && s[j] == s[i] {
147+
j++
148+
}
149+
if j-i >= 3 {
150+
ans = append(ans, []int{i, j - 1})
151+
}
152+
i = j
153+
}
154+
return ans
155+
}
78156
```
79157

80158
### **...**

solution/0800-0899/0830.Positions of Large Groups/README_EN.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,85 @@
5454
### **Python3**
5555

5656
```python
57-
57+
class Solution:
58+
def largeGroupPositions(self, s: str) -> List[List[int]]:
59+
i, n = 0, len(s)
60+
ans = []
61+
while i < n:
62+
j = i
63+
while j < n and s[j] == s[i]:
64+
j += 1
65+
if j - i >= 3:
66+
ans.append([i, j - 1])
67+
i = j
68+
return ans
5869
```
5970

6071
### **Java**
6172

6273
```java
74+
class Solution {
75+
public List<List<Integer>> largeGroupPositions(String s) {
76+
int n = s.length();
77+
int i = 0;
78+
List<List<Integer>> ans = new ArrayList<>();
79+
while (i < n) {
80+
int j = i;
81+
while (j < n && s.charAt(j) == s.charAt(i)) {
82+
++j;
83+
}
84+
if (j - i >= 3) {
85+
ans.add(Arrays.asList(i, j - 1));
86+
}
87+
i = j;
88+
}
89+
return ans;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
vector<vector<int>> largeGroupPositions(string s) {
100+
int n = s.size();
101+
int i = 0;
102+
vector<vector<int>> ans;
103+
while (i < n) {
104+
int j = i;
105+
while (j < n && s[j] == s[i]) {
106+
++j;
107+
}
108+
if (j - i >= 3) {
109+
ans.push_back({i, j - 1});
110+
}
111+
i = j;
112+
}
113+
return ans;
114+
}
115+
};
116+
```
63117
118+
### **Go**
119+
120+
```go
121+
func largeGroupPositions(s string) [][]int {
122+
i, n := 0, len(s)
123+
ans := [][]int{}
124+
for i < n {
125+
j := i
126+
for j < n && s[j] == s[i] {
127+
j++
128+
}
129+
if j-i >= 3 {
130+
ans = append(ans, []int{i, j - 1})
131+
}
132+
i = j
133+
}
134+
return ans
135+
}
64136
```
65137

66138
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> largeGroupPositions(string s) {
4+
int n = s.size();
5+
int i = 0;
6+
vector<vector<int>> ans;
7+
while (i < n) {
8+
int j = i;
9+
while (j < n && s[j] == s[i]) {
10+
++j;
11+
}
12+
if (j - i >= 3) {
13+
ans.push_back({i, j - 1});
14+
}
15+
i = j;
16+
}
17+
return ans;
18+
}
19+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func largeGroupPositions(s string) [][]int {
2+
i, n := 0, len(s)
3+
ans := [][]int{}
4+
for i < n {
5+
j := i
6+
for j < n && s[j] == s[i] {
7+
j++
8+
}
9+
if j-i >= 3 {
10+
ans = append(ans, []int{i, j - 1})
11+
}
12+
i = j
13+
}
14+
return ans
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public List<List<Integer>> largeGroupPositions(String s) {
3+
int n = s.length();
4+
int i = 0;
5+
List<List<Integer>> ans = new ArrayList<>();
6+
while (i < n) {
7+
int j = i;
8+
while (j < n && s.charAt(j) == s.charAt(i)) {
9+
++j;
10+
}
11+
if (j - i >= 3) {
12+
ans.add(Arrays.asList(i, j - 1));
13+
}
14+
i = j;
15+
}
16+
return ans;
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def largeGroupPositions(self, s: str) -> List[List[int]]:
3+
i, n = 0, len(s)
4+
ans = []
5+
while i < n:
6+
j = i
7+
while j < n and s[j] == s[i]:
8+
j += 1
9+
if j - i >= 3:
10+
ans.append([i, j - 1])
11+
i = j
12+
return ans

solution/0900-0999/0901.Online Stock Span/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ S.next(85) 被调用并返回 6。
5353

5454
这实际上是经典的单调栈模型,找出左侧第一个比当前元素大的元素。
5555

56-
我们维护一个从栈底到栈顶单调递减的栈,栈中每个元素存放的是 `(price, cnt)` 数据对,其中 `price` 表示价格,`cnt` 表示当前价格的跨度。
56+
我们维护一个从栈底到栈顶价格单调递减的栈,栈中每个元素存放的是 `(price, cnt)` 数据对,其中 `price` 表示价格,`cnt` 表示当前价格的跨度。
5757

5858
出现价格 `price` 时,我们将其与栈顶元素进行比较,如果栈顶元素的价格小于等于 `price`,则将当日价格的跨度 `cnt` 加上栈顶元素的跨度,然后将栈顶元素出栈,直到栈顶元素的价格大于 `price`,或者栈为空为止。
5959

@@ -122,15 +122,15 @@ public:
122122
StockSpanner() {
123123

124124
}
125-
125+
126126
int next(int price) {
127-
int ans = 1;
127+
int cnt = 1;
128128
while (!stk.empty() && stk.top().first <= price) {
129-
ans += stk.top().second;
129+
cnt += stk.top().second;
130130
stk.pop();
131131
}
132-
stk.push({price, ans});
133-
return ans;
132+
stk.push({price, cnt});
133+
return cnt;
134134
}
135135

136136
private:

solution/0900-0999/0901.Online Stock Span/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public:
107107
StockSpanner() {
108108

109109
}
110-
110+
111111
int next(int price) {
112112
int cnt = 1;
113113
while (!stk.empty() && stk.top().first <= price) {

0 commit comments

Comments
 (0)