2018-07-04

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<List<Integer>> combinationSum(int[] candidates, int target) {
        
    List<List<Integer>> results = new ArrayList<>();
      List<Integer> oneSol = new ArrayList<>();
      dfs(0,  candidates, target, 0, results, oneSol);
      return results;
        
    }
    private void dfs(int level,int[] candidates, int remaining, int start, List<List<Integer>> results, List<Integer> 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<List<Integer>> combine(int n, int k) {
       
    List<List<Integer>> results = new ArrayList<>();
      List<Integer> oneSol = new ArrayList<>();
      dfs(0, k, n, 1, results, oneSol);
      return results;
        
    }
    private void dfs(int level, int k, int n, int start, List<List<Integer>> results, List<Integer> 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<Pair> 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<List<Integer>> combinationSum3(int k, int n) {
      List<List<Integer>> results = new ArrayList<>();
      List<Integer> oneSol = new ArrayList<>();
      dfs(0, k, n, 1, results, oneSol);
      return results;
        
    }
    private void dfs(int level, int k, int remaining, int start, List<List<Integer>> results, List<Integer> 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;
    }
}



©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,591评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,448评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,823评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,204评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,228评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,190评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,078评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,923评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,334评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,550评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,727评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,428评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,022评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,672评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,826评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,734评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,619评论 2 354

推荐阅读更多精彩内容

  • 私教专业基础课程 运动解剖学 运动营养学 运动生理学 特殊情况训练 围度测量 抗阻力训练的意义 练前准备活动 训练...
    火鸟健身阅读 95评论 0 0
  • Compassion Gratitude Forgiveness Future Dreaming Perfect ...
    识途的小马阅读 479评论 0 1
  • 尚俊平,焦点网络中级,坚持分享532天,2017年9月12日周二 个体心理学的重大发现之一,自卑情结似乎已经驰名于...
    32598db751bb阅读 237评论 0 2
  • 退休厨师爆料餐馆内幕:这些菜最好不要点 吃货转需,拌面神器,8款神级酱料的做法!酱料,拌面的灵魂! 9 � 9 �...
    学徒晓成阅读 178评论 0 1
  • 中国的木工技术,自鲁班以来,能工巧匠频出。随着工业革命的兴起,很多板式家具实行了机械化。红木家具却由于选材、省料、...
    古镇老戴阅读 818评论 0 0