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

  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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容