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. OpenStack的HA方案

    一.HA服务分类 HA将服务分为两类: 有状态服务:后续对服务的请求依赖之前对服务的请求,OpenStack中有状态的服务包括MySQL数据库和AMQP消息队列.对于有状态类服务的HA,如neutro ...

  2. web.xml配置以及一些详解

    web.xml的根元素定义如下所示(代表当前使用哪个模版): <?xml version="1.0" encoding="UTF-8"?> < ...

  3. guava-retrying 源码解析(导入项目)

    1.从github上下载guava-retry源码 git clone git://github.com/rholder/guava-retrying.git 2.导入idea,使用gradle记得勾 ...

  4. vm虚拟机下ubuntu连接上ssr

    第一步: 第二步: 第三步: 第四步: 完成!

  5. 项目配置linux上, 配置文件访问不到

    /** * 读入TXT文件 */public List<String> readFile(String pathName) { List<String> list = new ...

  6. ASP.NET之使用Ajax实现页面异步刷新(无需刷新整个页面)

    目前在使用ASP.NET技术做毕业设计,但是关于网页中的各种配置我到现在还不是很清楚,正在努力进化... 一般情况下,新建网页页面的话,应该为.aspx后缀的文件,建好之后对应一个同名的.cs文件,属 ...

  7. 【转载】 Deepmind星际争霸2平台使用第一轮-完成采矿

    原文地址: https://blog.csdn.net/woaipichuli/article/details/78645999 ----------------------------------- ...

  8. spark on yarn运行产生jar包冲突问题

    1.1 问题描述 Spark Streaming程序解析protobuf序列化的数据时,--jars 来添加依赖的protobuf-java-3.0.0.jar包,使用local模式程序正常,使用ya ...

  9. Redis配置文件 redis.conf 解读(一)

    # Redis configuration file example# redis配置文件模板# Note on units: when memory size is needed, it is po ...

  10. css自定义checkbox和radio样式

    很常见的问题,也有许多人写过类似的文章,自己写来记录下 css代码如下: #myCheck + label,.myRadio + label{ width:16px; height:16px; bor ...