leetcode22
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的更多相关文章
- LeetCode22 Generate Parentheses
题意: iven n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- [Swift]LeetCode22. 括号生成 | Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode22.括号生成 JavaScript
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...
- Leetcode22. Generate Parentheses(生成有效的括号组合)
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74937307冷血之心的博客) 题目如下:
- 【leetcode-22】括号生成
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...
- dfs · leetcode-22.产生括号组?
题面 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- Leetcode22.Generate Parentheses括号生成
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...
- LeetCode22 生成所有括号对
本文始发于个人公众号:TechFlow,原创不易,求个关注 链接 Generate Parentheses 难度 Medium 描述 Given n pairs of parentheses, wri ...
- LeetCode 22. 括号生成(Generate Parentheses)
22. 括号生成 22. Generate Parentheses 题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结 ...
随机推荐
- chmod语法
chmod命令详细用法 指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : ...
- idea设置代码提示不区分大小写
idea设置代码提示不区分大小写 intellij idea默认下的代码提示是区分大小写的,例如类方法名过长.类的名字过长等,完全通过手打的话较为繁琐,这里简单的设置下即可. 把 Case sensi ...
- Qthread的使用方法
1:重载 run()函数 2:将对象移到Qthread对象中 Movetothread 该方法必须通过信号 -槽来激发.
- Scrapy、Scrapy-redis组件
目录 Scrapy 一.安装 二.基本使用 1. 基本命令 2.项目结构以及爬虫应用简介 3. 小试牛刀 4. 选择器 5. 格式化处理 6.中间件 7. 自定制命令 8. 自定义扩展 9. 避免重复 ...
- day07 深浅拷贝
今日概要 深浅拷贝(重点) 文件操作 详细内容 直接赋值: 直接将对象的引用赋值给另一个对象 v1=1000 v2=v1 #v1 v2指向同一个内存地址 print(id(v1),id(v2))#相等 ...
- Restful Service 中 DateTime 在 url 中传递
在C# url 中一旦包特殊字符,请求可能就无法送达.可以使用如下方法,最为便捷. 请求端: beginTime.Value.ToString("yyyyMMddHHmmss") ...
- Mathematics for Computer Science (Eric Lehman / F Thomson Leighton / Albert R Meyer 著)
I Proofs1 What is a Proof?2 The Well Ordering Principle3 Logical Formulas4 Mathematical Data Types5 ...
- Python Django orm操作数据库笔记之QuerySet API
什么时候Django会将QuerySet转换为SQL去执行: 根据Django的数据库机制,对于QuerySet来说,当QuerySet被第一次构建,然后又调用他的filter方法,接着在对其进行切片 ...
- Linux scp命令详解
Linux scp命令 Linux scp命令用于Linux之间复制文件和目录. scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令. 语法: ...
- MySQL 5.7临时表空间
MySQL 5.7起,开始采用独立的临时表空间(和独立的undo表空间不是一回事哟),命名ibtmp1文件,初始化12M,且默认无上限. 选项 innodb_temp_data_file_path 可 ...