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 = , 一共有 种不同结构的二叉搜索树: \ / / / \ \ / / ...
随机推荐
- 4.解析配置文件 redis.conf
将原始的redis.conf拷贝,得到一个myRedis.conf文件,修改配置文件时,就修改这个文件,不对原始的配置文件进行修改 redis配置文件中主要有以下内容: 1.units单位 a)配置大 ...
- SmartBinding工作原理分析
关于kbmMW SmartBinding,我翻译了作者写的几篇文章,其强大的绑定机制,将可视控制与各种数据源灵活绑定在一起,实现了类似DBEdit数据敏感控件的效果,可以及大的减少我们的代码,实现界面 ...
- 作为一个java高级工程师的进阶之路
本文可能可能更偏向于是内心的独白篇和面试技巧总结 一.独白 之前也面试别人,现在轮到自己找工作,怎么说呢,每个面试官的看法不一样,面试的方式就不一样,比如我面试别人我喜欢问项目中他用到了那些,然后针对 ...
- 一分钟了解Linux文件系统
Linux文件系统原理在所有的操作系统中文件都有文件名与数据,在Linux系统上文件系统分成两个部分:用户数据 (user data) 与元数据 (metadata).用户数据,即文件数据块 (dat ...
- java实战(一)-------jdk环境在windows安装及配置
1.jdk官方下载 http://www.oracle.com/technetwork/java/javase/downloads/index.html 点击下载windows的版本:jdk-13.0 ...
- js面向对象篇(一)
Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(OOP)语言,因为它的语法中没有class(类). ----摘 ...
- Educational Codeforces Round 76 (Rated for Div. 2) D
D题 原题链接 题意:就是给你n个怪兽有一个属性(攻击力),m个英雄,每个英雄有两种属性(分别为攻击力,和可攻击次数),当安排最好的情况下,最少的天数(每选择一个英雄出战就是一天) 思路:因为怪兽是不 ...
- Petrozavodsk Winter Training Camp 2018 Jagiellonian U Contest Problem A. XOR
先把所有的数异或起来 得到sum 然后sum有一些位是1一些位是0 是0的位表示所有数里面有这位的数是偶数个 则无论怎么划分数 这一位对最终的答案都是不会有贡献的 因为偶数=偶数+偶数/奇数+奇数 ...
- Mysql读写分离 及高可用高性能负载均衡实现
什么是读写分离,说白了就是mysql服务器读的操作和写的操作是分开的,当然这个需要两台服务器,master负责写,slave负责读,当然我们可以使用多个slave,这样我们也实现了简单意义上的高可用和 ...
- P1006 传纸条[棋盘DP]
题目来源:洛谷 题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接 ...