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的更多相关文章

  1. leetcode Submission Details

    代码: #include<iostream> #include<vector> using namespace std; struct ListNode { int val; ...

  2. Submission Details [leetcode] 算法的改进

    最先看到这一题,直觉的解法就是len从1到s1.size()-1,递归调用比較s1和s2长度为len的子串是否相等.以及剩余部分是否相等. 将s1分成s1[len + rest],分别比較s2[len ...

  3. 【leetcode】Submission Details

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  4. Submission Details

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  5. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  6. [leetCode][012] Two Sum (1)

    [题目]: Given an array of integers, find two numbers such that they add up to a specific target number ...

  7. [leetCode][016] Add Two Numbers

    [题目]: You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  8. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  9. [sqoop1.99.6] 基于1.99.6版本的一个小例子

    1.创建mysql数据库.表.以及测试数据mysql> desc test;+-------+-------------+------+-----+---------+------------- ...

随机推荐

  1. kubernetes namespace

    namespace 通常用来划分项目 默认kubectl命令 操作的namespace是default kube-system是k8s的系统组件namespaces 切换namespaces: 查看配 ...

  2. vsftp移植(待续)

    1. 下载sftp包,解压.如vsftpd-2.3.5.tar.gz2. 编译 进入目录后,修改交叉编译工具(vi Makefile) CC      = arm-none-linux-gnueabi ...

  3. ​网页图表Highcharts实践教程标之加入题副标题版权信息

    ​网页图表Highcharts实践教程标之加入题副标题版权信息 Highcharts辅助元素 辅助元素图表的非必要元素.如标题.版权信息.标签.加载动态.它们不和图表数据发生关联,仅仅是额外说明一些基 ...

  4. ansible 变量传递到include

    Task Include Files And Encouraging Reuse 假设您想在play或playbook中重复使用任务列表. 您可以使用include文件来执行此操作. 使用includ ...

  5. 一个小bug

    如果提交表单给按钮一个名字,就会报错... <html> <body> <form action="{:U('Index/login')}" meth ...

  6. VMWare -- winscp实现windows主机和Ubuntu虚拟机之间文件复制(通过ftp协议)

    我们经常需要将本地的文件上传到远程的Ubuntu 14.04服务器上,或者把远程Ubuntu 14.04服务器上的文件下载到本地,这就需要用到vsftpd来搭建FTP服务,现在介绍一下如何在Ubunt ...

  7. iOS 开发系列:CoreData Object 变成 Fault 的一种方式

    @quote: 近来一直与 CoreData 打交道.这是一个架构庞大.学习曲线比較陡峭的 iOS 组件,每次遇到问题都会对其有新的认识. 这次就仅仅讲一点,关于错误认知 Object(NSManag ...

  8. 简单html弹窗

    css: <style type="text/css"> .moneyrecord { display:none; border:0.5em solid #00AAEE ...

  9. jQuery 检查某个元素在页面上是否存在实例代码

    用jQuery检查某个元素在网页上是否存在时,应该根据获取元素的长度来判断,代码如下: if($("#tt").length > 0) {   //元素存在时执行的代码 }  ...

  10. DLL文件的使用

    一. 动态链接库 什么是动态链接库?DLL三个字母对于你来说一定很熟悉吧,它是Dynamic Link Library 的缩写形式,动态链接库 (DLL) 是作为共享函数库的可执行文件.动态链接提供了 ...