Skip to content

Commit 7604db4

Browse files
committed
Solution to 1021
1 parent 0daadc2 commit 7604db4

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/unresolved/1021.remove-outermost-parentheses.jl renamed to src/problems/1021.remove-outermost-parentheses.jl

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 1021. Remove Outermost Parentheses
33
# id: problem1021
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: Indigo
5+
# date: 2022-03-19
66
# difficulty: Easy
77
# categories: Stack
88
# link: <https://leetcode.com/problems/remove-outermost-parentheses/description/>
@@ -73,5 +73,14 @@
7373
## @lc code=start
7474
using LeetCode
7575

76-
## add your code here:
76+
function remove_outer_parentheses(s::String)
77+
res = ""
78+
cnt = 0
79+
for ch in s
80+
flg = cnt < 0
81+
cnt += ch == '(' ? -1 : 1
82+
(flg && cnt < 0) && (res *= ch)
83+
end
84+
return res
85+
end
7786
## @lc code=end
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@testset "1019.next-greater-node-in-linked-list.jl" begin
22
@test next_larger_nodes(ListNode{Int}([1, 7, 5, 1, 9, 2, 5, 1])) ==
3-
[7, 9, 9, 9, 0, 5, 0, 0]
3+
[7, 9, 9, 9, 0, 5, 0, 0]
44
@test next_larger_nodes(ListNode{Int}([2, 7, 4, 3, 5])) == [7, 0, 5, 5, 0]
55
@test next_larger_nodes(ListNode{Int}([2, 1, 5])) == [5, 5, 0]
66
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@testset "1021.remove-outermost-parentheses.jl" begin
2+
@test remove_outer_parentheses("(()())(())(()(()))") == "()()()()(())"
3+
@test remove_outer_parentheses("(()())(())") == "()()()"
4+
@test remove_outer_parentheses("()()") == ""
5+
end

0 commit comments

Comments
 (0)