You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var removeDuplicates = function(S) {
let len = S.length
let stack = []
for(let v of S){
let aLen = stack.length
if(stack[aLen - 1] === v){
stack.pop()
}else{
stack.push(v)
}
}
return stack.join('')
};
function deleteNearChar(str){
function deleteChar(str){
let k = 0
while(k<str.length-1){
if(str.charAt(k)===str.charAt(k+1)){
let c = str.charAt(k)
while(c === str.charAt(k)){
let list = str.split('')
list.splice(k,1)
str = list.join('')
}
}else{
k++
}
}
return str
}
function isNearRepeat(str){
let list = str.split('')
return list.some((it,idx)=>list[idx]===list[idx+1])
}
while(isNearRepeat(str)){ str = deleteChar(str)}return str
}
console.log(deleteNearChar('abbaca'))
Activity
Makus577 commentedon Apr 24, 2020
7777sea commentedon Apr 24, 2020
fanefanes commentedon Apr 24, 2020
解题思路:
新建Stack ;
遍历字符串S => S[i];
if Stack.top() === S[i] => Stack.pop
else => Stack.push(S[i])
时间复杂度 O(n),空间复杂度 O(n)
thxiami commentedon Apr 24, 2020
@fanefanes
提个小建议: 变量
L可以去掉的, 因为L === Stack.lengthfanefanes commentedon Apr 24, 2020
遍历的时候尽量赋值常量, 要不然for每次循环会进行一次数组遍历查询 获取length
thxiami commentedon Apr 24, 2020
获取数组的
length应该不需要遍历数组才能获得吧, 这是它的一个属性, O(1)的时间复杂度wufeicris commentedon Apr 24, 2020
sisterAn commentedon Apr 24, 2020
解法:利用栈
解题思路: 遍历字符串,依次入栈,入栈时判断与栈头元素是否一致,如果一致,即这两个元素相同相邻,则需要将栈头元素出栈,并且当前元素也无需入栈
解题步骤: 遍历字符串,取出栈头字符,判断当前字符与栈头字符是否一致
遍历完成后,返回栈中字符串
代码实现:
时间复杂度:O(n)
空间复杂度:O(n)
leetcode
iconWave commentedon May 29, 2020
qianlongo commentedon Aug 22, 2020
递归加遍历
qianlongo commentedon Aug 22, 2020
利用栈
qianlongo commentedon Aug 22, 2020
递归 + 正则
lirunkai commentedon Sep 15, 2020
laibao101 commentedon Oct 4, 2020
dinjufen commentedon Oct 14, 2020
比较简单的题,用栈处理
AnranS commentedon Nov 19, 2020
lewisYe commentedon Apr 9, 2021
利用栈
JohnApache commentedon Apr 29, 2021
利用栈 存储以扫描过的字符,每次读取字符的时候都会跟栈顶元素进行比较,如果相同就推出栈顶元素,否则字符入栈
zhw2590582 commentedon Sep 29, 2021
AlexZhang11 commentedon May 31, 2024
function deleteNearChar(str){ function deleteChar(str){ let k = 0 while(k<str.length-1){ if(str.charAt(k)===str.charAt(k+1)){ let c = str.charAt(k) while(c === str.charAt(k)){ let list = str.split('') list.splice(k,1) str = list.join('') } }else{ k++ } } return str } function isNearRepeat(str){ let list = str.split('') return list.some((it,idx)=>list[idx]===list[idx+1]) } while(isNearRepeat(str)){ str = deleteChar(str) } return str } console.log(deleteNearChar('abbaca'))