From 3ff604055147700bd66ad8c83b43ee6f073e4265 Mon Sep 17 00:00:00 2001 From: lzxzz <1042183935@qq.com> Date: Sat, 29 Apr 2023 09:49:25 +0800 Subject: [PATCH] optimize --- ....删除字符串中的所有相邻重复项.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 694f1a92..486d198b 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -264,14 +264,15 @@ javaScript: ```js var removeDuplicates = function(s) { - const stack = []; - for(const x of s) { - let c = null; - if(stack.length && x === (c = stack.pop())) continue; - c && stack.push(c); - stack.push(x); + const result = [] + for(const i of s){ + if(i === result[result.length-1]){ + result.pop() + }else{ + result.push(i) + } } - return stack.join(""); + return result.join('') }; ```