https://leetcode.com/problems/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]

为数不多的考察分治算法的题:

class Solution {
public: bool isNumber(string s) {
for(int i=; i<s.length(); ++i) {
if(!(s[i] <= '' && s[i] >= '')) return false;
}
return true;
} int toInt(string s) {
if(s == "") return ; int res = ;
for(int i=; i<s.length(); ++i) {
res = res* + (s[i]-'');
}
return res;
} vector<int> dfs(string input) {
vector<int> load;
if(isNumber(input)) {
load.push_back(toInt(input));
return load;
} int len = input.length();
vector<int> l, r;
for(int i=; i<len; ++i) {
if(input[i] == '-') {
l = dfs(input.substr(, i));
r = dfs(input.substr(i+, len-i-));
for(int p=; p<l.size(); ++p) {
for(int q=; q<r.size(); ++q) {
load.push_back(l[p] - r[q]);
}
}
}
else if(input[i] == '+') {
l = dfs(input.substr(, i));
r = dfs(input.substr(i+, len-i-));
for(int p=; p<l.size(); ++p) {
for(int q=; q<r.size(); ++q) {
load.push_back(l[p] + r[q]);
}
}
}
else if(input[i] == '*') {
l = dfs(input.substr(, i));
r = dfs(input.substr(i+, len-i-));
for(int p=; p<l.size(); ++p) {
for(int q=; q<r.size(); ++q) {
load.push_back(l[p] * r[q]);
}
}
}
} return load;
} vector<int> diffWaysToCompute(string input) {
vector<int> res;
int len = input.length();
if(len == ) return res;
if(isNumber(input)) {
res.push_back(toInt(input));
return res;
} vector<int> l, r;
for(int i=; i<len; ++i) {
if(input[i] == '-') {
l = dfs(input.substr(, i));
r = dfs(input.substr(i+, len-i-));
for(int p=; p<l.size(); ++p) {
for(int q=; q<r.size(); ++q) {
res.push_back(l[p] - r[q]);
}
}
}
else if(input[i] == '+') {
l = dfs(input.substr(, i));
r = dfs(input.substr(i+, len-i-));
for(int p=; p<l.size(); ++p) {
for(int q=; q<r.size(); ++q) {
res.push_back(l[p] + r[q]);
}
}
}
else if(input[i] == '*') {
l = dfs(input.substr(, i));
r = dfs(input.substr(i+, len-i-));
for(int p=; p<l.size(); ++p) {
for(int q=; q<r.size(); ++q) {
res.push_back(l[p] * r[q]);
}
}
}
} //sort(res.begin(), res.end());
return res;
}
};

leetcode@ [241] Different Ways to Add Parentheses (Divide and Conquer)的更多相关文章

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

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

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

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

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

  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. Smarty格式化数字为INT数

    <? require("setup.php"); define('PAGETITLE','pagtitle'); function insert_top($lid,$sid) ...

  2. 在smarty模板中嵌入php代码

    我个人并不太喜欢smarty的语法,写起来比较啰嗦易出现匹配出错,但是旧项目中有许多工程都是采用它作模板.最近需要在此上稍微加一些PHP的内容,但我不想在模板控制层去一个一个assign,而想在模板文 ...

  3. dijkstra,bellman-ford,floyd分析比较

    http://www.cnblogs.com/mengxm-lincf/archive/2012/02/11/2346288.html 其实我一直存在疑惑是什么导致dijkstra不能处理负权图? 今 ...

  4. ***CI异常记录到日志:CodeIgniter中设计一个全局exception hook

    在CodeIgniter中,当发生异常时,经常要通知系统管理员,因此有必要在全局的高度上 捕捉异常,因此可以写一个hook, 比如在config目录的hook.php中,加入: $hook['pre_ ...

  5. SaaS系列介绍之八: SaaS的运营模式

    1 引言 软件的核心是它为用户解决领域相关问题的能力.               ________Eric Evans,<领域驱动设计> 传统的软件生命周期中,软件的维护占整个过程的70 ...

  6. boost在linux下的编译和使用

    上一篇boost在windows可以正常的使用了,但是在linux下不行. [尝试一:使用和windows同一套代码编译,编译时报错] 我是在Ubuntu使用共享文件夹的方式和windows使用的同一 ...

  7. vimrc for windows

    set nobackupsource $VIMRUNTIME/vimrc_example.vimsource $VIMRUNTIME/mswin.vimbehave mswin:color deser ...

  8. 数据关联分析 association analysis (Aprior算法,python代码)

    1基本概念 购物篮事务(market basket transaction),如下表,表中每一行对应一个事务,包含唯一标识TID,和购买的商品集合.本文介绍一种成为关联分析(association a ...

  9. [译]好程序员的五声“呐喊”

    通常编程情况下,会导致软件项目变坏的一些列反应 原文:The five shouts of good programmers 在任何一天,在这个世界上都有软件项目正在失败,这很常见.常见到当软件产品按 ...

  10. (转)详解LVS负载均衡之三种工作模型原理和10种调度算法

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://linuxnx.blog.51cto.com/6676498/1195379 LV ...