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. codevs——1013 求先序排列

    1013 求先序排列 2001年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 给出 ...

  2. SAS学习笔记 - 基本原理与概念

    1.赋值符号 由一个尖括号和一个符号组成,可以从左到右也可以从右到左,即“->”或者“<-”. 赋值号也可以使用等号“=”. 如果对象已经存在,那么原先的值会被覆盖.除了可以赋一个数值,还 ...

  3. Android应用层View绘制流程之measure,layout,draw三步曲

    概述 上一篇博文对DecorView和ViewRootImpl的关系进行了剖析,这篇文章主要是来剖析View绘制的三个基本流程:measure,layout,draw.仅仅有把这三个基本流程搞清楚了, ...

  4. seajs入门使用

    使用 Sea.js 进行模块化开发还能够带来非常多优点: 模块的版本号管理. 通过别名等配置,配合构建工具,能够比較轻松地实现模块的版本号管理. 提高可维护性.模块化能够让每一个文件的职责单一,很有利 ...

  5. Windows 7 SID 修改

    在安裝Windows系統時會產生一個獨一無二的SID (Security ID),它用來識別每一部主機,若在同一個區域網路內有兩部相同SID的主機,會出現警告訊息.一般而言,每次安裝時的SID不可能會 ...

  6. Start Xamarin——与Microsoft 的sales development manager的闲谈

    由于在Xamarin属于微软之前,就已经有Xamarin的账号,试用过破解版的.所以4月1号微软set Xamarin free之后.就收到了Xamarin的邀请试用邮件. 试用完了之后第二天.收到邮 ...

  7. oracle initialization or shutdown in progress 问题解决

    今天登录oracle时遇到oracle initialization or shutdown in progress 这个错误提示,在网上搜了下,试了非常多方法,最后结合几种方法结合,成功攻克了问题! ...

  8. Appium、selenium与Robot Framework

    Robot Framework + Appium Appium-Python-Client: 与Appium Library和Appium Server的交互Appium Library通过Appii ...

  9. Nginx中配置vue,react项目地址

    如题 像以前在Nginx中配置域名解析的时候只需要在conf.d文件夹下添加对应的xx.conf文件(当然了你也可以在nginx.conf)下配置. 如果是以前的老项目只需要在配置文件中server内 ...

  10. eclipse到Android Studio的项目迁移

    一直以来.公司开发都是用eclipse.可是随着我们应用不断成长.项目结构越来越庞大.项目间依赖关系变得非常复杂.用eclipse管理显得非常吃力,常常一个同事更改依赖项目之后,别人在更新.都会出现故 ...