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;+-------+-------------+------+-----+---------+------------- ...
随机推荐
- buildroot 搭建ftpd 服务器记录
vsftpd 搭建失败,应该是buildroot 文件系统还有操作没有理解透,还需要不断的学习. 所以用轻量级的 ftpd 进行替代, 步骤如下: // ---> make busybox-me ...
- [小技巧]diff的文件夹忽略使用方式
当我们比较两个文件夹时经常需要忽略.svn或者.git,那么如下 diff -r -x ".git" -x "*.ko" -x "*.o" ...
- http://blog.csdn.net/ce123_zhouwei/article/details/7364294
http://blog.csdn.net/ce123_zhouwei/article/details/7364294
- contiki bsp
1 lpc1768 git clone https://github.com/bolandi/contiki.git 2 efm32 git clone https://github.com/ ...
- 《FPGA全程进阶---实战演练》第三章之PCB叠层
1.双面板 在双层板设计layout时,最好不要不成梳状结构,因为这样构成的电路,回路面积较大,但是只要对较重要的信号加以地保护,布线完成之后将空的地方敷上地铜皮,并在多个过孔将两个地连接起来,可以弥 ...
- 关于ARM中的tst、cmp、bne、beq指令
一.关于cmp的详细用法 假设现在AX寄存器中的数是0002H,BX寄存器中的数是0003H. 执行的指令是:CMP AX, BX 执行这条指令时,先做用AX中的数减去BX中的数的减法运算. 列出二进 ...
- Spring Tools Suite (STS) 简介
首先,sts是一个定制版的Eclipse,专为Spring开发定制的,方便创建调试运行维护Spring应用. 官方页面.下载地址(3.8.1 win x64). 其次,没什么好介绍的,用一下就明白了. ...
- 如果分配给命令的连接位于本地挂起事务中,ExecuteNonQuery 要求命令拥有事务。命令的 Transaction 属性尚未初始化
DbConnection dbc = database.CreateConnection(); DbTransaction dbtt = null; try { dbc.Open(); dbtt = ...
- LabVIEW中数组的自动索引
我们在LabVIEW里面使用While或者是For循环结构的时候,就会发现每一个循环中在它们的循环结构的边界都可以自动完成一个数组元素的索引或累积.LabVIEW中循环结构的这种能力就叫做自动索引(A ...
- 手动安装OpenStack Mistral
Prepare packages: $ sudo apt-get install python-dev python-setuptools python-pip libffi-dev libxslt1 ...