Q1 :leetcode 216,
Q2:739.
Q3;513,
Q4:733,
Q5:77.
Q6:39,
Q7: 40
Q8: 264 PQ解法
Q:9 203. Remove Linked List Elements
Q10:leetcode 237
//Q39
class Solution {
public List> combinationSum(int[] candidates, int target) {
List> results = new ArrayList<>();
List oneSol = new ArrayList<>();
dfs(0, candidates, target, 0, results, oneSol);
return results;
}
private void dfs(int level,int[] candidates, int remaining, int start, List> results, List oneSol) {
if (remaining <= 0 ) {
if (remaining == 0) {
results.add(new ArrayList<>(oneSol));
}
return;
}
for (int i = start; i < candidates.length; i++) {
oneSol.add(candidates[i]);
dfs(level + 1,candidates, remaining - candidates[i], i, results, oneSol);
//oneSol.remove(i); why NPE?
oneSol.remove(oneSol.size() - 1);
}
}
}
//Q77
class Solution {
public List> combine(int n, int k) {
List> results = new ArrayList<>();
List oneSol = new ArrayList<>();
dfs(0, k, n, 1, results, oneSol);
return results;
}
private void dfs(int level, int k, int n, int start, List> results, List oneSol) {
if (level == k ) {
results.add(new ArrayList<>(oneSol));
return;
}
for (int i = start; i <= n; i++) {
oneSol.add(i);
dfs(level + 1, k, n, i+ 1, results, oneSol);
oneSol.remove(oneSol.size() - 1);
}
}
}
//Q739
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
if (temperatures == null || temperatures.length == 0) {
return null;
}
Deque stack = new ArrayDeque<>();
int n = temperatures.length;
int[] result = new int[n];
for (int i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && stack.peekFirst().temp <= temperatures[i]) {
stack.pollFirst();
}
result[i] = stack.isEmpty() ? 0 : ( stack.peekFirst().idx - i ) ;
stack.offerFirst(new Pair(temperatures[i], i));
}
return result;
}
}
class Pair {
int temp;
int idx;
public Pair (int temp, int idx) {
this.temp = temp;
this.idx = idx;
}
}
//Q513
class Solution {
public int findBottomLeftValue(TreeNode root) {
return helper(root, root.val).val;
}
private Pair helper(TreeNode root, int parentVal) { //returns the left most int at cur root
if (root == null) { //core: root == null what val to return
return new Pair(0, parentVal);
}
Pair left = helper(root.left, root.val);
Pair right = helper(root.right, root.val);
if (left.height >= right.height) {
left.height++;
return left;
} else {
right.height++;
return right;
}
}
}
class Pair {
int height;
int val;
public Pair (int height, int val){
this.height = height;
this.val = val;
}
}
////Q216
class Solution {
public List> combinationSum3(int k, int n) {
List> results = new ArrayList<>();
List oneSol = new ArrayList<>();
dfs(0, k, n, 1, results, oneSol);
return results;
}
private void dfs(int level, int k, int remaining, int start, List> results, List oneSol) {
if (level == k ) {
if (remaining == 0) {
results.add(new ArrayList<>(oneSol));
}
return;
}
for (int i = start; i <= 9; i++) {
oneSol.add(i);
dfs(level + 1, k, remaining - i, i + 1, results, oneSol);
oneSol.remove(oneSol.size() - 1);
}
}
}
//Q:733
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
if (newColor == image[sr][sc]) { //if没有这个条件 会stackoverflow
return image;
}
dfs(image, sr, sc, image[sr][sc], newColor);
return image;
}
private void dfs (int[][] image, int x, int y, int oldCol, int newCol ) {
int n = image.length;
int m = image[0].length;
if (x < 0 || x >= n || y < 0 || y >= 0) {
return;
}
if (image[x][y] == oldCol) {
image[x][y] = newCol;
dfs(image, x + 1, y , oldCol, newCol);
dfs(image, x - 1, y , oldCol, newCol);
dfs(image, x , y + 1, oldCol, newCol);
dfs(image, x , y - 1, oldCol, newCol);
}
return;
}
//
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
String tem = strs[0];
for (int i = 1; i < strs.length; i++ ) {
tem = helper(tem, strs[i]);
if (tem.length() == 0) {
return "";
}
}
return tem;
}
private String helper(String a, String b) {
int i = 0;
int j = 0;
while (i < a.length() && j < b.length()) {
if (a.charAt(i) == b.charAt(j)) {
i++;
j++;
} else {
return a.substring(0, i);
}
}
return i == a.length() ? a : b;
}
}