forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kevin Naughton Jr
authored and
Kevin Naughton Jr
committed
May 29, 2018
1 parent
7920d94
commit 0adbc0d
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class GenerateParentheses { | ||
public List<String> generateParenthesis(int n) { | ||
List<String> result = new ArrayList<String>(); | ||
generateParenthesisRecursive(result, "", 0, 0, n); | ||
|
||
return result; | ||
} | ||
|
||
public void generateParenthesisRecursive(List<String> result, String current, int open, int close, int n) { | ||
if(current.length() == n * 2) { | ||
result.add(current); | ||
return; | ||
} | ||
|
||
if(open < n) { | ||
generateParenthesisRecursive(result, current + "(", open + 1, close, n); | ||
} | ||
|
||
if(close < open) { | ||
generateParenthesisRecursive(result, current + ")", open, close + 1, n); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class GenerateParentheses { | ||
public List<String> generateParenthesis(int n) { | ||
List<String> result = new ArrayList<String>(); | ||
generateParenthesisRecursive(result, "", 0, 0, n); | ||
|
||
return result; | ||
} | ||
|
||
public void generateParenthesisRecursive(List<String> result, String current, int open, int close, int n) { | ||
if(current.length() == n * 2) { | ||
result.add(current); | ||
return; | ||
} | ||
|
||
if(open < n) { | ||
generateParenthesisRecursive(result, current + "(", open + 1, close, n); | ||
} | ||
|
||
if(close < open) { | ||
generateParenthesisRecursive(result, current + ")", open, close + 1, n); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class GenerateParentheses { | ||
public List<String> generateParenthesis(int n) { | ||
List<String> result = new ArrayList<String>(); | ||
generateParenthesisRecursive(result, "", 0, 0, n); | ||
|
||
return result; | ||
} | ||
|
||
public void generateParenthesisRecursive(List<String> result, String current, int open, int close, int n) { | ||
if(current.length() == n * 2) { | ||
result.add(current); | ||
return; | ||
} | ||
|
||
if(open < n) { | ||
generateParenthesisRecursive(result, current + "(", open + 1, close, n); | ||
} | ||
|
||
if(close < open) { | ||
generateParenthesisRecursive(result, current + ")", open, close + 1, n); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class GenerateParentheses { | ||
public List<String> generateParenthesis(int n) { | ||
List<String> result = new ArrayList<String>(); | ||
generateParenthesisRecursive(result, "", 0, 0, n); | ||
|
||
return result; | ||
} | ||
|
||
public void generateParenthesisRecursive(List<String> result, String current, int open, int close, int n) { | ||
if(current.length() == n * 2) { | ||
result.add(current); | ||
return; | ||
} | ||
|
||
if(open < n) { | ||
generateParenthesisRecursive(result, current + "(", open + 1, close, n); | ||
} | ||
|
||
if(close < open) { | ||
generateParenthesisRecursive(result, current + ")", open, close + 1, n); | ||
} | ||
} | ||
} |