LeetCode——Submission Details
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]
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
使用分治递归的方法首先将问题划分成子问题,再对子问题的解进行全排列合并,最终得到问题的解。
深刻理解递归还是很有必要的。
public class Solution {
public List<Integer> diffWaysToCompute(String input) {
List<Integer> resList = new ArrayList<Integer>();
for(int i=0; i<input.length(); i++) {
char op = input.charAt(i);
if(op == '+' || op == '-' || op == '*') {
List<Integer> leftList = diffWaysToCompute(input.substring(0, i));
List<Integer> rightList = diffWaysToCompute(input.substring(i+1));
for(int left : leftList) {
for(int right : rightList) {
if(op == '+') {
resList.add(left + right);
}
else if(op == '-') {
resList.add(left - right);
}
else {
resList.add(left * right);
}
}
}
}
}
if(resList.size() == 0) {
resList.add(Integer.parseInt(input));
}
return resList;
}
}
这题竟然没有不是个位数的case。
LeetCode——Submission Details的更多相关文章
- leetcode Submission Details
代码: #include<iostream> #include<vector> using namespace std; struct ListNode { int val; ...
- Submission Details [leetcode] 算法的改进
最先看到这一题,直觉的解法就是len从1到s1.size()-1,递归调用比較s1和s2长度为len的子串是否相等.以及剩余部分是否相等. 将s1分成s1[len + rest],分别比較s2[len ...
- 【leetcode】Submission Details
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- Submission Details
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
- [leetCode][012] Two Sum (1)
[题目]: Given an array of integers, find two numbers such that they add up to a specific target number ...
- [leetCode][016] Add Two Numbers
[题目]: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- [sqoop1.99.6] 基于1.99.6版本的一个小例子
1.创建mysql数据库.表.以及测试数据mysql> desc test;+-------+-------------+------+-----+---------+------------- ...
随机推荐
- SO_REUSEADDR的作用
服务器socketstreamtcpc 原贴地址:http://topic.csdn.net/u/20090103/16/a0414edb-b289-4c72-84da-39e155e8f4be.ht ...
- iOS边练边学--通知机制和键盘处理
一.通知中心(NSNotificationCenter) 每一个程序都有一个通知中心实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以想通知中心发布通知(NSNotification),描述 ...
- jquery-easyui 中表格的行编辑功能
具体实现代码如下: <table id="tt"></table> $('#tt').datagrid({ title:'Editable DataGrid ...
- 【转】WebService 的创建,部署和使用
WebService,即Web服务,能使得运行在不同机器上的不同应用无须借助,专门的第三方软件或硬件,就可相互交换数据或集成. 第一次选择WebService,是为了替代数据库远程连接.我们都知道当S ...
- 格局中@null的代码实现方式
布局中通常会用到@null.如RadioButton常用的技巧通过RadioGroup实现Tab,需要设置android:button="@null".如果要在代码中动态创建控件, ...
- 【转载】PADS Layout将导入DXF,并转换成板框步骤
1.在PADS Layout中选择 Import... 2.选择DXF文件(一般由结构工程师给出),直接点OK即可. 3.导入后,板框图一角视图如下.右键选择 Select Shapes,然后双击外框 ...
- 关于对afx_msg的解释-----来源百度百科
1AFX前缀 Afx前缀是微软MFC一个小组的名称简写,并没有别的意义. MFC的很多代码,包括全局函数名.宏.头文件名都使用了"Afx". Afx*.h是一组MFC的核心头文件, ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...
- 在工程名.h头文件中写public:
class CaccessimageApp : public CWinApp { public: _ConnectionPtr m_pConnection; CaccessimageApp(); // ...
- tagVARIANT、VARIANT、_variant_t和COleVariant
tagVARIANT是一个结构体struct: C++ Code: tagVARIANT 123456789101112131415161718192021222324252627282930313 ...