(算法分析Week1)Different Ways to Add Parentheses[Medium]
来源:互联网 发布:各国语言在线翻译软件 编辑:程序博客网 时间:2024/05/29 14:31
241. Different Ways to Add Parentheses[Medium]
Description
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]
Solution
分治法,以各个操作符(+、-、*)为分割,分别计算每个“小块”的结果,然后再进行全排列。自然而然想到递归,然而一开始并没有想好应该怎么设计分割后的计算结果并返回的接口,想着自己要增加函数。发现顺着题目编辑栏给出的函数原型考虑,返回一个int类型的vector就可以满足储存不同计算结果的需求。概括地说,对于每一个操作符,不停分隔左右边,然后相应返回不同情况下的result,相当于执行了【在不同位置上加上括号的要求】
Complexity analysis
时间复杂度O(Catalan(n)) 即O((2n)! / n!(n-1)!)
空间复杂度O(n),递归时系统栈空间的消耗
n为数操作符个数
这里是按网上说的,其实不会计算时间复杂度…有空再研究
Code
class Solution {public: vector<int> diffWaysToCompute(string input) { vector<int> result; char ch; for (int i = 0; i < input.length(); i++) { ch = input[i]; if (ch == '+' || ch == '*' || ch == '-') { vector<int> left = diffWaysToCompute(input.substr(0, i)); vector<int> right = diffWaysToCompute(input.substr(i+1)); for (auto l : left) { for (auto r : right) { if (ch == '+') { result.push_back(l+r); } else if (ch == '-') { result.push_back(l - r); } else if (ch == '*') { result.push_back(l*r); } } } } } if (result.empty()) { result.push_back(atoi(input.c_str())); } return result; }};
Result
阅读全文
0 0
- (算法分析Week1)Different Ways to Add Parentheses[Medium]
- 算法分析与设计丨第一周丨LeetCode(2)——Different Ways to Add Parentheses(Medium)
- [Leetcode 241, Medium] Different Ways to Add Parentheses
- Leetcode 241. Different Ways to Add Parentheses (Medium) (cpp)
- 241. Different Ways to Add Parentheses 难度:medium 类别:分治
- Leetcode 241. Different Ways to Add Parentheses[medium]
- LeetCode[241. Different Ways to Add Parentheses] 难度[medium]
- LeetCode解题报告 241. Different Ways to Add Parentheses [medium]
- Different Ways to Add Parentheses——Difficulty:Medium
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Different Ways to Add Parentheses
- Java注解详解
- UVa 699 The Falling Leaves
- 中山大学算法课程题目详解(第一周)
- LeetCode:M-142. Linked List Cycle II
- 【POJ3195】Generalized Matrioshkas(栈)
- (算法分析Week1)Different Ways to Add Parentheses[Medium]
- 三级联动
- 路径规划之A* 算法
- 一个简单的侧滑
- sqli-labs page-1
- MFC无法添加控件变量
- Leetcode327——Count of Range Sum
- LeetCode-Median of Two Sorted Arrays
- python学习第一天