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 = , 一共有 种不同结构的二叉搜索树: \ / / / \ \ / / ...
随机推荐
- js 执行完setTimeout再接着执行函数
var counter = 0; function increase(){ var d = jQuery.Deferred(); var doIncrease = function() { if(co ...
- 一个基于Scrapy框架的pixiv爬虫
源码 https://github.com/vicety/Pixiv-Crawler,功能什么的都在这里介绍了 说几个重要的部分吧 登录部分 困扰我最久的部分,网上找的其他pixiv爬虫的登录方式大多 ...
- Ubuntu18.0 解决python虚拟环境中不同用户下或者python多版本环境中指定虚拟环境的使用问题
一. 不同用户下配置virtualenvwrapper的问题 问题描述: 安装virtualnev和virtualnevwrapper之后,在.bashrc进行virtualenvwrapper的相关 ...
- MySQL菜鸟入门“秘籍”
一.MySQL简介 1.什么是数据库 ? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不 ...
- 1.Hbase简介
1. Hbase简介 1.1. 什么是hbase(面向列) HBASE是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBASE技术可在廉价PC Server上搭建起大规模 结构化存储集群 ...
- 通过字节码分析Java异常处理机制
在上一次[https://www.cnblogs.com/webor2006/p/9691523.html]初步对异常表相关的概念进行了了解,先来回顾一下: 其源代码也贴一下: 下面来看一下jclas ...
- BCB6 使用TZCompressionStream压缩
最近由于项目需要涉及到解压第三方公司的数据,在此做一下记录环境部署和使用方法,免得以后忘记. 对方公司的数据是通过TCompressionStream 压缩之后,存到数据库中,采用的 ...
- Nginx中ngx_http_upstream_module模块
用于将多个服务器器定义成服务器器组,⽽而由 proxy_pass , fastcgi_pass 等指令进⾏行行引⽤用upstream backend {server backend1.example. ...
- pandas处理json脱坑(二)--jsonError: Expecting ',' delimiter: line 1 column 2674
Expecting ',' delimiter: line 1 column 2674 json_dict = json.loads(row[json_columns].replace("' ...
- ZrOJ #882. 画画
最后染成的图形一定一样的. 那么只用考虑两条路径在那些地方重合,重合的地方可以交换,这样答案就是2的重合次数次方.直接模拟就行了. qiang- CODE #include <bits/stdc ...