lc 241 Different Ways to Add Parentheses


241 Different Ways to Add Parentheses

Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.

Example 1

Input: "2-1-1".

((2-1)-1) = 0
(2-(1-1)) = 2
Output: [0, 2]

Example 2

Input: "2*3-4*5"

(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10
Output: [-34, -14, -10, -10, 10]

分治法 Accepted##

这几周的算法课,老师一直在讲分治法的应用,所以现在尝试利用分治法去解决这个问题。利用递归,将复杂的问题拆分成同样的小的问题。

分治法的精髓:

分--将问题分解为规模更小的子问题;

治--将这些规模更小的子问题逐个击破;

合--将已解决的子问题合并,最终得出“母”问题的解;

class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> ans;
for (int i = 0; i < input.size(); i++) {
char c = input[i];
if (ispunct(c)) {
for (auto a : diffWaysToCompute(input.substr(0, i))) {
for (auto b : diffWaysToCompute(input.substr(i+1))) {
ans.push_back(c == '-' ? a-b : c == '+' ? a+b : a*b);
}
}
}
}
return ans.size() ? ans : vector<int>{stoi(input)};
}
};

LN : leetcode 241 Different Ways to Add Parentheses的更多相关文章

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

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

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

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

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

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

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

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

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

  6. 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]* ...

  7. 241. Different Ways to Add Parentheses

    241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...

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

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

  9. LC 241. Different Ways to Add Parentheses

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

随机推荐

  1. JS函数节流代码实现

    函数被频繁调用场景 Js中的函数大多数情况下都是由用户主动调用触发的,一般不会遇到性能相关的问题.但在一些少数情况下,函数的触发不是由用户直接控制.在这些场景下,函数有可能被非常频繁地调用,而造成大的 ...

  2. Project Perfect让Swift在server端跑起来-Perfect in Visual Studio Code (四)

    编者语 : 本系列文章已经被Perfect官方引用了,这样的感觉非常好.感恩!Thx all ! Visual Studio Code是一个轻量级的编辑器,但也功能丰富,通过插件你能够完毕如Cordo ...

  3. AE After Effect 如何替换和修改素材

    替换素材:如图所示,相框外的人是成双成对的,相框里面的却只有一个人,我们想要把这个素材替换成两个人的.我们鼠标放到视频预览框的任何一个元素上面底部都会提示这是什么素材.比如我放到一个人的上面,则该人物 ...

  4. Chromium硬件加速渲染的UI合成过程分析

    在Chromium中.Render端和WebGL端绘制出来的UI终于是通过Browser端显示在屏幕上的.换句话说.就是Browser端负责合成Render端和WebGL端的UI.这涉及到不同Open ...

  5. python搭建web server

    假设你急需一个简单的Web Server,但你又不想去下载并安装那些复杂的HTTP服务程序,比方:Apache,ISS等.那么, Python 可能帮助你.使用Python能够完毕一个简单的内建 HT ...

  6. 大数据R语言简析

    R语言是用于统计分析.画图的语言和操作环境.R是属于GNU系统的一个自由.免费.源码开放的软件.它是一个用于统计计算和统计制图的优秀工具. R是统计领域广泛使用的诞生于1980年左右的S语言的一个分支 ...

  7. OSX:不同OSX版本号的标记可能不兼容

    现象: 依据測试,中文OS X 10.9和中文10.10的文件标记彼此不兼容. 也就是说.比方在10.9中的颜色标记,在10.10DP2中不能删除,但能够加入/删除10.10自己的颜色标记,反之亦然. ...

  8. 【bzoj3609】[Heoi2014]人人尽说江南好

    可以算出合并多少次. #include<algorithm> #include<iostream> #include<cstdlib> #include<cs ...

  9. java SocketChannel and ServerSocketChannel

    1 SocketChannel 1.1 打开一个SocketChannel SocketChannel socketChannel = SocketChannel.open(); socketChan ...

  10. dom小练习

    dom小练习 学习要点 综合运用学过的知识完成几个综合小练习,巩固学过的知识. 阶段小练习8-1:改变网页字体的大小 要求和提示: 要求:当用户选择‘大/中/小’的选项时,页面字体发生相应的变化 阶段 ...