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的更多相关文章

  1. 【LeetCode】241. Different Ways to Add Parentheses

    Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...

  2. 【LeetCode】241. Different Ways to Add Parentheses 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归构建所有表达式 方法二:分而治之 日期 ...

  3. [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  4. 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 ...

  5. (medium)LeetCode 241.Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  6. [LeetCode#241]Different Ways to Add Parentheses

    Problem: Given a string of numbers and operators, return all possible results from computing all the ...

  7. 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 ...

  8. LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)

    题目: Given a string of numbers and operators, return all possible results from computing all the diff ...

  9. LeetCode 96. 不同的二叉搜索树(Unique Binary Search Trees )

    题目描述 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 输出: 解释: 给定 n = , 一共有 种不同结构的二叉搜索树: \ / / / \ \ / / ...

随机推荐

  1. mysql count distinct 统计结果去重

    1.使用distinct去重(适合查询整张表的总数)有多个学校+教师投稿,需要统计出作者的总数select count(author) as total from files每个作者都投稿很多,这里有 ...

  2. 【转】DELPHI开始支持LINUX DOCKER

    这是咏南翻译Marco Cantu的文章. 在过去的几年中,将服务器端解决方案(实际上是任何类型的应用程序)部署到轻量级DOCKER而不是物理机器或虚拟机已经变得越来越普遍,因为这允许更大的灵活性(在 ...

  3. sql基础的基础

    一.数据定义语言(DDL) create table alter table drop table create index alter index drop index create view dr ...

  4. AWD模式搅屎模式

    AWD模式搅屎模式 ###0x01 出题思路 ####1:题目类型 1-出题人自己写的cms,为了恶心然后加个so. 2-常见或者不常见的cms. 3-一些框架漏洞,比如ph师傅挖的CI这种 #### ...

  5. 安装 ALC 解决 centos8 不能播放多媒体的问题

    装完centos8 后,发现看不到视频,听不到音乐,连web在线听音乐也不行.通过安装ALC可以解决. 1.三步安装VLC 这是 centos8 的安装包,曾使用 8 之前的版本安装是不成功的 sud ...

  6. LVM——header

  7. Python中的对象与参考

    参考 当创建一个对象并给它赋一个变量的时候,这个变量仅仅参考哪个对象,而不是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存.这被称作名称到对象的绑定. 对象与参考的例子 注意两次不 ...

  8. idou老师教你学Istio 27:解读Mixer Report流程

    1.概述 Mixer是Istio的核心组件,提供了遥测数据收集的功能,能够实时采集服务的请求状态等信息,以达到监控服务状态目的. 1.1 核心功能 •前置检查(Check):某服务接收并响应外部请求前 ...

  9. ASP.NET Uploadify 上传文件过大报错

    Uploadify上传文件原来很早之前用过,没发现什么问题.今天再使用过程中,当文件大于30M的时候就会报错404.查看错误消息提示配置最大上传太小了.需要修改. 记得原来配置上传文件大小在这里:&l ...

  10. re模块中的非贪婪匹配

    python的re模块中有贪婪匹配和非贪婪匹配之分,当使用*时会匹配零个或多个,使用+时会匹配一个或多个.当使用?在前边特殊符号前时会进行非贪婪匹配,匹配零个或者一个,今天主要讨论非贪婪匹配中存在的坑 ...