- 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);
}
}
}
N Queens
dfs, N層每層N個(gè)可選位置,在是否加Q的地方限制條件是不在同一行同一列敷矫,slope != +-1Spiral Matrix N*N
剝洋蔥础芍,分四個(gè)方向分別進(jìn)行遞歸,注意offset的使用
reverse linkedlist in pair
多搞一個(gè)nextnext node神汹, 然后再進(jìn)行翻轉(zhuǎn)庆捺,遞歸
其中翻轉(zhuǎn)最后是cur = nextnext
String Abbreviation Matching
分別對兩個(gè)str進(jìn)行比對, 分兩種情況
1.如果不是數(shù)字 則直接比較
2.如果是digit慎冤,需要把后面跟著的digit全都取出來疼燥,再跳過那么大的position
char 不是 0-9 時(shí)
t.charAt(ti) < '0' || t.charAt(ti) > '9'
- Lowest Common Ancestor of a Binary Tree
分兩種情況 - 不直接隸屬
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