leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees
https://www.cnblogs.com/grandyang/p/4299608.html
3由dp[1]*dp[1]、dp[0]*dp[2]、dp[2]*dp[0]相加而成
从2开始
class Solution {
public:
int numTrees(int n) {
vector<int> dp(n+);
dp[] = ;
dp[] = ;
for(int i = ;i <= n;i++){
for(int j = ;j < i;j++){
dp[i] += dp[j] * dp[i--j];
}
}
return dp[n];
}
};
也可以从1开始
class Solution {
public:
int numTrees(int n) {
vector<int> dp(n+,);
dp[] = ;
for(int i = ;i <= n;i++){
for(int j = ;j < i;j++){
dp[i] += dp[j] * dp[i-j-];
}
}
return dp[n];
}
};
95. Unique Binary Search Trees II
https://www.cnblogs.com/grandyang/p/4301096.html
这个题与96. Unique Binary Search Trees不同,96. Unique Binary Search Trees是求生成的二叉搜索树的个数,这个题是把所有可能找出来。
使用分治的方法做,左半边构成左子树,右半边构成右子树。
因为数字是从小到大排列,自然能形成二叉搜索树。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode*> generateTrees(int n) {
if(n == )
return {};
return generateTrees(,n);
}
vector<TreeNode*> generateTrees(int start,int end){
if(start > end)
return {NULL};
vector<TreeNode*> res;
for(int i = start;i <= end;i++){
vector<TreeNode*> left = generateTrees(start,i-);
vector<TreeNode*> right = generateTrees(i+,end);
for(int j = ;j < left.size();j++){
for(int k = ;k < right.size();k++){
TreeNode* root = new TreeNode(i);
root->left = left[j];
root->right = right[k];
res.push_back(root);
}
}
}
return res;
}
};
241. Different Ways to Add Parentheses
https://www.cnblogs.com/grandyang/p/4682458.html
这个题和95. Unique Binary Search Trees II的做法很像,分成当前位置和左侧、右侧。不同的是,这里的当前位置必须是符号出现的时候。
最后可能出现没有符号的情况,这个时候就需要将整个字符串转成int型数字输出。
注意:第一个是input.substr(0, i),而不是input.substr(0, i-1)。很容易像95. Unique Binary Search Trees II那样写成i - 1,实质上left确实等于i-1前的,但是substr第二个参数是字符的个数,前i-1个的个数就是i。
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> res;
for(int i = ;i < input.size();i++){
if(input[i] == '+' || input[i] == '-' || input[i] == '*'){
vector<int> left = diffWaysToCompute(input.substr(,i));
vector<int> right = diffWaysToCompute(input.substr(i+));
for(int j = ;j < left.size();j++){
for(int k = ;k < right.size();k++){
if(input[i] == '+')
res.push_back(left[j] + right[k]);
else if(input[i] == '-')
res.push_back(left[j] - right[k]);
else
res.push_back(left[j] * right[k]);
}
}
}
}
if(res.empty())
res.push_back(stoi(input));
return res;
}
};
leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses的更多相关文章
- 【LeetCode】241. Different Ways to Add Parentheses
Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...
- 【LeetCode】241. Different Ways to Add Parentheses 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归构建所有表达式 方法二:分而治之 日期 ...
- [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- leetcode@ [241] Different Ways to Add Parentheses (Divide and Conquer)
https://leetcode.com/problems/different-ways-to-add-parentheses/ Given a string of numbers and opera ...
- (medium)LeetCode 241.Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- [LeetCode#241]Different Ways to Add Parentheses
Problem: Given a string of numbers and operators, return all possible results from computing all the ...
- LN : leetcode 241 Different Ways to Add Parentheses
lc 241 Different Ways to Add Parentheses 241 Different Ways to Add Parentheses Given a string of num ...
- LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)
题目: Given a string of numbers and operators, return all possible results from computing all the diff ...
- LeetCode 96. 不同的二叉搜索树(Unique Binary Search Trees )
题目描述 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 输出: 解释: 给定 n = , 一共有 种不同结构的二叉搜索树: \ / / / \ \ / / ...
随机推荐
- Flutter——AppBar组件(顶部导航组件)
AppBar组件的常用属性如下: 属性 描述 leading 在标题前面显示的一个控件,在首页通常显示应用的 logo:在其他界面通常显示为返回按钮 title 标题,通常显示为当前界面的标题文字,可 ...
- 《浏览器工作原理与实践》<10>作用域链和闭包 :代码中出现相同的变量,JavaScript引擎是如何选择的?
在上一篇文章中我们讲到了什么是作用域,以及 ES6 是如何通过变量环境和词法环境来同时支持变量提升和块级作用域,在最后我们也提到了如何通过词法环境和变量环境来查找变量,这其中就涉及到作用域链的概念. ...
- xargs 使用详解
参考转载:https://www.cnblogs.com/f-ck-need-u/p/5925923.html#auto_id_12 xargs 作用:将管道传输过来的stdin进行处理(分割.分批) ...
- jade继承
代码的复用是jade非常突出的一个设计目标,jade不仅仅通过mixin来复用代码,他在文件的组织能力上也非常突出,说白了,就是继承和包涵 block定义的方式和调用的方式 block desc ...
- 【反向多源点同时BFS一个强连通图+类最短路题面】Fair-986C-Codeforce
借鉴博客:https://www.cnblogs.com/zhangjiuding/p/9112273.html 986-A. Fair /* 986-A-Fair,codeforce: 大致题意: ...
- C# 对Outlook2010进行二次开发
第一步:添加新项目 第二步:添加新的页签,注意,此页签是显示到Outlook主界面的 第三步:添加自己想要的文本框以及按钮 第四步:如果你想将此界面显示到主界面的话,需要这样设置:属性里面的Ribbo ...
- 软件测试第二周个人作业:WordCount
github地址:https:/github.com/muzhailong/wc.git 第一次写博客很不容易,也算是一个好的开始吧. 1. 个人作业要求 作业简述:根据WordCount的需求描 ...
- node.js通过回调函数获取异步函数的返回结果
html文件代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- 002_linuxC++_.h和.c文件
(一)程序修改001_linuxC++之_类的引入 (二)修改成为.h和.c文件 #include <stdio.h> #include "person.h" int ...
- ueditor+word粘贴上传!
图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码 目前限chrome浏览器使用,但是项目要求需要支持所有的浏览器,包括Windows和macOS系统.没有办 ...