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 = , 一共有 种不同结构的二叉搜索树: \ / / / \ \ / / ...
随机推荐
- 6.Redis的事务
Redis的事务(Redis部分支持事务) a)是什么 可以一次执行多个命令,本质是一组命令的集合.一个事务中的所有命令都会序列化,按顺序地串行化执行而不会被其它命令插入,不许加塞 b)能干吗 一个( ...
- java入门学习总结_03
1.键盘录入 2.分支结构 键盘录入 概述 1.键盘录入:在程序运行的过程中,可以让用户录入一些数据,存储在内存的变量中,在后续的程序运行过程中,可以使用这些数据. 2.步骤: 第一步:导包,在类声明 ...
- apk签名文件生成
一.生成签名文件 命令: keytool -genkey -v -keystore 签名文件名称.keystore -alias 签名文件别名 -keyalg RSA -keysize 2048 -v ...
- 【问题】XShell连接不上Debian root用户
类似文章:https://www.lianst.com/3231.html 修改此文件 重启ssh服务 ssh restart有问题,换一条命令OK 你的Linux发行版可能不一样,针对CentOS参 ...
- C# Winfrom DataGridView常用设置
DataGridView常用设置 using System; using System.Collections.Generic; using System.Drawing; using System. ...
- kafka学习链接收藏
1.kafka官方文档 Apache Kafka : broker.producer.consumer等参数配置直接看目录 2.系统学习 kafka中文教程 - OrcHome <Apache ...
- Map集合中get不存在的key值
返回的值是null 测试代码 import java.util.HashMap; import java.util.Map; public class Test { public static voi ...
- spring实例化三:CglibSubclassingInstantiationStrategy
在SimpleInstantiationStrategy类中,留下了包含MethodOverride对象的bd对象定义未做实现,做了抽象.CglibSubclassingInstanti ...
- 大数据之路week05--day02(Maven安装,环境变量的配置及基本使用)
今天我们就来学习一下maven,怎么说呢,maven更像是一种管理的工具,实现的原理是使用插件. 举个例子,比如说,一个公司需要做一个项目,这个项目又分成了很多的模块,每个模块又分成了许多的业务等等, ...
- C# List<T> 集合使用
1.初始化集合 , ); , ); , ); ) { graham, emerson, mario }; 2. 添加元素 racers.Add(, )); racers.Add(, )); racer ...