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. asp.net mvc模板布局

  2. Redis学习第六课:Redis ZSet类型及操作

    Sorted set是set的一个升级版本,它在set的基础上增加了一个顺序属性,这一属性在添加修改元素时候可以指定,每次指定后,zset会自动重新按新的值调整顺序.可以理解为有两列字段的数据表,一列 ...

  3. Python2和Python3安装注意事项

    1. 到官网 https://www.python.org/downloads/windows/ 下载 Windows x86-64 executable installer版本: 2. python ...

  4. I think I need a boat house

    I think I need a boat house. Fred Mapper is considering purchasing some land in Louisiana to build h ...

  5. CVE-2017-12615和CVE-2017-12616

    Tomcat代码执行漏洞分析测试 1. 漏洞花絮        2017年9月19日,Apache Tomcat官方确认并修复了两个高危漏洞,漏洞CVE编号:CVE-2017-12615和CVE-20 ...

  6. python 3.5 import theano ::hypot error

    # win10 , mingw(nuwen,g++ 6.3), python 3.5 , 描述: import theano 时生成动态的 mod.cpp ,然后编译库的时候报 ::hypot 未定义 ...

  7. 【SpringBoot】SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener

    =================6.SpringBoot拦截器实战和 Servlet3.0自定义Filter.Listener ============ 1.深入SpringBoot2.x过滤器Fi ...

  8. oracle-logminer

    LogMiner工具实际上是由两个新的PL/SQL内建包((DBMS_LOGMNR 和 DBMS_ LOGMNR_D)和四个V$动态性能视图(视图是在利用过程DBMS_LOGMNR.START_LOG ...

  9. windows server 修改远程桌面连接端口号

    1. [运行]输入 regedit 2.  在注册表编辑器中找到以下PortNamber键,改为要使用的远程端口,如10000. HKEY_LOCAL_MACHINE\SYSTEM\CurrentCo ...

  10. 自己写一个 Hash 表

    项目地址:  https://github.com/kelin-xycs/HashTableLib 为什么会想要自己写一个 Hash 表, 以前也想过 Hash 表 的 原理, 觉得很神奇, 不过最近 ...