public class Solution {
public IList<string> GenerateParenthesis(int n)
{
List<string> list = new List<string>();
backtrack(list, "", , , n);
return list;
} private void backtrack(List<String> list, String str, int open, int close, int max)
{ if (str.Length == max * )
{
list.Add(str);
return;
} if (open < max)
{
backtrack(list, str + "(", open + , close, max);
}
if (close < open)
{
backtrack(list, str + ")", open, close + , max);
}
}
}

https://leetcode.com/problems/generate-parentheses/#/description

重新实现了一遍,也是使用回溯法。先使用python来做,本地测试正常,但是在leetcode上提交却WA,于是把python代码改写为C#,则AC了。

因此判断是OJ对python的代码判定有些问题,暂时不知道是什么原因。之前做题时,用CPP的代码也会出现这种问题,换成Java或C#则能正常AC。

现在把C#的正常AC的代码先贴出来:

 public class Solution
{
private List<string> list = new List<string>(); private void BackTrack(int L, int R, int n, string str)
{
if (L >= R && L < n)
{
BackTrack(L + , R, n, str + '(');
if (L > R)
{
BackTrack(L, R + , n, str + ')');
}
} else if( L> R && L== n)
{
BackTrack(L, R + , n, str + ')');
}
else if (L == n && R == n)
{
list.Add(str);
}
} public IList<string> GenerateParenthesis(int n)
{
string str = "(";
int L = ;
int R = ;
BackTrack(L, R, n, str);
return list;
}
}

下面是python的实现,这个和上面的C#的使用的是同样的逻辑,但是WA。

 class Solution:
y=list()#结果集合
def generateParenthesis(self, n: 'int') -> 'List[str]':
string = '('
L = 1#左括号数量
R = 0#右括号数量
self.BackTrack(L,R,n,string)
return self.y def BackTrack(self,L,R,n,string):
#print('L='+str(L)+'|R='+str(R)+'|string='+string)
if L>=R and L<n:
self.BackTrack(L+1,R,n,string+'(')
if L > R:
self.BackTrack(L,R+1,n,string+')')
elif L>R and L==n:
self.BackTrack(L,R+1,n,string+')')
elif L == R == n:
self.y.append(string)
else:
return
return

错误信息如下:

经过实验,是全局变量self.y中的数据造成的影响,于是把y改为局部变量,就可以正常提交了:

 class Solution:
def generateParenthesis(self, n: 'int') -> 'List[str]':
y=list()
string = '('
L = #左括号数量
R = #右括号数量
self.BackTrack(L,R,n,string,y)
return y def BackTrack(self,L,R,n,string,y):
if L == R == n:
y.append(string)
elif L == n and R < n:
self.BackTrack(L,R+,n,string + ')',y)
elif L < n and R < n:
self.BackTrack(L+,R,n,string + '(',y)
if L > R:
self.BackTrack(L,R+,n,string + ')',y)
return

leetcode22的更多相关文章

  1. LeetCode22 Generate Parentheses

    题意: iven n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  2. [Swift]LeetCode22. 括号生成 | Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  3. LeetCode22.括号生成 JavaScript

    给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...

  4. Leetcode22. Generate Parentheses(生成有效的括号组合)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74937307冷血之心的博客) 题目如下:

  5. 【leetcode-22】括号生成

    给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...

  6. dfs · leetcode-22.产生括号组?

    题面 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  7. Leetcode22.Generate Parentheses括号生成

    给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...

  8. LeetCode22 生成所有括号对

    本文始发于个人公众号:TechFlow,原创不易,求个关注 链接 Generate Parentheses 难度 Medium 描述 Given n pairs of parentheses, wri ...

  9. LeetCode 22. 括号生成(Generate Parentheses)

    22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...

随机推荐

  1. 2019.4 sigfox EMC

    干扰源: ------- Leakage Sensor 有-30dB的谐波 1在NPN 基级加100pF 电容 从VCC到GND,一级级整改.

  2. SAZ文件的打开与保存

    保存 在Fiddler上,使用菜单“文件”>“保存” 可以将当前的HTTP请求信息保存成SAZ文件.   读取 在Fiddler上,使用菜单“文件”>“加载档案”可以读取SAZ文件,加载其 ...

  3. DAY 04运算符与流程控制

    输入输出补充: python2与python3的输入输出不同 python2中有两种用户 输入方式,一种是raw_input,和input raw_input与python3的input是相同的 而p ...

  4. python day28--json,pickle,hashlib,logging

    一.json格式的限制 1.json格式的key必须是字符串数据类型,如果是数字dumps后会被转为字符串. # dic = {1:2,3:4} # str_dic = json.dumps(dic) ...

  5. 浅入浅出JDBC————1分钟了解JDBC

    一.了解基本的几个jdbc需要的类 1.1DriverManager类 DriverManager类是一个jdbc的驱动服务类.通常使用该类获得一个Connection对象,得到一个数据库的链接. 1 ...

  6. dp——完全背包(方案数)

    Problem J. icebound 的商店Time limit: 1000msMemory limit: 65536KBDescriptionicebound 在得到神殿的宝藏之后,开了一家神秘的 ...

  7. configparse模块和hashlib模块

    # import configparser # # config = configparser.ConfigParser() #config = {} # config['DEFAULT'] = {' ...

  8. javascript继承的6种方法

    1原型式继承 简介:对类式继承的封装,过渡对象相当于子类. function inheritObject(o) { //声明过渡函数对象 function F() {} //过渡对象的原型继承父类 F ...

  9. js barcode 打印

    新建 html <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset=&quo ...

  10. spring-boot入门总结

    1.org.springframework.web.bind.annotation不存在 错误的pom.xml <dependency> <groupId>org.spring ...