Skip to content

Commit a4585d2

Browse files
committed
1047. 删除字符串中的所有相邻重复项 (二刷)
1 parent 5e45452 commit a4585d2

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @lc app=leetcode.cn id=1047 lang=javascript
3+
*
4+
* [1047] 删除字符串中的所有相邻重复项
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {string} s
10+
* @return {string}
11+
*/
12+
var removeDuplicates = function (s) {
13+
const stack = [];
14+
15+
for (let i = 0; i < s.length; i++) {
16+
const top = stack[stack.length - 1];
17+
18+
if (top === s[i]) {
19+
stack.pop();
20+
} else {
21+
stack.push(s[i]);
22+
}
23+
}
24+
25+
return stack.join('')
26+
};
27+
// @lc code=end
28+

0 commit comments

Comments
 (0)