美文网首页
LeetCode 01/13/18 & 01/15/18

LeetCode 01/13/18 & 01/15/18

作者: Muama | 来源:发表于2018-01-16 13:43 被阅读0次
    1. Permutations II
     private void permutate(int[] nums, int index, List<List<Integer>> res) {
            // terminate condition
            if (index == nums.length) { 
                List<Integer> temp = new ArrayList<>();
                for (int num : nums) { 
                    temp.add(num); 
                }
                res.add(temp);
                return;
            }
            
            Set<Integer> set = new HashSet<>();
            for (int i = index; i < nums.length; i++) {
                if(set.add(nums[i])) {
                    swap(nums, i, index);
                    permutate(nums, index + 1, res);
                    swap(nums, i, index);
                }
            }
        }
    
    1. N Queens
      dfs, N层每层N个可选位置,在是否加Q的地方限制条件是不在同一行同一列,slope != +-1

    2. Spiral Matrix N*N
      剥洋葱,分四个方向分别进行递归,注意offset的使用

    reverse linkedlist in pair
    多搞一个nextnext node, 然后再进行翻转,递归
    其中翻转最后是cur = nextnext

    String Abbreviation Matching
    分别对两个str进行比对, 分两种情况
    1.如果不是数字 则直接比较
    2.如果是digit,需要把后面跟着的digit全都取出来,再跳过那么大的position
    char 不是 0-9 时
    t.charAt(ti) < '0' || t.charAt(ti) > '9'

    1. Lowest Common Ancestor of a Binary Tree
      分两种情况
    2. 不直接隶属
      1.1 both child return not null, then return root
      1.2 one of the child return not null, return the not null one
      1.3 both return null, return null
      2.直接隶属
      2.1 both return null, return null
      2.2 one of the child return not null, return the not null one
      合并以上情况 只需要做1.2 1.3

    相关文章

      网友评论

          本文标题:LeetCode 01/13/18 & 01/15/18

          本文链接:https://www.haomeiwen.com/subject/vxahoxtx.html