- 浏览: 185599 次
- 性别:
- 来自: 济南
文章分类
最新评论
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]
输出所有可能的结果。我们采用分治法,当遇到字符‘+’ ,’-‘ ,’*‘ 时就把字符串分为两段,然后递归求出左半部分可右半部分能有的结果,然后在将左半部分和有半部分根据当前的字符求出当前可能的结果。当for循环结束就返回结果。代码如下:
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]
输出所有可能的结果。我们采用分治法,当遇到字符‘+’ ,’-‘ ,’*‘ 时就把字符串分为两段,然后递归求出左半部分可右半部分能有的结果,然后在将左半部分和有半部分根据当前的字符求出当前可能的结果。当for循环结束就返回结果。代码如下:
public class Solution { public List<Integer> diffWaysToCompute(String input) { List<Integer> list = new ArrayList<Integer>(); if(input == null || input.length() == 0) return list; if(input.indexOf("+") < 0 && input.indexOf("-") < 0 && input.indexOf("*") < 0) { list.add(Integer.parseInt(input)); return list; } for(int i = 0; i < input.length(); i++) { char tem = input.charAt(i); if(tem == '+' || tem == '-' || tem == '*') { List<Integer> left = diffWaysToCompute(input.substring(0, i)); List<Integer> right = diffWaysToCompute(input.substring(i + 1, input.length())); for(int m : left) for(int n : right) { switch(input.charAt(i)) { case('+') : list.add(m + n); break; case('-') : list.add(m - n); break; case('*') : list.add(m * n); break; } } } } return list; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 271Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 275You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 392Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 380Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 506Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 573Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 484Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 674Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 478The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 436Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 585Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 595Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 432All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 909Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 936Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 609Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 704Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 869Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 795You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 733For a undirected graph with tre ...
相关推荐
java java_leetcode题解之Different Ways to Add Parentheses.java
* Different Ways to Add Parentheses:该题目要求计算将一个字符串分解成不同的括号表达式的方式数。解决该题目需要使用分治法,递归地计算每个子字符串的括号表达式,最后合并结果。 本资源摘要信息涵盖了算法...
本资源“The-parentheses-matching-the-test.rar”提供了一个C语言实现的括号匹配检验,旨在帮助学习者深入理解和应用C语言以及相关的数据结构。 括号匹配的核心在于检查一个字符串中的开括号(如"(", "[", "{")...
Improvements to nested parentheses Case statements with comments now align correctly Now adds a space before aliases following function calls Added "Place BEGIN keyword on new line" option. You can ...
java java_leetcode题解之Generate Parentheses.java
Improvements to nested parentheses Case statements with comments now align correctly Now adds a space before aliases following function calls (forum) Added "Place BEGIN keyword on new line" option ...
java java_leetcode题解之Longest Valid Parentheses.java
c c语言_leetcode 0020_valid_parentheses.zip
java入门 java_leetcode题解之22_Generate_Parentheses
The book explains the different ways to declare integers, including the use of modifiers like `short`, `long`, and `long long`. It also discusses the range of values that can be stored and how to ...
java java_leetcode题解之Maximum Nesting Depth of Two Valid Parentheses
js js_leetcode题解之22-generate-parentheses.js
js js_leetcode题解之20-valid-parentheses.js
c语言入门 C语言_leetcode题解之22-generate-parentheses.c
c语言入门 C语言_leetcode题解之20-valid-parentheses.c
js js_leetcode题解之32-longest-valid-parentheses.js
c语言入门 C语言_leetcode题解之32-longest-valid-parentheses.c
(add-to-list 'load-path "/path/to/highlight-parentheses.el") ;; 加载并启用highlight-parentheses (require 'highlight-parentheses) ``` 然后,当你启动Emacs或者重新加载配置后,`highlight-parentheses.el`...