From the pre-order array, we know that first element is the root. We can find the root in in-order array. Then we can identify the left and right sub-trees of the root from in-order array. Using the length of left sub-tree, we can identify left and right sub-trees in pre-order array. Recursively, we can build up the tree.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ classSolution { public TreeNode buildTree(int[] preorder, int[] inorder) { int preStart=0; int preEnd=preorder.length-1; intinStart=0; intinEnd= inorder.length-1; return buildTree(preorder,preStart,preEnd,inorder,inStart,inEnd); }
public TreeNode buildTree(int[] preorder, int preStart,int preEnd, int[] inorder, int inStart, int inEnd) { if (preStart>preEnd || inStart>inEnd) returnnull;
Instead of thinking about filling a matrix, think in terms of the recurrence relation. I have seen a lot of people who try to think dynamic programming problems in terms of filling a table/array. But this approach is problematic. For example, if the state representation has more than two dimensions, then this approach does not help much.
The essence of dynamic programming is the idea of a state space and a recurrence relation between states. Once you figure that out for any problem, implementation is straightforward.
参考了这些,虽然还是没有完全理解,程序在leetcode上通过了,程序其实就短短的几行,The code has a O(NC) pseudo-polynomial complexity where N is the amount and C the number of input coins。
这题是关于表达式解析的,2007年学习数据结构曾经实现过,思路是把中缀表达式转换为后缀表达式,再结合栈这种数据结构计算后缀表达式,就拿来执行下,结果LeetCode test case 1*2-3/4+5*6-7*8+9/10 报了个错 java.lang.ArithmeticException: / by zero,用StringTokenizer把操作符与操作数拆开,改了下原来的代码,最终运行通过了,但自己对其中的机理还是有点知其然而不知其所以然。
classSolution { publicintpostfixCalculator(List<String> postfixExpr) { Stack<Integer> stk = newStack<Integer>(); for (inti=0; i < postfixExpr.size(); i++) { Strings= postfixExpr.get(i); int a, b, x; switch (s) { case"*": if (!stk.isEmpty()) { a = stk.pop().intValue(); b = stk.pop().intValue(); x = a * b; stk.push(newInteger(x)); } break; case"+": if (!stk.isEmpty()) { a = stk.pop().intValue(); b = stk.pop().intValue(); x = a + b; stk.push(newInteger(x)); } break; case"-": if (!stk.isEmpty()) { a = stk.pop().intValue(); b = stk.pop().intValue(); x = b - a; stk.push(newInteger(x)); } break; case"/": if (!stk.isEmpty()) { a = stk.pop().intValue(); b = stk.pop().intValue(); x = b / a; stk.push(newInteger(x)); } break; default: stk.push(newInteger(s)); break; } } intret=0; if (!stk.isEmpty()) ret = stk.pop().intValue(); return ret; }
public List<String> infix2postfix(String infixExpr) { List<String> r = newArrayList<String>(); Stack<String> stk = newStack<String>(); List<String> list = newArrayList<String>(); StringTokenizerst=newStringTokenizer(infixExpr,"[\\+\\-\\*/()]",true); while (st.hasMoreTokens()) { list.add(st.nextToken()); } for (inti=0; i < list.size(); i++) { Strings= list.get(i); switch (s) { case"*": case"/": while (!stk.isEmpty() && (stk.peek().equals("*") || stk.peek().equals("/"))) { r.add( stk.pop() ); } stk.push(s); break; case"+": case"-": while (!stk.isEmpty() && !stk.peek().equals("(")) { r.add( stk.pop() ); } stk.push(s); break; case"(": stk.push(s); break; case")": while (!stk.isEmpty() && !stk.peek().equals("(")) { r.add(stk.pop()); } if (stk.peek().equals("(")) stk.pop(); break; default: r.add(s); } }
UsingEnglish.com was established in 2002 and is a general English language site specialising in English as a Second Language (ESL). https://www.usingenglish.com/
English Language & Usage Stack Exchange is a question and answer site for linguists, etymologists, and serious English language enthusiasts. https://english.stackexchange.com/