一道題根本不會,抄答案過了。一道自己寫,莫名其妙出現了不會的bug。最后交了暴力解,過了5/7。估計要跪。
總結:
缺點:做過的不熟練。沒做過的題不會。一個陌生的小bug也de不出來。
措施:多總結還是有好處的。堅持刷tag題有必要,絕對能慢慢增強實力,很重要。
指定方向的dp: 改變i - 1 j - 1就行了 dp[j] = Math.min(dp[i+1][j-1], dp[i+1][j], dp[i+1][j+1]) + matrix[j];
加下一行不行,不能處理第一行的時候。該成每次加上一行,這樣第一行就可以被初始化了。lc 64。
public class Solution { /* * @param grid: a list of lists of integers * @return: An integer, minimizes the sum of all numbers along its path */ public int minPathSum(int[][] grid) { //corner case if (grid == null || grid.length == 0) { return -1; } if (grid[0] == null || grid[0].length == 0) { return -1; } //intialization int m = grid.length; int n = grid[0].length; int[][] f = new int[m][n]; f[0][0] = grid[0][0];// for (int i = 1; i < m; i++) { f[i][0] = f[i - 1][0] + grid[i][0]; } for (int j = 1; j < n; j++) { f[0][j] = f[0][j - 1] + grid[0][j]; } //top down for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { f[i][j] = grid[i][j] + Math.min(f[i - 1][j],f[i][j - 1]); } } //result return f[m - 1][n - 1]; } }
[學生遲到問題]
Q2(MEDIUM): greatest lateness,不知道地里有沒有這題的面經,給一個2d-vector的string,每一行格式是 [日期, 姓名, 開課時間, 到課時間],輸出整個輸入里累計“相對”遲到時間最長的同學的姓名, 如果學生早於開課時間absolute lateness算0,相對遲到時間是說同學當天絕對遲到時間和當天所有人絕對遲到時間的平均值的差,如果小於的話就算0,如果結果一樣按姓名alphabetical輸出
不知道存兩個哈希表的好處
map.keySet().size()用於找size
每次i都要重新存聲明為int
自己造輪子,寫了半天,終於貌似好像過了:

// package whatever; // don't place package name! import java.io.*; import java.util.*; import java.lang.*; class Solution { //method, need parameter, there is return public String latestStudent(String[][] students) { //initialization HashMap<String, Integer> dayToTime = new HashMap<String, Integer>(); HashMap<String, Integer> nameToTime = new HashMap<String, Integer>(); HashMap<String, Integer> dayToAverageLateTime = new HashMap<String, Integer>(); int count = 0; //put time to map for (int i = 1; i < students.length; i++) { if (students[i][0] == students[i - 1][0]) { count++; }else { int averageLateTime = dayToTime.get(students[i - 1][0]) / count; count = 0; dayToAverageLateTime.put(students[i - 1][0], averageLateTime); break; } int time = Integer.valueOf(students[i][3]) - Integer.valueOf(students[i][2]); if (time > 0) { dayToTime.put(students[i][0], time); }else dayToTime.put(students[i][0], 0); } int countOfAverage = dayToAverageLateTime.keySet().size(); int max = 0; //put student into the map, and find the max time for (int i = 0; i < students.length; i++) { int averageLateTime = dayToAverageLateTime.getOrDefault(students[i][0], 0); int absoluteTime = Integer.valueOf(students[i][3]) - Integer.valueOf(students[i][2]); if ((absoluteTime - averageLateTime)> 0) { nameToTime.put(students[i][1], absoluteTime - averageLateTime); max = Math.max(max, absoluteTime - averageLateTime); } } //find the max student int countOfStudents = nameToTime.keySet().size(); for (String name : nameToTime.keySet()) { if (nameToTime.get(name) == max) System.out.println("max = " + max); return name; } return null; } } class MyCode { public static void main (String[] args) { String[][] students = {{"1", "A", "540", "570"}, {"1", "B", "540", "543"}, {"1", "C", "540", "530"}, {"2", "A", "540", "580"}, {"2", "B", "540", "580"}, {"2", "C", "540", "595"}}; Solution answer = new Solution(); System.out.println("name = " + answer.latestStudent(students)); } }
右端對齊:sb.append("\n");
// package whatever; // don't place package name! import java.io.*; class MyCode { public static void main (String[] args) { String str1 = "abc"; String str2 = "def"; String str3 = str1 + "\n" + str2; System.out.println(str3); } }
\r是把前面的刪掉
// package whatever; // don't place package name! import java.io.*; class MyCode { public static void main (String[] args) { String str1 = "abc"; String str2 = "def"; String str3 = "gji"; String str4 = "jkl"; String str = str1 + "\r" + str2 + "\r" + str3 + "\r" + str4; System.out.println(str); } }
轉大寫要用.toUpperCase()
學生填志願問題:可以用 comparable結構
class Student implements Comparable<Student>{ int score; int index; public Student(int score, int index) { this.score = score; this.index = index; } @Override public int compareTo(Student o) { if(this.score - o.score == 0) { return this.index - o.index; } // TODO Auto-generated method stub return this.score-o.score; } }
慈善數組每次分錢給最少的:
用pq,然后也可以用比較結構
class Orgnization implements Comparable<Orgnization>{ String name; double amount; public Orgnization(String name) { this.name = name; this.amount = 0; } @Override public int compareTo(Orgnization o) { if(this.amount - o.amount == 0) { return this.name.compareTo(o.name); } return (int) (this.amount - o.amount); }
有符號的算式版reverse words in a string:用一個變量isnumber來控制,true就append
public class ReverseAlgebraExpression { public static void main(String[] args) { String test1 = "1*2.4+9.6-23.89"; String answer1 = "23.89-9.6+2.4*1"; String test2 = "1*2.4+-9.6-23.89"; String answer2 = "23.89--9.6+2.4*1"; String test3 = "-1*2.4+-9.6-23.89"; String answer3 = "23.89--9.6+2.4*-1"; test1(test1, answer1, 1); test1(test2, answer2, 2); test1(test3, answer3, 3); // String t1 = "My keyboard is broken!"; // String a1 = "My keyboRD IS BROKEN!"; // String t2 = "\"Baa, Baa!\" said the sheep"; // String a2 = "\"B, B!\" sID THE SHEEP"; // test2(t1, a1, 1); // test2(t2, a2, 2); } public static void test1(String a, String b, int i) { String output = null; output = rae(a); if(b.equals(output)) { System.out.println("Passed case "+i); } else { System.out.println("Incorrect "+i+": " + output); } } public static void test2(String a, String b, int i) { String output = null; output = capLock(a); if(b.equals(output)) { System.out.println("Passed case "+i); } else { System.out.println("Incorrect "+i+": " + output); } } public static String rae(String expression) { boolean isNumber = true; StringBuilder output = new StringBuilder(); if(expression.length() == 0) return output.toString(); StringBuilder buffer = new StringBuilder(); buffer.append(expression.charAt(0)); for(int i = 1; i<expression.length(); i++) { char cur = expression.charAt(i); if(isNumber && (Character.isDigit(cur) || cur == '.')) { buffer.append(cur); continue; } if(isNumber && (cur=='+' || cur == '-' || cur == '*' || cur == '/')) { output.insert(0, buffer); buffer = new StringBuilder(); buffer.append(cur); isNumber = false; continue; } if(!isNumber && (cur=='+' || cur == '-' || cur == '*' || cur == '/')) { output.insert(0, buffer); buffer = new StringBuilder(); buffer.append(cur); isNumber = true; continue; } if(!isNumber && Character.isDigit(cur)) { output.insert(0, buffer); buffer = new StringBuilder(); buffer.append(cur); isNumber = true; } } output.insert(0, buffer); return output.toString(); } public static String capLock(String input) { boolean capLock = false; StringBuilder output = new StringBuilder(); for(char c: input.toCharArray()) { if(c == 'a' || c == 'A') { capLock = !capLock; continue; } if(capLock) { output.append(Character.toUpperCase(c)); } else { output.append(c); } } return output.toString(); } }
4 Whole Minute Dilemma有圖
給一串歌曲的時間,求里面有多少 pair 可以相加變成整分鍾
例如[40, 20, 60] 結果就是 1,必須是 pair,60 自己不能算一個. Waral 博客有更多文章,
我的理解是同一首歌不能重復組成 pair 的
思路是把 時間%60 和 出現的次數 作為鍵值對存進到一個 map 里面 然后遍歷 map 找互補的:
就是twosum的hashmap的變形
世界杯概率: 保留兩位小數
String a = String.format("%.2f", result) 一般只能長小數轉為短小數
pq的寫法:用comparator
PriorityQueue<Map.Entry<Character, Integer>> pq = new PriorityQueue( new Comparator<Map.Entry<Character, Integer>>() { public int compare(Map.Entry<Character, Integer> a, Map.Entry<Character, Integer> b) { return b.getValue() - a.getValue(); } } );
getOrDefault(Object,V)允許調用者在代碼語句中規定獲得在map中符合提供的鍵的值,否則在沒有找到提供的鍵的匹配項的時候返回一個“默認值”。