File tree Expand file tree Collapse file tree 8 files changed +223
-9
lines changed
0800-0899/0830.Positions of Large Groups
0900-0999/0901.Online Stock Span Expand file tree Collapse file tree 8 files changed +223
-9
lines changed Original file line number Diff line number Diff line change 59
59
60
60
<!-- 这里可写通用的实现逻辑 -->
61
61
62
+ ** 方法一:双指针**
63
+
64
+ 我们用双指针 $i$ 和 $j$ 找到每个分组的起始位置和终止位置,然后判断分组长度是否大于等于 $3$,若是则将其加入结果数组。
65
+
66
+ 时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。
67
+
62
68
<!-- tabs:start -->
63
69
64
70
### ** Python3**
65
71
66
72
<!-- 这里可写当前语言的特殊实现逻辑 -->
67
73
68
74
``` 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
70
87
```
71
88
72
89
### ** Java**
73
90
74
91
<!-- 这里可写当前语言的特殊实现逻辑 -->
75
92
76
93
``` 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
+ ```
77
137
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
+ }
78
156
```
79
157
80
158
### ** ...**
Original file line number Diff line number Diff line change 54
54
### ** Python3**
55
55
56
56
``` 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
58
69
```
59
70
60
71
### ** Java**
61
72
62
73
``` 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
+ ```
63
117
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
+ }
64
136
```
65
137
66
138
### ** ...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change @@ -53,7 +53,7 @@ S.next(85) 被调用并返回 6。
53
53
54
54
这实际上是经典的单调栈模型,找出左侧第一个比当前元素大的元素。
55
55
56
- 我们维护一个从栈底到栈顶单调递减的栈 ,栈中每个元素存放的是 ` (price, cnt) ` 数据对,其中 ` price ` 表示价格,` cnt ` 表示当前价格的跨度。
56
+ 我们维护一个从栈底到栈顶价格单调递减的栈 ,栈中每个元素存放的是 ` (price, cnt) ` 数据对,其中 ` price ` 表示价格,` cnt ` 表示当前价格的跨度。
57
57
58
58
出现价格 ` price ` 时,我们将其与栈顶元素进行比较,如果栈顶元素的价格小于等于 ` price ` ,则将当日价格的跨度 ` cnt ` 加上栈顶元素的跨度,然后将栈顶元素出栈,直到栈顶元素的价格大于 ` price ` ,或者栈为空为止。
59
59
@@ -122,15 +122,15 @@ public:
122
122
StockSpanner() {
123
123
124
124
}
125
-
125
+
126
126
int next (int price) {
127
- int ans = 1;
127
+ int cnt = 1;
128
128
while (!stk.empty() && stk.top().first <= price) {
129
- ans += stk.top().second;
129
+ cnt += stk.top().second;
130
130
stk.pop();
131
131
}
132
- stk.push({price, ans });
133
- return ans ;
132
+ stk.push({price, cnt });
133
+ return cnt ;
134
134
}
135
135
136
136
private:
Original file line number Diff line number Diff line change @@ -107,7 +107,7 @@ public:
107
107
StockSpanner() {
108
108
109
109
}
110
-
110
+
111
111
int next (int price) {
112
112
int cnt = 1;
113
113
while (!stk.empty() && stk.top().first <= price) {
You can’t perform that action at this time.
0 commit comments