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之Neutron分配VIP提供给两台虚拟机做高可用

    一. 简单介绍 在openstack私有云平台的应用场景中,涉及多台虚拟机实例进行高可用的绑定,这里我们需要在云平台中提供一个IP给高可用场景切换,这里介绍keepalived + allow_add ...

  2. sublime text 使用小技巧

    sublime下载各个版本 官网 插件官方网站地址 https://packagecontrol.io/ 一.安装设置字体及字体大小 1.点菜单“Preferences--->Setting - ...

  3. 剑指Offer 34. 第一个只出现一次的字符 (字符串)

    题目描述 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写). 题目地址 https:// ...

  4. 转-软件测试人员在工作中如何运用Linux

    从事过软件测试的小伙们就会明白会使用Linux是多么重要的一件事,工作时需要用到,面试时会被问到,简历中需要写到. 对于软件测试人员来说,不需要你多么熟练使用Linux所有命令,也不需要你对Linux ...

  5. Dijkstra(迪杰斯特拉)模板

    直接将模板封装在结构体里面. struct Edge{ int from,to,dist; Edge(int u, int v,int d): from(u),to(v),dist(d){} }; s ...

  6. python 不同进程间通信

    from multiprocessing import Process,Queue import os def f (qq): qq.put([42,None,'hello']) #将列表传入队列qq ...

  7. python中表示False的一些内置对象

    By default, an object is considered true unless its class defines either a __bool__() method that re ...

  8. 使用outflux 导入influxdb 的数据到timescaledb

    influxdb 以及timescaledb 都是不错的时序数据库,timescaledb 团队提供了直接从influxdb 导入 环境准备 docker-compose 文件 version: &q ...

  9. centos7使用snmp

     一.安装snmp net-snmp :服务端 net-snmp-utils:客户端工具集   二.启动 systemctl start snmpd   三.修改配置文件(完整的配置文件如下) com ...

  10. Firebird 烂笔头(一)

    下载非安装版,将文件解压缩到D:\FireBird2.5下面.然后里面有.bat文件,选择自己适合的类型安装后,在服务里面会有一个firebirdserver开头的服务,右键启动. win+R,在命令 ...