Wednesday, October 28, 2015

Leetcode question 241: Different ways to add parentheses

10/28/2015  

  Read the blog,

  http://fisherlei.blogspot.ca/2015/10/leetcode-different-ways-to-add.html

  http://blog.csdn.net/guanzhongshan/article/details/48086695

  and write C# code, compile it, build it, pass online judge, check in Github, and then, write a new code using memorization.

1. First step, write down C# code, compile ok.

https://github.com/jianminchen/Leetcode_C-/blob/master/241DifferentWaysToAddParentheses.cs

2. Read the most popular blog about this leetcode question through Google,
http://blog.csdn.net/sbitswc/article/details/48546421

and then, take Java code as a sample, write C# version of implementation.

https://github.com/jianminchen/Leetcode_C-/blob/master/241DifferentWaysToAddParentheses_B.cs

read the leetcode string algorithms blog (A plus):
http://blog.csdn.net/sbitswc/article/details/20429853

The leetcode question 241 is just a medium  question in difficulties.

Dec. 17, 2015
Review the solution, and then, need to write down the solution - a script to help solve the problem:
1. 1+2*3
-> read a substring from leftmost to convert it to number, for the example, read '1', integer 1, and then, visit '+' stop,
-> now, let us use recursive function call to get the first part of substring before '+', and get second part of substring after '+', and then, two lists, each one of list1 will operate '+' with each one of list2, add into return list.

Not convincing, try again:
use binary tree to model this problem:
http://juliachencoding.blogspot.ca/2015/12/oo-principle-solid-open-close-principle.html

So, get the root node - operator, and then, deal with left child, right child, and root node does the evaluate function.







No comments:

Post a Comment