第九十天:力扣830题,较大分组的位置
地址:leetcode-cn.com/problems/po…
思路:双指针,一次遍历;但是要注意 'aaa' 这种特殊情况
var largeGroupPositions = function(s) {
let res = [];
const n = s.length;
for(let l1 = 0,l2 = 1;l2 < n; l2++)
{
if(s[l2] !== s[l1] || l2 === n - 1)
{
if(l2 - l1 >= 3)
{
s[l2] === s[l1] ? res.push([l1, l2]) : res.push([l1, l2 - 1]);
}
else if(l2 - l1 === 2 && s[l2] === s[l1])
{
res.push([l1, l2]);
}
l1 = l2;
}
}
return res;
};
执行用时:96 ms, 在所有 JavaScript 提交中击败了82.86%的用户
内存消耗:41.1 MB, 在所有 JavaScript 提交中击败了21.36%的用户