【LeetCode 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 :

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]

题意:

  给一串表达式(由+、-、*以及数字组成),通过加括号改变运算顺序,求所有可能的结果。

思路:

  没有想到什么比较巧妙的方法,只能递归所有的可能咯,其中有很多重复计算,等找到好办法了再来更新吧。

C++:

 1 class Solution {
 2 public:
 3
 4     //string转为int
 5     int getInts(string str)
 6     {
 7         int ret = 0;
 8
 9         stringstream ss;
10         ss << str;
11         ss >> ret;
12
13         return ret;
14     }
15
16     vector<int> rec(vector<int>& num, int start, int ends)
17     {
18         vector<int> vec;
19
20         if(start == ends)
21         {
22             vec.push_back(num[start]);
23             return vec;
24         }
25
26         for(int i = start; i <= ends; i++)
27         {
28             if(num[i] == -3)
29             {
30                 vector<int> vecl = rec(num, start, i - 1);
31                 vector<int> vecr = rec(num, i + 1, ends);
32
33                 vector<int>::iterator itl = vecl.begin();
34                 for(; itl != vecl.end(); itl++)
35                 {
36                     vector<int>::iterator itr = vecr.begin();
37                     for(; itr != vecr.end(); itr++)
38                         vec.push_back((*itl) + (*itr));
39                 }
40             }
41             if(num[i] == -1)
42             {
43                 vector<int> vecl = rec(num, start, i - 1);
44                 vector<int> vecr = rec(num, i + 1, ends);
45
46                 vector<int>::iterator itl = vecl.begin();
47                 for(; itl != vecl.end(); itl++)
48                 {
49                     vector<int>::iterator itr = vecr.begin();
50                     for(; itr != vecr.end(); itr++)
51                         vec.push_back((*itl) - (*itr));
52                 }
53             }
54             if(num[i] == -4)
55             {
56                 vector<int> vecl = rec(num, start, i - 1);
57                 vector<int> vecr = rec(num, i + 1, ends);
58
59                 vector<int>::iterator itl = vecl.begin();
60                 for(; itl != vecl.end(); itl++)
61                 {
62                     vector<int>::iterator itr = vecr.begin();
63                     for(; itr != vecr.end(); itr++)
64                         vec.push_back((*itl) * (*itr));
65                 }
66             }
67         }
68         return vec;
69     }
70
71     vector<int> diffWaysToCompute(string input) {
72
73         //先把字符串转换为一个int型的数组,符号用负数来表示
74         vector<int> nums;
75         int index = 0;
76         for(int i = 0; i < input.size(); i++)
77         {
78             if(input[i] == ‘+‘ || input[i] == ‘-‘ || input[i] == ‘*‘)
79             {
80                 string str = input.substr(index, i - index);
81                 nums.push_back(getInts(str));
82                 nums.push_back(input[i]-46); //加号转换为-3, 减号转换为-1,乘号为-4
83                 index = i + 1;
84             }
85             if(i == input.size() - 1)
86             {
87                 string str = input.substr(index, input.size() - index);
88                 nums.push_back(getInts(str));
89             }
90         }
91
92         int len = nums.size();
93         if(len == 0 || len == 1)
94             return nums;
95
96         vector<int> ret = rec(nums, 0, len - 1);
97         return ret;
98     }
99 };

AC: https://leetcode.com/submissions/detail/34357736/

时间: 2024-10-16 04:29:46

【LeetCode 241】Different Ways to Add Parentheses的相关文章

【Leetcode】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)) =

Leetcode Algorithm No.241Different Ways to Add Parentheses

题目来自Leetcode 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)

LeetCode OJ : 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: 

LeetCode 241. Different Ways to Add Parentheses

241. Different Ways to Add Parentheses Add to List Description Submission Solutions Total Accepted: 38849 Total Submissions: 92740 Difficulty: Medium Contributors: Admin Given a string of numbers and operators, return all possible results from comput

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

【LeetCode OJ】Longest Consecutive Sequence

Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classical problem where we can reduce the running time by the help of hash table. By given a list of numbers, we can find the longest consecutive sequence b

【LeetCode OJ】Word Ladder I

Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in this problem: Hash Table. One hash set is the words dictionary where we can check if a word is in the dictionary in O(1) time. The other hash set is us

【leetcode系列】3Sum

这个题我最开始的思路是:先一个数定下来,然后在除这个数之外的集合里面找另外两个数,最后计算和.如此反复,对于N个数,需要进行N-2次循环. 我遇到的问题就是怎么找另外两个数,其实我想过参照Two Sum里面的解法,就是用Hashtable存,键值对的结构是<任意两个数的和,<下标1,下标2>>,但是构造这个Hashtable就需要O(N^2),后面真正解的时候有需要O(N^2). 参考了大牛的解法后,明白了找两个数还是用两个下标同时往中间移动比较好,下面上代码. import ja

【LeetCode 229】Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 思路: [LeetCode 169]Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛.因此,接着上一题的方