关于divide and conquer的两道Leetcode

来源:互联网 发布:计步器软件哪个好用 编辑:程序博客网 时间:2024/06/06 09:01
贴上两道感觉很像的leetcode题目,第一道并不是自己独立写出来的,但是第二道因为对第一道的总结而一遍AC,开心~!
返回一个集合时,要比较注意arraylist们都是在哪里声明的,在哪里添加的。

Unique Binary Search Trees II
Question:Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.
思路: 函数的作用是构建从1-N的所有BST树,把这些树的根节点存在一个ARRAYLIST里。
他的递归思想是,构建1-N的所有=构建以1为根节点,(1,0)为左子树的所有情况与(2,N)为右子树的所有情况的组合+构建以2为根节点,(1,1)为左子树的所有情况与(3,N)为右子树的所有情况的组合+构建以3为根节点,(1,2)为左子树的所有情况与(4,N)为右子树的所有情况的组合+……+构建以N为根节点,(1,N-1)为左子树的所有情况与(N+1,N)为右子树的所有情况的组合。
当左边>右边的值时,说明不能组成任何BST树,应该返回一个含有null的arraylist.

public class Solution {    public List<TreeNode> generateTrees(int n) {         return build(1, n);           }    public List<TreeNode> build(int low, int high) {         List<TreeNode> res = new ArrayList<TreeNode>();//这个声明的位置                 if(high < low) {res.add(null); return res;}; //这句并不太理解,开始写成了 return null; 然而不对,会造成下面代码的Nullpointerexception                         List<TreeNode> lefttree = new ArrayList<TreeNode>();         List<TreeNode> righttree = new ArrayList<TreeNode>();                 for(int i = low; i <= high; i++) {              lefttree = build(low, i - 1);              righttree = build(i + 1, high);              for(TreeNode j: lefttree)                   for(TreeNode k: righttree)                   {                        TreeNode rt = new TreeNode(i); //这个语句的地方写错了                        rt.left = j;                        rt.right = k;                        res.add(rt);                   }         }         return res;    }}

Different Ways to Add Parentheses

Question: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]

思路:采用了divide and conquer的思想,也就是第一个运算符号左边右边分别看成一个子项,两边的结果相互组合形成的集合+第二个运算符左右看成一个子项,两边的结果互相组合形成的集合+。。。最后一个运算符左右看成一个子项,两边的结果互相组合形成的集合。这些所有的集合的并集就是最后的答案咯~

public class Solution {    public List<Integer>   diffWaysToCompute(String input ) {       int len = input.length(); int j = 0;       Character[] op = new Character[len ];       Integer[] num = new Integer[len ];       int sum = 0;       for(int i = 0; i < len; i++) {               if(input .charAt(i ) == '+' ||input .charAt(i ) == '-' ||input .charAt(i ) == '*')              {                      op[ j] = input.charAt( i);                      num[ j++] = sum;                      sum = 0;              } else{sum = sum *10 + input .charAt(i ) - '0' ;}               if(i == len - 1) num [j ++] = sum ;       }       List<Integer>  a = compute( num, op, 0, j - 1);       return a;    }       public List<Integer> compute(Integer[] nums, Character[] op, int left , int right) {       List<Integer> ans = new ArrayList<Integer>();       if(right - left == 0) { ans.add( nums[ left]); return ans ;}       else{              List<Integer> a = new ArrayList<Integer>();              List<Integer> b = new ArrayList<Integer>();               for(int i = left ; i < right ; i ++){                      a = compute( nums, op, left, i);                      b = compute( nums, op, i + 1, right);                      for(Integer t : a )                            for(Integer s : b ){                                   if(op [i ] == '+' ) ans .add(t + s );                                   if(op [i ] == '-' ) ans .add(t - s );                                   if(op [i ] == '*' ) ans .add(t * s );                           }              }               return ans ;       }    }}


0 0
原创粉丝点击