File tree Expand file tree Collapse file tree 3 files changed +76
-0
lines changed
solution/0000-0099/0022.Generate Parentheses Expand file tree Collapse file tree 3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -225,6 +225,33 @@ var generateParenthesis = function (n) {
225225};
226226```
227227
228+ #### C#
229+
230+ ``` cs
231+ public class Solution {
232+ private List <string > ans = new List <string >();
233+ private int n ;
234+
235+ public List <string > GenerateParenthesis (int n ) {
236+ this .n = n ;
237+ Dfs (0 , 0 , " " );
238+ return ans ;
239+ }
240+
241+ private void Dfs (int l , int r , string t ) {
242+ if (l > n || r > n || l < r ) {
243+ return ;
244+ }
245+ if (l == n && r == n ) {
246+ ans .Add (t );
247+ return ;
248+ }
249+ Dfs (l + 1 , r , t + " (" );
250+ Dfs (l , r + 1 , t + " )" );
251+ }
252+ }
253+ ```
254+
228255#### PHP
229256
230257``` php
Original file line number Diff line number Diff line change @@ -220,6 +220,33 @@ var generateParenthesis = function (n) {
220220};
221221```
222222
223+ #### C#
224+
225+ ``` cs
226+ public class Solution {
227+ private List <string > ans = new List <string >();
228+ private int n ;
229+
230+ public List <string > GenerateParenthesis (int n ) {
231+ this .n = n ;
232+ Dfs (0 , 0 , " " );
233+ return ans ;
234+ }
235+
236+ private void Dfs (int l , int r , string t ) {
237+ if (l > n || r > n || l < r ) {
238+ return ;
239+ }
240+ if (l == n && r == n ) {
241+ ans .Add (t );
242+ return ;
243+ }
244+ Dfs (l + 1 , r , t + " (" );
245+ Dfs (l , r + 1 , t + " )" );
246+ }
247+ }
248+ ```
249+
223250#### PHP
224251
225252``` php
Original file line number Diff line number Diff line change 1+ public class Solution {
2+ private List < string > ans = new List < string > ( ) ;
3+ private int n ;
4+
5+ public List < string > GenerateParenthesis ( int n ) {
6+ this . n = n ;
7+ Dfs ( 0 , 0 , "" ) ;
8+ return ans ;
9+ }
10+
11+ private void Dfs ( int l , int r , string t ) {
12+ if ( l > n || r > n || l < r ) {
13+ return ;
14+ }
15+ if ( l == n && r == n ) {
16+ ans . Add ( t ) ;
17+ return ;
18+ }
19+ Dfs ( l + 1 , r , t + "(" ) ;
20+ Dfs ( l , r + 1 , t + ")" ) ;
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments