Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbf64a9f

Browse files
authored
Merge pull requestneetcode-gh#1274 from voski/patch-4
Create 0022-Generate-Parentheses.rb
2 parents75193fd +00b5baa commitbf64a9f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎ruby/22-Generate-Parentheses.rb‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# @param {Integer} n
2+
# @return {String[]}
3+
defgenerate_parenthesis(n)
4+
parenthesis=[]
5+
recurse(n,"",parenthesis,0,0)
6+
parenthesis
7+
end
8+
9+
defrecurse(n,pre,parenthesis,opens,closes)
10+
if(n *2 ==pre.length)
11+
parenthesis <<pre
12+
else
13+
ifcloses <opens
14+
recurse(n,pre +")",parenthesis,opens,closes +1)
15+
end
16+
17+
ifopens <n
18+
recurse(n,pre +"(",parenthesis,opens +1,closes)
19+
end
20+
end
21+
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp