File tree Expand file tree Collapse file tree 1 file changed +69
-0
lines changed Expand file tree Collapse file tree 1 file changed +69
-0
lines changed Original file line number Diff line number Diff line change
1
+ // leetcode
2
+ // 2021.04.08
3
+ // wrong answer
4
+
5
+ import java .util .ArrayList ;
6
+ import java .util .List ;
7
+
8
+
9
+ // 예외 케이스 발생, 다시 해볼것!
10
+ public class PositionsLargeGroups2 {
11
+
12
+ // public static void main(String[] args) {
13
+ // System.out.println(largeGroupPositions("abc").toString());
14
+ // }
15
+
16
+ public static List <List <Integer >> largeGroupPositions (String s ) {
17
+ List <List <Integer >> solution = new ArrayList <>();
18
+ List <Integer > position = new ArrayList <>();
19
+ StringBuffer str = new StringBuffer (s );
20
+ int cnt = 1 , max = 0 , idx = 0 ;
21
+ char tmp = str .charAt (0 );
22
+
23
+ for (int i = 1 ; i < str .length (); i ++) {
24
+ if (tmp == str .charAt (i )) {
25
+ cnt ++;
26
+ if (i == str .length () - 1 )
27
+ if (cnt > max ) {
28
+ max = cnt ;
29
+ idx = i ;
30
+ }
31
+ } else {
32
+ if (cnt > max ) {
33
+ max = cnt ;
34
+ idx = i ;
35
+ }
36
+ cnt = 1 ;
37
+ tmp = str .charAt (i );
38
+ }
39
+ }
40
+ if (max == 1 )
41
+ return solution ;
42
+ else {
43
+ position .add (idx - max );
44
+ position .add (idx - 1 );
45
+ solution .add (position );
46
+ }
47
+ return solution ;
48
+ }
49
+ }
50
+
51
+ // best solution_USE Two Pointer
52
+ /*
53
+ class Solution {
54
+ public List<List<Integer>> largeGroupPositions(String S) {
55
+ List<List<Integer>> ans = new ArrayList();
56
+ int i = 0, N = S.length(); // i is the start of each group
57
+ for (int j = 0; j < N; ++j) {
58
+ if (j == N-1 || S.charAt(j) != S.charAt(j+1)) {
59
+ // Here, [i, j] represents a group.
60
+ if (j-i+1 >= 3)
61
+ ans.add(Arrays.asList(new Integer[]{i, j}));
62
+ i = j + 1;
63
+ }
64
+ }
65
+
66
+ return ans;
67
+ }
68
+ }
69
+ */
You can’t perform that action at this time.
0 commit comments