241. Different Ways to Add Parentheses
241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parentheses/
思路就是:首先找到以运算符为根节点,分别计算左子串和右子串的所有结果的集合,然后依次进行组合计算。参考博客http://www.cnblogs.com/ganganloveu/p/4681439.html。
自己的思路错了,直接用两边只用了一个整数去接收左右子串的计算值!!
#include<iostream> #include<vector> #include<string> using namespace std; class Solution { public:vector<int> diffWaysToCompute(string input) {vector<int> temp;for (int i = 0; i < input.size(); i++){if (isop(input[i])){vector<int> left = diffWaysToCompute(input.substr(0, i));vector<int> right = diffWaysToCompute(input.substr(i + 1));for (int j = 0; j < left.size(); j++){for (int k = 0; k < right.size(); k++){temp.push_back(compute(left[j], right[k], input[i]));}}}}if (temp.empty()){temp.push_back(atoi(input.c_str()));}return temp;}bool isop(char ch){if (ch == '+' || ch == '-' || ch == '*')return true;return false;}int compute(int v1, int v2, char ch){int sum = 0;switch (ch){case '+':sum = v1 + v2; break;case '-':sum = v1 - v2; break;case '*':sum = v1*v2; break;}return sum;}}; int main() {Solution test;string te = "2+4*3";vector<int> res=test.diffWaysToCompute(te);for (auto it = res.begin(); it != res.end(); it++){cout << *it << endl;}return 0; }
转载于:https://www.cnblogs.com/chess/p/5296502.html
241. Different Ways to Add Parentheses相关推荐
- leetcode 241. Different Ways to Add Parentheses | 241. 为运算表达式设计优先级(Java)
题目 https://leetcode.com/problems/different-ways-to-add-parentheses/ 题解 参考:C++ Solution [Faster than ...
- 【LeetCode】241. Different Ways to Add Parentheses
题目: Given a string of numbers and operators, return all possible results from computing all the diff ...
- LeedCode 241. Different Ways to Add Parentheses
一.题目 Given a string expression of numbers and operators, return all possible results from computing ...
- LeetCode-241. Different Ways to Add Parentheses [C++][Java]
LeetCode-241. Different Ways to Add Parentheseshttps://leetcode.com/problems/different-ways-to-add-p ...
- [LeetCode]241.Different Ways to Add Parenthese
description Given a string of numbers and operators, return all possible results from computing all ...
- LeetCode Different Ways to Add Parentheses(递归,动态规划)
问题:给出一个表达式,求其添加括号后可能的结果 思路:第一种方式是根据运算符号作分割,分为两个子问题. 第二种方式是在第一种方式下作记忆,避免重复计算问题. 第二种方式使用动态规划,先将操作数和操作符 ...
- LeetCode----241. Different Ways to Add Parenthese(M)分治
1 题目 Given a string of numbers and operators, return all possible results from computing all the dif ...
- 继续过中等难度.0309
. 8 String to Integer (atoi) 13.9% Medium . 151 Reverse Words in a String 15.7% Mediu ...
- Leetcode重点250题
LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...
最新文章
- 【数字信号处理】基本序列 ( 实指数序列 | 收敛序列 | 发散序列 )
- python3学习笔记(2)_list-tuple
- 云+X案例展 | 金融类:荣之联助力君康人寿构建新一代数据中心
- mysql table as_Mysql中create table as与like命令的区别
- 第 7 章 本地方法栈
- navicat for MySQL连接本地数据库时报1045错误的解决方法
- 项目管理相关的考试认证及证书价值介绍
- 《信号与系统学习笔记》—线性时不变系统(二)
- 双线性插值bilinear interpolation
- SPSS——统计描述
- 基于github上camera开源框架CameraKit,实现仿微信拍照摄像功能
- 代码审查工具 rietveld的安装与使用(二)
- 关于Holder不等式等号成立的条件的理解
- SUSE(Linux操作系统)
- 3626 三元一次方程(枚举)
- 与虎谋皮,饮鸩止渴,却有什么办法呢?
- 面部表情识别---学习笔记
- FME将用SHP数据对栅格影像数据进行裁剪
- Linux之文件共享
- 传奇私服游戏支付接口申请(已解决)