Leetcode_22
来源:互联网 发布:电子软件合同范本 编辑:程序博客网 时间:2024/05/16 12:52
using System;using System.Collections.Generic;namespace Leetcode_22{public class Solution{public IList<string> GenerateParenthesis(int n){IList<string> list = new List<string>();backtrack(list, n, "");return list;}private void backtrack(IList<string> list, int n, string result){if (is_a_solution(result, n)){process_solution(list, result);}else {if (result.Length == 2 * n)return;char[] c = new char[2] { '(', ')' };for (int i = 0; i < 2; i++){result += c[i];backtrack(list, n, result);result = unmake_move(result);}}}private string unmake_move(string result){string str = "";for (int i = 0; i < result.Length-1; i++){str += result[i];}return str;}private bool is_a_solution(string str, int n){if (str.Length == 2*n){return query(str);}else {return false;}}private bool query(string str){Stack<char> s1 = new Stack<char>();int count = 0;while (count < str.Length){if (count == 0 && str[count] == ')'){return false;}else {if (str[count] == ')'){if (s1.Count == 0)return false;else {char c = s1.Peek();if (c != '('){return false;}else {s1.Pop();}}}else {s1.Push(str[count]);}count++;}}if (s1.Count == 0)return true;elsereturn false;}private void process_solution(IList<string> list, string result){list.Add(result);}}}
0 0
- Leetcode_22
- LeetCode_22---Generate Parentheses
- LeetCode_22 Generate Parentheses
- leetcode_22. Generate Parentheses 生成括号,给n对括号,有多少种括号生成方式
- Embitz 使用ARM GCC Compiler(generic) 兼容性问题<signal handler called>
- 一元二次方程
- Android之序列化
- 运算符
- 二分法查找法
- Leetcode_22
- 全排列
- 相互转换 时间类
- 手把手教你做音乐播放器(四)播放服务的搭建
- Windows下的程序放到linux下面不能执行的问题
- Docker镜像的创建、存出、载入
- 下载Solr5
- 图像处理算法的性能评估
- 使用mageui部署游戏微端(ClickOnce)(一)