LeetCode算法入门- Generate Parentheses -day16
LeetCode算法入门- Generate Parentheses -day16
- 题目描述
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
[
“((()))”,
“(()())”,
“(())()”,
“()(())”,
“()()()”
]
题目分析:给定一个数字n,列出所有合法的序列,条件是左括号个数要大于或等于右括号个数:使用递归来实现
Java实现:
class Solution {public List<String> generateParenthesis(int n) {List<String> result = new ArrayList<>();helper(n,n,"",result);return result;}public static void helper(int left, int right, String tmp, List<String> result){if(left == 0 && right == 0){result.add(tmp);return ;}if(left < 0 || right < 0 || left > right){return ;}helper(left-1, right, tmp+"(",result);helper(left, right-1, tmp+")",result);}
}
LeetCode算法入门- Generate Parentheses -day16相关推荐
- LeetCode算法入门- Valid Parentheses -day11
LeetCode算法入门- Valid Parentheses -day11 题目描述: Given a string containing just the characters '(', ')', ...
- LeetCode算法入门- Longest Valid Parentheses -day12
LeetCode算法入门- Longest Valid Parentheses -day12 Given a string containing just the characters '(' and ...
- LeetCode算法入门- Implement strStr() -day22
LeetCode算法入门- Implement strStr() -day22 题目描述 Implement strStr(). Return the index of the first occur ...
- LeetCode算法入门- Remove Duplicates from Sorted Array -day21
LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...
- LeetCode算法入门- Remove Element -day20
LeetCode算法入门- Remove Element -day20 1. 题目描述 Given an array nums and a value val, remove all instance ...
- LeetCode算法入门- Search Insert Position -day19
LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the ...
- LeetCode算法入门- Multiply Strings -day18
LeetCode算法入门- Multiply Strings -day18 题目介绍 Given two non-negative integers num1 and num2 represented ...
- LeetCode算法入门- Remove Nth Node From End of List -day17
LeetCode算法入门- Remove Nth Node From End of List -day17 题目解释: Given a linked list, remove the n-th nod ...
- LeetCode算法入门- Merge Two Sorted Lists -day15
LeetCode算法入门- Merge Two Sorted Lists -day15 题目描述: Merge two sorted linked lists and return it as a n ...
最新文章
- 互联网大脑进化简史,华为云EI智能体加入-2018年7月新版
- 手机模拟位置实现精准位置营销效果分析
- 单片机原理及其应用——单片机控制8只发光二极管交替闪烁
- 《C++ Primer 5th》笔记(9 / 19):顺序容器
- C++学习之路 | PTA乙级—— 1043 输出PATest (20 分)(精简)
- 2个 string 日期比较
- 御用导航提示提醒_汽车导航,离线和在线哪个好用?两者的区别分析
- php钩子是啥意思,thinkphp钩子是什么意思
- nodejs接收前端formData数据
- linux配置maven环境变量
- 古代一些练外丹的资料
- 全球科学家公认的高效学习法——费曼学习法
- 每日一練06:javascript原生拖拽写的日食
- HEVC 、H.264与AVS2 视频压缩
- 【C++】虚函数与虚函数表
- ReXNet学习笔记 --- ReXNet: Diminishing Representational Bottleneck on Convolutional Neural Network
- linux 命令安装 wine,Wine安装使用(适用Linux Mint 19与Ubuntu 18.04下)
- 视频转图片序列 java_怎样才能把一段视频转换成序列图片文件
- 如何配置系统数据库服务器地址,如何配置系统数据库服务器地址
- Erro和Exception区别Throw和Throws的区别