241. Different Ways to ADD parentheses

Source: Internet
Author: User

Topic:

Given A string of numbers and operators, return all possible results from computing all the different possible ways to Gro Up numbers and operators. The valid operators + are, - and * .


Example 1

Input: "2-1-1" .

((2-1)-1) = 0 ((1-1)) = 2

Output:[0, 2]


Example 2

Input:"2*3-4*5"

((4*5)) =-34 ((2*3)-(4*5)) = 14 ((3-4)) = 10 (((3-4))) =-10 (((2*3) 4) = 10

Output:[-34, -14, -10, -10, 10]

Links: http://leetcode.com/problems/different-ways-to-add-parentheses/

Exercises

This is said to be different Ways to add parentheses, in fact, the meaning is to ignore the operator's priority to calculate the formula. Our solution is: In the case of valid, as long as the operator is encountered, we calculate the number on the left and the right, and then according to the operator to get the result. When looking at discuss, I found Stefan Pochmann the solution of the great God ... One line, rub, this dude Rubik's Cube also play very well, is said to have won the German Rubik's Cube game first, is true cow.

Time Complexity-o (3n), Space complexity-o (3n)

 Public classSolution { PublicList<integer>diffwaystocompute (String input) {if(Input = =NULL|| Input.length () = = 0)            return NewArraylist<integer>(); List<Integer> res =NewArraylist<>();  for(inti = 0; I < input.length (); i++) {            Charc =Input.charat (i); if(c = = ' + ' | | c = = ' * ' | | c = = '-') {List<Integer> L1 = diffwaystocompute (input.substring (0, i)); List<Integer> L2 = Diffwaystocompute (input.substring (i + 1));  for(intx:l1) {                     for(inty:l2) {                        if(c = = ' + ')) Res.add (x+y); Else if(c = = '-')) Res.add (x-y); ElseRes.add (x*y); }                }            }        }                if(res.size () = = 0) Res.add (integer.valueof (input)); returnRes; }}

Reference:

Https://leetcode.com/discuss/48468/1-11-lines-python-9-lines-c

Https://leetcode.com/discuss/60626/share-a-clean-and-short-java-solution

Https://leetcode.com/discuss/53566/python-easy-to-understand-solution-divide-and-conquer

Https://leetcode.com/discuss/61840/java-recursive-9ms-and-dp-4ms-solution

Https://leetcode.com/discuss/48477/a-recursive-java-solution-284-ms

Https://leetcode.com/discuss/48488/c-4ms-recursive-%26-dp-solution-with-brief-explanation

Https://leetcode.com/discuss/48494/what-is-the-time-complexity-of-divide-and-conquer-method

Https://leetcode.com/discuss/55255/clean-ac-c-solution-with-explanation

241. Different Ways to ADD parentheses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.