LeetCode.241

题目大意
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 + - *
思路: 分治
我们对于每一个运算符可以考虑它左边可以运算出的值 和 右边可以运算出的值,他们两个再进行运算的值就是结果的可能值
class Solution {
public:
int stoi(string s) {
int cnt = 0;
for(char c: s) {
cnt = cnt * 10 + c - '0';
}
return cnt;
}
vector<int> diffWaysToCompute(string expression) {
vector<int> count;
int len = expression.size();
cout << expression << endl;
for(int i = 0; i < expression.size(); i ++ ) {
if(expression[i] == '+' || expression[i] == '-' || expression[i] == '*') {
char c = expression[i];
vector<int> left = diffWaysToCompute(expression.substr(0, i));
vector<int> right = diffWaysToCompute(expression.substr(i + 1));
for(auto l : left)
for(auto r: right) {
if(c == '+')
count.push_back(l + r);
else if(c == '-')
count.push_back(l - r);
else
count.push_back(l * r);
}
}
}
if(count.empty()) {
count.push_back(stoi(expression));
}
return count;
}
};
LeetCode.241的更多相关文章
- 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 ...
- 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 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- Java实现 LeetCode 241 为运算表达式设计优先级
241. 为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 ...
- (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 ...
- Leetcode 241.为运算表达式设计优先级
为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输 ...
- 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 & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
随机推荐
- Boosting Adversarial Training with Hypersphere Embedding
目录 概 主要内容 代码 Pang T., Yang X., Dong Y., Xu K., Su H., Zhu J. Boosting Adversarial Training with Hype ...
- JUC之线程间定制化通信
线程通信之定制化 之前文章中写了下Condition的使用,这里我们详细说下其中的用法: 首先使用Condition需要实例化Lock private Lock lock = new Reentran ...
- Android物联网应用程序开发(智慧城市)—— 火焰监控界面开发
效果: 布局代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns: ...
- JavaScript交互式网页设计 • 【第7章 jQuery操作 DOM】
全部章节 >>>> 本章目录 7.1 DOM 对象和 jQuery 对象 7.1.1 DOM 对象 7.1.2 jQuery 对象 7.1.3 jQuery 对象和 DOM ...
- exit hook
之前经常改 malloc_hook , realloc_hook,free_hook 为 one_gadget 来 get shell ,最近看到一种利用是改 exit hook(winmt师傅告诉我 ...
- [学习笔记] IT项目管理 - 关键路径法
关键路径法 只有项目网络中最常的或者耗时最多的活动完成之后,项目才能结束,这条最长的活动路线就叫关键路径.组成关键路径的活动称为关键活动. 图形表示 最早开始时间ES 工期Duration 最早结束时 ...
- 教你三步在CentOS 7 中安装或升级最新的内核
转载自:https://www.linuxprobe.com/update-kernel-centos7.html #步骤 1:检查已安装的内核版本 >uname -sr #步骤 2:在 Cen ...
- c# - 一个.cs类文件里如何建多个类
方法类可以使用 internal 修饰符,意为接口类, 主函数建议添加私有修饰符 private 控制台打印
- 从内存管理原理,窥探OS内存管理机制
摘要:本文将从最简单的内存管理原理说起,带大家一起窥探OS的内存管理机制,由此熟悉底层的内存管理机制,写出高效的应用程序. 本文分享自华为云社区<探索OS的内存管理原理>,作者:元闰子 . ...
- 关于 Intel CPU 和Iris Xe Graphics的报告问题
关于 Intel CPU 和Iris Xe Graphics的报告问题 有些用户报告了一些技术问题,这里有更多的信息和如何解决. Intel 11th CPU & Iris Xe Graphi ...