File tree 5 files changed +87
-0
lines changed
algorithms/PositionsOfLargeGroups
5 files changed +87
-0
lines changed Original file line number Diff line number Diff line change @@ -131,6 +131,7 @@ All solutions will be accepted!
131
131
| 415| [ Add Strings] ( https://leetcode-cn.com/problems/add-strings/description/ ) | [ java/py/js] ( ./algorithms/AddStrings ) | Easy|
132
132
| 67| [ Add Binary] ( https://leetcode-cn.com/problems/add-binary/description/ ) | [ java/py/js] ( ./algorithms/AddBinary ) | Easy|
133
133
| 504| [ Base 7] ( https://leetcode-cn.com/problems/base-7/description/ ) | [ java/py/js] ( ./algorithms/Base7 ) | Easy|
134
+ | 830| [ Positions Of Large Groups] ( https://leetcode-cn.com/problems/positions-of-large-groups/description/ ) | [ java/py/js] ( ./algorithms/PositionsOfLargeGroups ) | Easy|
134
135
135
136
# Database
136
137
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Positions Of Large Groups
2
+ This problem is easy to solve
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <List <Integer >> largeGroupPositions (String S ) {
3
+ Character lastCharacter = null ;
4
+ List <List <Integer >> res = new ArrayList <List <Integer >>();
5
+ int start = 0 ,
6
+ end = 0 ;
7
+
8
+ for (int i = 0 ; i < S .length (); i ++) {
9
+ if (lastCharacter == null || lastCharacter .equals (S .charAt (i ))) {
10
+ end = i ;
11
+ } else {
12
+ if (end - start + 1 >= 3 ) {
13
+ List <Integer > group = new ArrayList <Integer >();
14
+ group .add (start );
15
+ group .add (end );
16
+ res .add (group );
17
+ }
18
+ end = start = i ;
19
+ }
20
+ lastCharacter = S .charAt (i );
21
+ }
22
+
23
+ if (end - start + 1 >= 3 ) {
24
+ List <Integer > group = new ArrayList <Integer >();
25
+ group .add (start );
26
+ group .add (end );
27
+ res .add (group );
28
+ }
29
+ return res ;
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } S
3
+ * @return {number[][] }
4
+ */
5
+ var largeGroupPositions = function ( S ) {
6
+ let lastCharacter = null ,
7
+ start = 0 ,
8
+ end = 0 ,
9
+ res = [ ]
10
+
11
+ for ( let i = 0 ; i < S . length ; i ++ ) {
12
+ if ( lastCharacter === null || lastCharacter === S [ i ] ) {
13
+ end = i
14
+ } else {
15
+ if ( end - start + 1 >= 3 ) {
16
+ res . push ( [ start , end ] )
17
+ }
18
+ end = start = i
19
+ }
20
+ lastCharacter = S [ i ]
21
+ }
22
+
23
+ if ( end - start + 1 >= 3 ) {
24
+ res . push ( [ start , end ] )
25
+ }
26
+
27
+ return res
28
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def largeGroupPositions (self , S ):
3
+ """
4
+ :type S: str
5
+ :rtype: List[List[int]]
6
+ """
7
+ last_character = None
8
+ start = 0
9
+ end = 0
10
+ res = []
11
+
12
+ for i in range (len (S )):
13
+ if last_character == None or last_character == S [i ]:
14
+ end = i
15
+ else :
16
+ count = end - start + 1
17
+ if count >= 3 :
18
+ res .append ([start , end ])
19
+ end = start = i
20
+ last_character = S [i ]
21
+
22
+ if end - start + 1 >= 3 :
23
+ res .append ([start , end ])
24
+
25
+ return res
You can’t perform that action at this time.
0 commit comments