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;+-------+-------------+------+-----+---------+------------- ...
随机推荐
- zip文件内存中解压读取
// 构造zip输入流 ZipInputStream zip = new ZipInputStream(fis,Charset.forName("gbk")); byte[] tm ...
- 我的mac的其他满了,发现是一个叫core的文件
6Q12KFX%PDARS7B{B__OZVW.jpg (97.93 KB, 下载次数: 0) 下载附件 保存到相册 2014-7-3 15:40 上传 如图,电脑虽然买了半年但是基本没怎么用, ...
- 【3C认证】安防产品3C认证
安防产品3C认证作为3C认证的一部分.安防产品即:防范的手段达到或实现安全的目的的设备或器材. 依据<中华人民共和国产品质量法>.<中华人民共和国标准化法>.<中华人民共 ...
- 将string转换成char型的一般方法
C++文件读取中: infile in: in.open("file.dat",ios::in); 这样是能够的. 可是 string a; a="file.dat&qu ...
- Java-DBCP连接池
创建项目: 导入jar包: 参见上图. JDBCConn.java获取数据源类: package com.gordon.jdbcconn; import java.io.InputStream; im ...
- Oracle的sql语句中关键字冲突用双引号
select distinc user from instrument where created>"TO_DATE"('2015-02-05 12:00:00', 'yyy ...
- Java 获取webapp,Root,classpath,项目等路径工具类
public class UtilPath { public static void main(String[] args) { String systemName = System.getPrope ...
- C语言写的trim()函数
C语言的标准库中缺少对字符串进行操作的trim()函数,使用起来有些不便,可以使用利用 strlen 和 isspace 函数以及指针来自己写一个. 1.strlen 函数 原型:extern int ...
- imx6 fec分析
/***************************************************************************** * imx6 fec分析 * 本文主要分析 ...
- [mysql] 查询前几条记录
From: http://www.cnblogs.com/xuxm2007/archive/2010/11/16/1878211.html SELECT * FROM table LI ...