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沒(méi)有這個(gè)條件 會(huì)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;
    }
}



?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末扭吁,一起剝皮案震驚了整個(gè)濱河市莺掠,隨后出現(xiàn)的幾起案子畜份,更是在濱河造成了極大的恐慌,老刑警劉巖安岂,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件驾荣,死亡現(xiàn)場(chǎng)離奇詭異外构,居然都是意外死亡普泡,警方通過(guò)查閱死者的電腦和手機(jī)播掷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)撼班,“玉大人歧匈,你說(shuō)我怎么就攤上這事∨猷遥” “怎么了件炉?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)矮湘。 經(jīng)常有香客問(wèn)我斟冕,道長(zhǎng),這世上最難降的妖魔是什么缅阳? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任磕蛇,我火速辦了婚禮,結(jié)果婚禮上十办,老公的妹妹穿的比我還像新娘秀撇。我一直安慰自己,他們只是感情好向族,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布呵燕。 她就那樣靜靜地躺著,像睡著了一般件相。 火紅的嫁衣襯著肌膚如雪再扭。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,457評(píng)論 1 311
  • 那天夜矗,我揣著相機(jī)與錄音泛范,去河邊找鬼。 笑死侯养,一個(gè)胖子當(dāng)著我的面吹牛敦跌,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼柠傍,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼麸俘!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起惧笛,我...
    開(kāi)封第一講書(shū)人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤从媚,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后患整,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體拜效,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年各谚,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了紧憾。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡昌渤,死狀恐怖赴穗,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情膀息,我是刑警寧澤般眉,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站潜支,受9級(jí)特大地震影響甸赃,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜冗酿,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一埠对、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧已烤,春花似錦鸠窗、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至裕循,卻和暖如春臣嚣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背剥哑。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工硅则, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人株婴。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓怎虫,卻偏偏與公主長(zhǎng)得像暑认,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子大审,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360

推薦閱讀更多精彩內(nèi)容

  • 私教專業(yè)基礎(chǔ)課程 運(yùn)動(dòng)解剖學(xué) 運(yùn)動(dòng)營(yíng)養(yǎng)學(xué) 運(yùn)動(dòng)生理學(xué) 特殊情況訓(xùn)練 圍度測(cè)量 抗阻力訓(xùn)練的意義 練前準(zhǔn)備活動(dòng) 訓(xùn)練...
    火鳥(niǎo)健身閱讀 95評(píng)論 0 0
  • Compassion Gratitude Forgiveness Future Dreaming Perfect ...
    識(shí)途的小馬閱讀 482評(píng)論 0 1
  • 尚俊平蘸际,焦點(diǎn)網(wǎng)絡(luò)中級(jí),堅(jiān)持分享532天徒扶,2017年9月12日周二 個(gè)體心理學(xué)的重大發(fā)現(xiàn)之一粮彤,自卑情結(jié)似乎已經(jīng)馳名于...
    32598db751bb閱讀 241評(píng)論 0 2
  • 退休廚師爆料餐館內(nèi)幕:這些菜最好不要點(diǎn) 吃貨轉(zhuǎn)需,拌面神器姜骡,8款神級(jí)醬料的做法导坟!醬料,拌面的靈魂圈澈! 9 ? 9 ?...
    學(xué)徒曉成閱讀 182評(píng)論 0 1
  • 中國(guó)的木工技術(shù)士败,自魯班以來(lái)闯两,能工巧匠頻出褥伴。隨著工業(yè)革命的興起谅将,很多板式家具實(shí)行了機(jī)械化。紅木家具卻由于選材重慢、省料饥臂、...
    古鎮(zhèn)老戴閱讀 826評(píng)論 0 0