Created
August 18, 2018 13:57
-
-
Save aire-con-gas/ee8ff3c2bbe385b981a8a0b51aa225c2 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/tafolux
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width"> | |
| <title>JS Bin</title> | |
| </head> | |
| <body> | |
| <script id="jsbin-javascript"> | |
| 'use strict'; | |
| function generateParenthesis(n) { | |
| var list = []; | |
| function backtrack(list, str, open, close, max) { | |
| if (str.length === max * 2) { | |
| console.log('>>>', str); | |
| list.push(str); | |
| return; | |
| } | |
| console.log('>> current', list, str, open, close, max); | |
| if (open < max) { | |
| console.log('>> open < max ', list, str + '(', open + 1, close, max); | |
| backtrack(list, str + '(', open + 1, close, max); | |
| } | |
| if (close < open) { | |
| console.log('>> close < open', list, str + ')', open, close + 1, max); | |
| backtrack(list, str + ')', open, close + 1, max); | |
| } | |
| }; | |
| backtrack(list, '', 0, 0, n); | |
| return list; | |
| } | |
| console.log('>>>>> generateParenthesis(2) ', generateParenthesis(2)); | |
| </script> | |
| <script id="jsbin-source-javascript" type="text/javascript">function generateParenthesis(n) { | |
| let list = []; | |
| function backtrack(list, str, open, close, max) { | |
| if (str.length === max * 2) { | |
| console.log('>>>', str); | |
| list.push(str); | |
| return; | |
| } | |
| console.log('>> current', list, str, open, close, max) | |
| if (open < max) { | |
| console.log('>> open < max ', list, str + '(', open + 1, close, max) | |
| backtrack(list, str + '(', open + 1, close, max); | |
| } | |
| if (close < open) { | |
| console.log('>> close < open', list, str + ')', open, close + 1, max) | |
| backtrack(list, str + ')', open, close + 1, max); | |
| } | |
| }; | |
| backtrack(list, '', 0, 0, n); | |
| return list; | |
| } | |
| console.log('>>>>> generateParenthesis(2) ', generateParenthesis(2));</script></body> | |
| </html> |
This file contains hidden or 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
| 'use strict'; | |
| function generateParenthesis(n) { | |
| var list = []; | |
| function backtrack(list, str, open, close, max) { | |
| if (str.length === max * 2) { | |
| console.log('>>>', str); | |
| list.push(str); | |
| return; | |
| } | |
| console.log('>> current', list, str, open, close, max); | |
| if (open < max) { | |
| console.log('>> open < max ', list, str + '(', open + 1, close, max); | |
| backtrack(list, str + '(', open + 1, close, max); | |
| } | |
| if (close < open) { | |
| console.log('>> close < open', list, str + ')', open, close + 1, max); | |
| backtrack(list, str + ')', open, close + 1, max); | |
| } | |
| }; | |
| backtrack(list, '', 0, 0, n); | |
| return list; | |
| } | |
| console.log('>>>>> generateParenthesis(2) ', generateParenthesis(2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment