最后
除了简历做到位,面试题也必不可少,整理了些题目,前面有117道汇总的面试到的题目,后面包括了HTML、CSS、JS、ES6、vue、微信小程序、项目类问题、笔试编程类题等专题。
文章目录
- 剑指 Offer II 085. 生成匹配的括号|22. 括号生成|面试题 08.09. 括号:
- 样例 1:
- 样例 2:
- 提示:
- 分析
- 题解
- 原题传送门:https://leetcode-cn.com/problems/IDBivT/
- 原题传送门:https://leetcode-cn.com/problems/generate-parentheses/
- 原题传送门:https://leetcode.cn/problems/bracket-lcci/
剑指 Offer II 085. 生成匹配的括号|22. 括号生成|面试题 08.09. 括号:
正整数 n
代表生成括号的对数,请设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
样例 1:
输入:
n = 3
输出:
["((()))","(()())","(())()","()(())","()()()"]
样例 2:
输入:
n = 1
输出:
["()"]
提示:
- 1 <= n <= 8
分析
- 首先想到的是全排列排列方式,然后再从中挑选满足括号规则的序列。每个字符只可能是 ‘(’ 或者 ‘)’ 两种选择,所以全排列的数量是 22n 个。真正有效的括号序列远远少于这个量。
- 是否可以直接去穷举生成有效的括号序列呢?
- 首先要判断什么样的序列是有效的括号序列?
- 很显然,首先左右括号字符的数量应该相等。
- 右括号一定是匹配前面某个左括号。
- 所以只包含左右括号字符,并且从左往右看,右括号数始终小于等于左括号数,最终左右括号数相等的序列就是合理合法合规的括号序列了。
题解
java
class Solution {
public List<String> generateParenthesis(int n) {
List<String> ans = new ArrayList<>();
this.dfs(n, ans, new char[n \* 2], 0, 0);
return ans;
}
private void dfs(int n, List<String> ans, char[] cs, int left, int right) {
if (left == n && right == n) {
ans.add(new String(cs));
return;
}
if (left < n) {
cs[left + right] = '(';
dfs(n, ans, cs, left + 1, right);
}
if (right < left) {
cs[left + right] = ')';
dfs(n, ans, cs, left, right + 1);
}
}
}
c
#define MAX\_SIZE 1430
void dfs(int n, int \*returnSize, char \*\*ans, char \*cs, int left, int right) {
if (left == n && right == n) {
ans[(\*returnSize)] = calloc((n \* 2 + 1), sizeof(char));
strcpy(ans[(\*returnSize)], cs);
++(\*returnSize);
return;
}
if (left < n) {
cs[left + right] = '(';
dfs(n, returnSize, ans, cs, left + 1, right);
}
if (right < left) {
cs[left + right] = ')';
dfs(n, returnSize, ans, cs, left, right + 1);
}
}
/\*\*
\* Note: The returned array must be malloced, assume caller calls free().
\*/
char \*\*generateParenthesis(int n, int \*returnSize) {
\*returnSize = 0;
char \*\*ans = malloc(MAX_SIZE \* sizeof(char \*));
char \*cs = calloc((n \* 2 + 1), sizeof(char));
dfs(n, returnSize, ans, cs, 0, 0);
return ans;
}
c++
class Solution {
private:
void dfs(int n, vector<string> &ans, string &buf, int left, int right) {
if (left == n && right == n) {
ans.push\_back(buf);
return;
}
if (left < n) {
buf.push\_back('(');
dfs(n, ans, buf, left + 1, right);
buf.pop\_back();
}
if (right < left) {
buf.push\_back(')');
dfs(n, ans, buf, left, right + 1);
buf.pop\_back();
}
}
public:
vector<string> generateParenthesis(int n) {
vector<string> ans;
string buf;
dfs(n, ans, buf, 0, 0);
return ans;
}
};
python
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
ans = []
def dfs(s, left, right):
if left == n and right == n:
ans.append(''.join(s))
return
### 最后
![前15.PNG](https://img-blog.csdnimg.cn/img_convert/7b53f9c413a8b448d95849b9fa1968f4.webp?x-oss-process=image/format,png)
![前16.PNG](https://img-blog.csdnimg.cn/img_convert/598bf27ed408c915673776f4ae0d2ec7.webp?x-oss-process=image/format,png)
>由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容
>
>**[开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0)**
### 最后
[外链图片转存中...(img-2q012cRL-1715668071147)]
[外链图片转存中...(img-NSwDPfai-1715668071148)]
>由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容
>
>**[开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0)**