classSolution { public: vector<vector<int>> largeGroupPositions(string s) { vector<vector<int>> ret; int n = s.size(); int num = 1; for (int i = 0; i < n; i++) { if (i == n - 1 || s[i] != s[i + 1]) { if (num >= 3) { ret.push_back({i - num + 1, i}); } num = 1; } else { num++; } } return ret; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public List<List<Integer>> largeGroupPositions(String s) { List<List<Integer>> ret = newArrayList<List<Integer>>(); intn= s.length(); intnum=1; for (inti=0; i < n; i++) { if (i == n - 1 || s.charAt(i) != s.charAt(i + 1)) { if (num >= 3) { ret.add(Arrays.asList(i - num + 1, i)); } num = 1; } else { num++; } } return ret; } }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
funclargeGroupPositions(s string) (ans [][]int) { cnt := 1 for i := range s { if i == len(s)-1 || s[i] != s[i+1] { if cnt >= 3 { ans = append(ans, []int{i - cnt + 1, i}) } cnt = 1 } else { cnt++ } } return }
int** largeGroupPositions(char* s, int* returnSize, int** returnColumnSizes) { *returnSize = 0; int n = strlen(s); int** ret = malloc(sizeof(int*) * (n / 3)); *returnColumnSizes = malloc(sizeof(int) * (n / 3)); int num = 1; for (int i = 0; i < n; i++) { if (i == n - 1 || s[i] != s[i + 1]) { if (num >= 3) { int* tmp = malloc(sizeof(int) * 2); tmp[0] = i - num + 1, tmp[1] = i; (*returnColumnSizes)[*returnSize] = 2; ret[(*returnSize)++] = tmp; } num = 1; } else { num++; } } return ret; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var largeGroupPositions = function(s) { const ret = []; const n = s.length; let num = 1; for (let i = 0; i < n; i++) { if (i === n - 1 || s[i] !== s[i + 1]) { if (num >= 3) { ret.push([i - num + 1, i]); } num = 1; } else { num++; } } return ret; };
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: deflargeGroupPositions(self, s: str) -> List[List[int]]: ret = list() n, num = len(s), 1
for i inrange(n): if i == n - 1or s[i] != s[i + 1]: if num >= 3: ret.append([i - num + 1, i]) num = 1 else: num += 1 return ret