第二章 模拟算法和字符串处理技巧
1 Sliding Window Average from Data Stream
public class MovingAvage { int id, size; double[] sum; public MovingAvage(int s) { size = s; sum = new double[100000]; } public double add(int num) { id++; sum[id] = sum[id - 1] + num; if (id - size >= 0) { return (sum[id] - sum[id - size]) / size; } else { return sum[id] / id; } } }
使用滚动数组 1 缩减数组 2 写mod函数 3 所有用到数组的地方改变量
public class MovingAvage { int id, size; double[] sum; public MovingAvage(int s) { size = s; sum = new double[size + 1]; // 1 } int mod(int x) { // 2 return x % (size + 1); } // 3 public double add(int num) { id++; sum[mod(id)] = sum[mod(id - 1)] + num; if (id - size >= 0) { return (sum[mod(id)] - sum[mod(id - size)]) / size; } else { return sum[mod(id)] / id; } } }
2 Edit Distance II
public boolean isOneEditDistance(String s, String t) { // write your code here if (s.length() > t.length()) { return isOneEditDistance(t, s); } int diff = t.length() - s.length(); if (diff > 1) { return false; } if (diff == 0) { int con = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != t.charAt(i)) { con++; } } return con == 1; } if (diff == 1) { for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != t.charAt(i)) { return s.substring(i).equals(t.substring(i + 1)); } } } return true; }
3 Read Characters From File - multiple calls
public class MovingAvage { char[] buffer = new char[4]; int head, tail; public int read(char[] buf, int n) { int i = 0; while (i < n) { if (head == tail) { head = 0; tail = read4(buffer); if (tail == 0) { break; } } while (i < n && head < tail) { buf[i++] = buffer[head++]; } } return i; } }
4 Strings Serialization
public class MovingAvage { String encode(List<String> list) { StringBuilder sb = new StringBuilder(); for (String s : list) { for (char c : s.toCharArray()) { if (c == ':') { sb.append("::"); } else { sb.append(c); } } sb.append(":;"); } return sb.toString(); } List<String> decode(String str) { List<String> res = new ArrayList<>(); char[] sc = str.toCharArray(); StringBuilder item = new StringBuilder(); int i = 0; while (i < sc.length) { if (sc[i] == ':') { if (sc[i + 1] == ';') { res.add(item.toString()); item = new StringBuilder(); i += 2; } else { item.append(sc[i + 1]); i += 2; } } else { item.append(sc[i]); i++; } } return res; } }
5 System Longest File Path
public int lengthLongestPath(String input) { // write your code here if (input.length() == 0) { return 0; } int[] level = new int[input.length() + 1]; int res = 0; for (String line : input.split("\n")) { int l = line.lastIndexOf("\t") + 2; int len = line.length() - (l - 1); if (line.contains(".")) { res = Math.max(res, level[l - 1] + len); } else { level[l] = level[l - 1] + len + 1; } } return res; }
6 Roman to Integer
public int romanToInt(String s) { if (s.length() == 0) { return 0; } char[] cs = s.toCharArray(); int res = toInt(cs[0]); for (int i = 1; i < cs.length; i++) { res += toInt(cs[i]); if (toInt(cs[i - 1]) < toInt(cs[i])) { res -= toInt(cs[i - 1]) * 2; } } return res; } int toInt(char c) { switch(c) { case 'I': return 1; case 'V': return 5; case 'X': return 10; case 'L': return 50; case 'C': return 100; case 'D': return 500; case 'M': return 1000; } return 0; }
7 Integer to Roman
public static String intToRoman(int num) { String M[] = {"", "M", "MM", "MMM"}; String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10]; }
8 identify celebrity
int findCelebrity(int n) { int res = 0; for (int i = 1; i < n; i++) { if (know(res, i)) { res = i; } } for (int i = 0; i < n; i++) { if (res != i && know(res, i)) { return -1; } if (res != i && !know(i, res)) { return -1; } } return res; }
的