---------------------------------------------------------------

本文使用方法:所有题目,只需要把标题输入lintcode就能找到。主要是简单的剖析思路以及不能bug-free的具体细节原因。

----------------------------------------------------------------

-------------------------------------------

第九周:图和搜索。

-------------------------------------------

1,-------Clone Graph(克隆图)

(1)答案和思路:首先用list来记录有哪些图,然后对于每一个节点都要新建。用map来做新旧节点的一一对应,以便于后面的建立邻结点。

  1. /**
  2. * Definition for undirected graph.
  3. * class UndirectedGraphNode {
  4. * int label;
  5. * ArrayList<UndirectedGraphNode> neighbors;
  6. * UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
  7. * };
  8. */
  9. public class Solution {
  10. /**
  11. * @param node: A undirected graph node
  12. * @return: A undirected graph node
  13. */
  14. public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
  15. if (node == null) {
  16. return null;
  17. }
  18. ArrayList<UndirectedGraphNode> nodes = new ArrayList();
  19. nodes.add(node);
  20. Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap();
  21. map.put(node, new UndirectedGraphNode(node.label));
  22. int index = 0;
  23. while (index < nodes.size()) {
  24. UndirectedGraphNode curNode = nodes.get(index++);
  25. for (int i = 0; i < curNode.neighbors.size(); i++) {
  26. if (!nodes.contains(curNode.neighbors.get(i))) {
  27. nodes.add(curNode.neighbors.get(i));
  28. map.put(curNode.neighbors.get(i), new UndirectedGraphNode(curNode.neighbors.get(i).label));
  29. }
  30. }
  31. }
  32. for (int i = 0; i < nodes.size(); i++) {
  33. UndirectedGraphNode curNode = nodes.get(i);
  34. for (int j = 0; j < curNode.neighbors.size(); j++) {
  35. map.get(curNode).neighbors.add(map.get(curNode.neighbors.get(j)));
  36. }
  37. }
  38. return map.get(node);
  39. }
  40. }

(2)一刷没有AC:思路没有处理好。

(2)二刷bug-free;

 2,-----------Toplogical Sorting(拓扑排序) 

(1)答案和思路:首先,利用map来记录入度。并且把没进去map的那些点先加入结果,因为他们入度为0.为了方便遍历,用queue来辅助遍历。

  1. /**
  2. * Definition for Directed graph.
  3. * class DirectedGraphNode {
  4. * int label;
  5. * ArrayList<DirectedGraphNode> neighbors;
  6. * DirectedGraphNode(int x) { label = x; neighbors = new ArrayList<DirectedGraphNode>(); }
  7. * };
  8. */
  9. public class Solution {
  10. /**
  11. * @param graph: A list of Directed graph node
  12. * @return: Any topological order for the given graph.
  13. */
  14. public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {
  15. ArrayList<DirectedGraphNode> result = new ArrayList();
  16. if (graph == null || graph.size() == 0) {
  17. return result;
  18. }
  19. HashMap<DirectedGraphNode, Integer> map = new HashMap();
  20. //map 用来记录那些成为了别人邻居的点作为多少个邻居,也就是他的入度是多少。
  21. for (DirectedGraphNode g : graph) {
  22. for (DirectedGraphNode n : g.neighbors) {
  23. if (map.containsKey(n)) {
  24. map.put(n, map.get(n) + 1);
  25. } else {
  26. map.put(n, 1);
  27. }
  28. }
  29. }
  30. Queue<DirectedGraphNode> queue = new LinkedList();
  31. for (DirectedGraphNode g : graph) {
  32. if (!map.containsKey(g)) {
  33. //不在map里面,说明他的度为0
  34. result.add(g);
  35. queue.add(g);
  36. }
  37. }
  38. while (!queue.isEmpty()) {
  39. DirectedGraphNode node = queue.poll();
  40. for (DirectedGraphNode n : node.neighbors) {
  41. map.put(n, map.get(n) - 1);
  42. if (map.get(n) == 0) {
  43. result.add(n);
  44. queue.add(n);
  45. }
  46. }
  47. }
  48. return result;
  49. }
  50. }

(2)一刷没过。

3,------permutation(全排列)

(1)答案和思路:就是没一个数字加进去之前,他可以放在已经有的地方的n+1个位置。不断地循环就可以了。

  1. class Solution {
  2. /**
  3. * @param nums: A list of integers.
  4. * @return: A list of permutations.
  5. */
  6. public static ArrayList<ArrayList<Integer>> permute(ArrayList<Integer> nums) {
  7. ArrayList<ArrayList<Integer>> res = new ArrayList();
  8. if (nums == null || nums.size() == 0) {
  9. return res;
  10. }
  11. ArrayList<Integer> list1 = new ArrayList();
  12. list1.add(nums.get(0));
  13. res.add(list1);
  14. for (int i = 1; i < nums.size(); i++){
  15. ArrayList<ArrayList<Integer>> res2 = new ArrayList();
  16. for (ArrayList<Integer> tmp : res) {
  17. for (int j = 0; j <= tmp.size(); j++) {
  18. ArrayList<Integer> list2 = new ArrayList(tmp);
  19. list2.add(j, nums.get(i));
  20. res2.add(list2);
  21. }
  22. }
  23. res = new ArrayList(res2);
  24. }
  25. return res;
  26. }
  27. }

(2)注意细节问题。

(3)二刷bug-free; 

4,---------permutationII(全排列)

(1)答案和思路:跟上一题的区别就只需要再加入结果之前判断一下是否已经有了。

  1. class Solution {
  2. /**
  3. * @param nums: A list of integers.
  4. * @return: A list of unique permutations.
  5. */
  6. public static ArrayList<ArrayList<Integer>> permuteUnique(ArrayList<Integer> nums) {
  7. // write your code here
  8. HashSet<ArrayList<Integer>> res = new HashSet();
  9. ArrayList<ArrayList<Integer>> ans = new ArrayList();
  10. if (nums == null || nums.size() == 0) {
  11. return ans;
  12. }
  13. ArrayList<Integer> list1 = new ArrayList();
  14. list1.add(nums.get(0));
  15. res.add(list1);
  16. for (int i = 1; i < nums.size(); i++){
  17. HashSet<ArrayList<Integer>> res2 = new HashSet();
  18. for (ArrayList<Integer> tmp : res) {
  19. for (int j = 0; j <= tmp.size(); j++) {
  20. ArrayList<Integer> list2 = new ArrayList(tmp);
  21. list2.add(j, nums.get(i));
  22. res2.add(list2);
  23. }
  24. }
  25. res = new HashSet(res2);
  26. }
  27. for (ArrayList list : res){
  28. ArrayList<Integer> list3 = new ArrayList(list);
  29. ans.add(list3);
  30. }
  31. return ans;
  32. }
  33. }

 (2)一刷bug-free;

 5,----------N Queens (n皇后问题)

(1)答案和思路:首先,就是一场不断尝试放置的过程,那么需要一个函数来判断是否能够放(不能放的条件:这一列已经放了,他不能和上面的行构成斜对角线(当行差值==列差值的时候,就是在对角线上))。技巧:columns[i] = j来记录每一行的Q放在哪里。i行放在j列。    第二步就是进行放置:放置对于第一行来说,能放N个位置,所以,for循环。放好了第一个,利用递归,开始放第二。不断的判断是否可行,直到放置行数达到N。那么就是一次合法的放置,存进list里面。

  1. import java.util.ArrayList;
  2. public class Solution {
  3. public static void main(String[] args) {
  4. System.out.println(solveNQueens(4));
  5. }
  6. public static ArrayList<ArrayList<String>> solveNQueens(int n) {
  7. ArrayList<ArrayList<String>> result = new ArrayList();
  8. int[] columns = new int[n];
  9. for (int i = 0; i < n; i++) {
  10. columns[i] = -1;
  11. }
  12. ArrayList<int[]> results = new ArrayList();
  13. placeNQueens(0, columns, results, n);
  14. for (int[] r : results) {
  15. ArrayList<String> list = new ArrayList();
  16. for (int i = 0; i < r.length; i++) {
  17. char[] c = new char[n];
  18. for (int j = 0; j < n; j++) {
  19. c[j] = '.';
  20. }
  21. c[r[i]] = 'Q';
  22. list.add(new String(c));
  23. }
  24. result.add(new ArrayList(list));
  25. }
  26. return result;
  27. }
  28. private static void placeNQueens(int row, int[] columns, ArrayList<int[]> results, int n) {
  29. if (row == n) {
  30. results.add(columns.clone());
  31. } else {
  32. for (int col = 0; col < n; col++) {
  33. if (checkValid(columns, row, col)) {
  34. columns[row] = col;
  35. placeNQueens(row + 1, columns, results, n);
  36. }
  37. }
  38. }
  39. }
  40. // columns[i] = j 表示第i行第j列放皇后
  41. private static boolean checkValid(int[] columns, int row, int column) {
  42. // 看一下跟已有的行的哪些列冲突了
  43. // i 到 row 行之间这些行已经存了东西了。columns[i]能得到存了哪些列
  44. for (int i = 0; i < row; i++) {
  45. int hasColumn = columns[i];
  46. if (hasColumn == column) {
  47. return false;
  48. }
  49. if (row - i == Math.abs(column - hasColumn)) {
  50. // 如果行之间的差值等于列之间的差值,那么在同一个对角线
  51. return false;
  52. }
  53. }
  54. return true;
  55. }
  56. }

(2)一刷没过。完全没想到怎么判断。

 6,--------Palindrome Partition I(回文区分)

(1)答案和思路:首先,拿到0到第i个字符,如果他是回文,那么存下来,然后从他到结尾,这一个字符串,也来进行0,到1,。。,i的判断。

基本思路就是从0-i是否是回文,是的话,接着处理i到结束的字符串递归:在后面的字符串里继续考虑:第一个到新的i是否是回文。如果一直这样处理,直到最后的str为空了,那么说明找到一组完整的了,因为只有是回文才会进入递归。这里就是递归结束之后,要remove掉list的最后一个,这是因为:假如,0-i是,但是后来一直处理结束了都不是,那么这就不是一组合格的,那么递归结束,这个值必须出局。想法如果全部都是,那么一层一层往外走,也需要list一个一个删除。

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class Solution {
  4. public static void main(String[] args) {
  5. System.out.println(partition("aaa"));
  6. }
  7. public static List<List<String>> partition(String s) {
  8. List<List<String>> result = new ArrayList();
  9. if (null == s || s.length() == 0) {
  10. return result;
  11. }
  12. List<String> list = new ArrayList();
  13. calResult(result, list, s);
  14. return result;
  15. }
  16. public static void calResult(List<List<String>> result, List<String> list, String str) {
  17. if (str == null || str.length() == 0) {
  18. // ERROR : result.add(list);
  19. result.add(new ArrayList(list));
  20. }
  21. for (int i = 1; i <= str.length(); i++) {
  22. String subStr = str.substring(0, i);
  23. if (isPalindrome(subStr)) {
  24. list.add(subStr);
  25. calResult(result, list, str.substring(i));
  26. list.remove(list.size() - 1);
  27. }
  28. }
  29. }
  30. public static boolean isPalindrome(String s) {
  31. if (null == s || s.length() == 0) {
  32. return false;
  33. }
  34. int i = 0;
  35. int j = s.length() - 1;
  36. while (i < j) {
  37. if (s.charAt(i) != s.charAt(j)) {
  38. return false;
  39. }
  40. i++;
  41. j--;
  42. }
  43. return true;
  44. }
  45. }

(2)一刷没过。

(3)二刷错在:list存入是需要new的。不然一直是原来的list,是错的。

 

7,----------combination sum(求一个集合里面能够组合成目标值的组合种类)

(1)答案和思路:这里依然是利用递归来做。不同的就是,从i开始,在sum没有大于target之前,再次递归的元素下标是不变的。

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. public class Solution {
  5. public static void main(String[] args) {
  6. int[] a = {7, 1, 2, 5, 1, 6, 10};
  7. System.out.println(combinationSum(a, 8));
  8. }
  9. public static List<List<Integer>> combinationSum(int[] candidates, int target) {
  10. List<List<Integer>> result = new ArrayList();
  11. if (candidates == null || candidates.length == 0) {
  12. return result;
  13. }
  14. List<Integer> list = new ArrayList();
  15. Arrays.sort(candidates);
  16. calResult(candidates,target, 0, 0, result, list);
  17. return result;
  18. }
  19. public static void calResult(int[] candidates, int target, int sum, int index, List<List<Integer>> result, List<Integer> list) {
  20. if (sum > target) {
  21. return;
  22. }
  23. if (sum == target) {
  24. if (!result.contains(list)) {
  25. result.add(new ArrayList(list));
  26. }
  27. }
  28. for (int i = index; i < candidates.length; i++) {
  29. sum += candidates[i];
  30. list.add(candidates[i]);
  31. calResult(candidates, target, sum, i, result, list);
  32. sum -= candidates[i];
  33. list.remove(list.size() - 1);
  34. }
  35. }
  36. }

(2)一刷思路没弄对。

(3)二刷:忘记了sort数组。因为结果要求有序。

8, --------------combination sum II(和I的区别是,每个元素只能用一次,那么就是每一次递归都是i+1的走)

答案:

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. public class Solution {
  5. // public static void main(String[] args) {
  6. // int[] a = {7, 1, 2, 5, 1, 6, 10};
  7. // System.out.println(combinationSum(a, 8));
  8. // }
  9. public static List<List<Integer>> combinationSum2(int[] candidates, int target) {
  10. List<List<Integer>> result = new ArrayList();
  11. if (candidates == null || candidates.length == 0) {
  12. return result;
  13. }
  14. List<Integer> list = new ArrayList();
  15. Arrays.sort(candidates);
  16. calResult(candidates,target, 0, 0, result, list);
  17. return result;
  18. }
  19. public static void calResult(int[] candidates, int target, int sum, int index, List<List<Integer>> result, List<Integer> list) {
  20. if (sum > target) {
  21. return;
  22. }
  23. if (sum == target) {
  24. if (!result.contains(list)) {
  25. result.add(new ArrayList(list));
  26. }
  27. }
  28. for (int i = index; i < candidates.length; i++) {
  29. sum += candidates[i];
  30. list.add(candidates[i]);
  31. calResult(candidates, target, sum, i + 1, result, list);
  32. sum -= candidates[i];
  33. list.remove(list.size() - 1);
  34. }
  35. }
  36. }

(1)一刷bug-free;

9,--------------word ladder

(1)答案和思路:利用队列来进行记录每一次可以有多少个next直到遇到了end。

  1. import java.util.ArrayList;
  2. import java.util.HashSet;
  3. import java.util.LinkedList;
  4. import java.util.Queue;
  5. import java.util.Set;
  6.  
  7. public class Solution {
  8.  
  9. public static int ladderLength(String start, String end, Set<String> dict) {
  10. if (dict == null) {
  11. return 0;
  12. }
  13. dict.add(start);
  14. dict.add(end);
  15. HashSet<String> hash = new HashSet();
  16. Queue<String> queue = new LinkedList();
  17. queue.add(start);
  18. hash.add(start);
  19. int length = 1;
  20. while (!queue.isEmpty()) {
  21. length++;
  22. int size = queue.size();
  23. for (int i = 0; i < size; i++) {
  24. String word = queue.poll();
  25. dict.remove(word);
  26. for (String nextWord : getNextWords(word, dict)) {
  27. if (hash.contains(nextWord)) {
  28. continue;
  29. }
  30. if (nextWord.equals(end)) {
  31. return length;
  32. }
  33. hash.add(nextWord);
  34. queue.add(nextWord);
  35. }
  36.  
  37. }
  38. }
  39. return 0;
  40. }
  41. private static String replace(String s, int index, char c) {
  42. char[] chars = s.toCharArray();
  43. chars[index] = c;
  44. return new String(chars);
  45. }
  46. private static ArrayList<String> getNextWords(String word, Set<String> dict) {
  47. ArrayList<String> nextWords = new ArrayList();
  48. for (char c = 'a'; c <= 'z'; c++) {
  49. for (int i = 0; i < word.length(); i++) {
  50. if (c == word.charAt(i)) {
  51. continue;
  52. }
  53. String nextWord = replace(word, i, c);
  54. if (dict.contains(nextWord)) {
  55. nextWords.add(nextWord);
  56. }
  57. }
  58. }
  59.  
  60. return nextWords;
  61. }
  62. }

(2)注意如何找next。要利用好字符范围,通过改变字符的方式来找。因为dict太大。不能。利用contain。O(1)复杂度。

8,------------

-------------------------------------------

第八周:数据结构。

-------------------------------------------

1,------------min stack(最小栈)

答案:注意错误点,不要只判断null,还要判断size()

  1. public class MinStack {
  2. Stack<Integer> s1 = new Stack();
  3. Stack<Integer> s2 = new Stack();
  4. public MinStack() {
  5. // do initialize if necessary
  6. }
  7. public void push(int number) {
  8. // write your code here
  9. s1.push(number);
  10. if (s2 == null || s2.size() == 0) {
  11. s2.push(number);
  12. } else {
  13. if (number < s2.peek()) {
  14. s2.push(number);
  15. } else {
  16. s2.push(s2.peek());
  17. }
  18. }
  19. }
  20. public int pop() {
  21. // write your code here
  22. s2.pop();
  23. return s1.pop();
  24. }
  25. public int min() {
  26. // write your code her
  27. return s2.peek();
  28. }
  29. }

(1)一刷所犯错误是:没判断size==0;

(2)二刷bug-free;

 2,---------implement a queue by two stacks(用两个栈实现一个队列)

答案:1,这里有一个很好的结构就是,在class里面做出一定的定义,然后new的时候在结构函数里面。

  1. public class Queue {
  2. private Stack<Integer> stack1;
  3. private Stack<Integer> stack2;
  4. public Queue() {
  5. stack1 = new Stack();
  6. stack2 = new Stack();
  7. // do initialization if necessary
  8. }
  9. public void push(int element) {
  10. // write your code here
  11. stack1.push(element);
  12. }
  13. public int pop() {
  14. // write your code here
  15. if (stack2.isEmpty()) {
  16. while (!stack1.isEmpty()) {
  17. stack2.push(stack1.pop());
  18. }
  19. }
  20. return stack2.pop();
  21. }
  22. public int top() {
  23. // write your code here
  24. if (stack2.isEmpty()) {
  25. while (!stack1.isEmpty()) {
  26. stack2.push(stack1.pop());
  27. }
  28. }
  29. return stack2.peek();
  30. }
  31. }

(1)一刷忘记了在构造函数里面初始化。

(2)2刷bug-free;

 

 

3,----------largest rectangle in histogram(在直方图中最大的矩形)(再一次验证了lintcode的数据集不严谨,卡时间不严谨,暴力也能过。这道题公认的暴力是过不了的。)

 答案和思路:利用stack来存index。只要是递增的时候,就一直往里放,如果不是递增,那么就往外pop。

  1. public class Solution {
  2. public int largestRectangleArea(int[] heights) {
  3. if (null == heights || heights.length == 0) {
  4. return 0;
  5. }
  6. int area = 0;
  7. Stack<Integer> stack = new Stack();
  8. for (int i = 0; i < heights.length; i++) {
  9. if (stack.empty() || heights[stack.peek()] < heights[i]) {
  10. stack.push(i);
  11. } else {
  12. int start = stack.pop();
  13. int width = stack.isEmpty() ? i : i - stack.peek() - 1;
  14. area = Math.max(area, heights[start] * width);
  15. i--;
  16. }
  17. }
  18. while (!stack.isEmpty()) {
  19. int start = stack.pop();
  20. int width = stack.isEmpty() ? heights.length : heights.length - stack.peek() - 1;
  21. area = Math.max(area, heights[start] * width);
  22. }
  23. return area;
  24. }
  25. }

(1)下次重点看着道题。

 

 

 4,-----------

5,-----------rehashing(重哈希)

 答案:

  1. /**
  2. * Definition for ListNode
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) {
  7. * val = x;
  8. * next = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. /**
  14. * @param hashTable: A list of The first node of linked list
  15. * @return: A list of The first node of linked list which have twice size
  16. */
  17. public ListNode[] rehashing(ListNode[] hashTable) {
  18. // write your code here
  19. int capacity = hashTable.length;
  20. ListNode[] res = new ListNode[2 * capacity];
  21. for (int i = 0; i < capacity; i++) {
  22. ListNode a = hashTable[i];
  23. while (a != null) {
  24. int index = 0;
  25. if (a.val >= 0) {
  26. index = a.val % (2 * capacity);
  27. } else {
  28. index = (a.val % (2 * capacity) + (2 * capacity)) % (2 * capacity);
  29. }
  30. if (res[index] == null) {
  31. res[index] = new ListNode(a.val);
  32. } else {
  33. ListNode tmp = res[index];
  34. while (tmp.next != null) {
  35. tmp = tmp.next;
  36. }
  37. tmp.next = new ListNode(a.val);
  38. }
  39. a = a.next;
  40. }
  41. }
  42. return res;
  43. }
  44. }

 6,-----------

7,-----------median number(中位数)

答案:

  1. public static int[] medianII(int[] nums) {
  2. if (nums == null || nums.length == 0) {
  3. return null;
  4. }
  5. int len = nums.length;
  6. int[] res = new int[len];
  7. ArrayList<Integer> list = new ArrayList();
  8. for (int i = 0; i < len; i++) {
  9. list.add(nums[i]);
  10. Collections.sort(list);
  11. res[i] = list.get((list.size() - 1) / 2);
  12. }
  13. return res;
  14. }

 8,---------ugly number(丑数)

答案:

  1. public static long kthPrimeNumber(int k) {
  2. Queue<Long> q1 = new PriorityQueue();
  3. Queue<Long> q2 = new PriorityQueue();
  4. Queue<Long> q3 = new PriorityQueue();
  5. q1.offer((long) 3);
  6. q2.offer((long) 5);
  7. q3.offer((long) 7);
  8. long res = 0;
  9. for (int i = 0; i < k; i++) {
  10. if (q1.peek() < q2.peek()) {
  11. if (q1.peek() < q3.peek()) {
  12. res = q1.poll();
  13. if (!q1.contains(res * 3)) {
  14. q1.offer(res * 3);
  15. }
  16. if (!q1.contains(res * 5)) {
  17. q1.offer(res * 5);
  18. }
  19. if (!q1.contains(res * 7)) {
  20. q1.offer(res * 7);
  21. }
  22. } else {
  23. res = q3.poll();
  24. if (!q3.contains(res * 7)) {
  25. q3.offer(res * 7);
  26. }
  27. }
  28. } else {
  29. if (q2.peek() < q3.peek()) {
  30. res = q2.poll();
  31. if (!q2.contains(res * 5)) {
  32. q2.offer(res * 5);
  33. }
  34. if (!q2.contains(res * 7)) {
  35. q2.offer(res * 7);
  36. }
  37. } else {
  38. res = q3.poll();
  39. if (!q3.contains(res * 7)) {
  40. q3.offer(res * 7);
  41. }
  42. }
  43. }
  44. }
  45. return res;
  46. }

 9,-------merge K sorted arrays(合并k个有序的数组)

 答案:

  1. public static List<Integer> mergekSortedArrays(int[][] arrays) {
  2. List<Integer> res = new ArrayList();
  3. int height = arrays.length;
  4. int[] index = new int[height];
  5. boolean flag = true;
  6. while (flag) {
  7. flag = false;
  8. int min = Integer.MAX_VALUE;
  9. int minIndex = 0;
  10. for (int i = 0; i < height; i++) {
  11. if (arrays[i] != null && index[i] < arrays[i].length && arrays[i][index[i]] < min) {
  12. minIndex = i;
  13. min = arrays[i][index[i]];
  14. flag = true;
  15. }
  16. }
  17. if (!flag) {
  18. break;
  19. }
  20. res.add(min);
  21. index[minIndex]++;
  22. }
  23. return res;
  24. }

10,-----------

 

-------------------------------------------

第七周:数组。

-------------------------------------------

1,---------------merge sorted array I(归并两个有序的数组)

答案:注意1,要判断有一个是否已经copy完了。其次要注意是A还是B。

  1. public void mergeSortedArray(int[] A, int m, int[] B, int n) {
  2. // write your code here
  3. // error :b < 0
  4. int a = m - 1;
  5. int b = n - 1;
  6. for (int i = m + n - 1; i >= 0; i--) {
  7. if (b < 0) {
  8. A[i] = A[a--];
  9. } else if (a < 0) {
  10. A[i] = A[b--];
  11. } else if (A[a] > B[b]) {
  12. A[i] = A[a--];
  13. } else {
  14. A[i] = B[b--];
  15. }
  16. }
  17. }

(2)二刷bug-free; 

2,------------merge sorted array II (归并两个有序的数组)

答案:

  1. class Solution {
  2. /**
  3. * @param A and B: sorted integer array A and B.
  4. * @return: A new sorted integer array
  5. */
  6. public int[] mergeSortedArray(int[] A, int[] B) {
  7. // Write your code here
  8. int lenA = A.length;
  9. int lenB = B.length;
  10. int len = lenA + lenB;
  11. int[] result = new int[len];
  12. int indexA = lenA - 1;
  13. int indexB = lenB - 1;
  14. int index = len - 1;
  15. while (index >= 0) {
  16. if (indexA >= 0 && (indexB < 0 || (A[indexA] >= B[indexB]))) {
  17. result[index--] = A[indexA--];
  18. } else {
  19. result[index--] = B[indexB--];
  20. }
  21. }
  22. return result;
  23. }
  24. }

(2)二刷bug-free;

 3,------------median of two sorted Arrays(找两个有序数组的中位数)

答案:需要改进,这是暴力解法。没有logn

  1. public double findMedianSortedArrays(int[] a, int[] b) {
  2. // write your code here
  3. int n = a.length + b.length;
  4. int[] tmp = mergeSortedArray(a, b);
  5. if (n % 2 == 0) {
  6. return (tmp[n / 2 - 1] + tmp[n / 2]) / 2.0;
  7. } else {
  8. return (double) tmp[n / 2];
  9. }
  10. }
  11. public static int[] mergeSortedArray(int[] a, int[] b) {
  12. // Write your code here
  13. int m = a.length;
  14. int n = b.length;
  15. int[] result = new int[m + n];
  16. int len1 = 0;
  17. int len2 = 0;
  18. for (int i = 0; i < m + n; i++) {
  19. if (len1 >= m) {
  20. result[i] = b[len2++];
  21. } else if (len2 >= n) {
  22. result[i] = a[len1++];
  23. } else if (a[len1] < b[len2]) {
  24. result[i] = a[len1++];
  25. } else {
  26. result[i] = b[len2++];
  27. }
  28. }
  29. return result;
  30. }

 4,-------best time to buy and sell stock I(买卖股票最佳时机)

答案:

  1. public int maxProfit(int[] prices) {
  2. // write your code here
  3. int max = 0;
  4. int[] dp = new int[prices.length];
  5. for (int i = 0; i < prices.length; i++) {
  6. for (int j = i - 1; j >= 0; j--) {
  7. if (prices[j] < prices[i]) {
  8. dp[i] = Math.max(dp[i], prices[i] - prices[j]);
  9. }
  10. max = Math.max(max, dp[i]);
  11. }
  12. }
  13. return max;
  14. }

(2)一刷bug-free;

II

答案:

  1. public int maxProfit(int[] prices) {
  2. // write your code here
  3. int maxProfit = 0;
  4. for (int i = 1; i < prices.length; i++) {
  5. if (prices[i] - prices[i - 1] > 0) {
  6. maxProfit += prices[i] - prices[i - 1];
  7. }
  8. }
  9. return maxProfit;
  10. }

(2)bug-free;

III,

答案:思路:就是通过left,right来判断,就是到了这个点,前面最大能有多少利益,后面又有多少。(再一次显示了lintcode的敷衍性,数据太少了,时间卡的不及时。如果单纯的left,right来不断算的话会超时。所以需要用数组来进行操作)

  1. public class Solution {
  2. public int maxProfit(int[] prices) {
  3. if (null == prices || prices.length == 0) {
  4. return 0;
  5. }
  6. int max = 0;
  7. int min = prices[0];
  8. int[] left = new int[prices.length];
  9. int[] right = new int[prices.length];
  10. for (int i = 1; i < prices.length; i++) {
  11. min = Math.min(min, prices[i]);
  12. left[i] = Math.max(left[i - 1], prices[i] - min);
  13. }
  14. max = prices[prices.length - 1];
  15. for (int i = prices.length - 2; i >= 0; i--) {
  16. max = Math.max(max, prices[i]);
  17. right[i] = Math.max(right[i + 1], max - prices[i]);
  18. }
  19. System.out.println(Arrays.toString(left));
  20. int profit = 0;
  21. for (int i = 0; i < prices.length; i++) {
  22. profit = Math.max(profit, left[i] + right[i]);
  23. }
  24. return profit;
  25. }
  26. }

(1)一刷错误是:边界没有考虑清楚,比如说left【】是从第一个开始算,所以min要初始化为第0个。

(2)bug-free;

IV,

答案和思路:

 

 

 

5,---maximum subarray(最大子数组)

(I)答案:

  1. public int maxSubArray(int[] nums) {
  2. // write your code
  3. if (null == nums || nums.length == 0) {
  4. return 0;
  5. }
  6. int len = nums.length;
  7. int[] dp = new int[len];
  8. dp[0] = nums[0];
  9. int max = nums[0];
  10. for (int i = 1; i < len; i++) {
  11. if (dp[i - 1] > 0) {
  12. dp[i] = dp[i - 1] + nums[i];
  13. } else {
  14. dp[i] = nums[i];
  15. }
  16. max = Math.max(max, dp[i]);
  17. }
  18. return max;
  19. }

 bug-free;

 

-------------------------------------------

第六周:链表。

-------------------------------------------

1,---------remove duplicates from sorted list II(从有序的链表中移除重复元素)

(1)答案和思路:注意的地方就是记得判断null。

  1. public class Solution {
  2. /**
  3. * @param ListNode head is the head of the linked list
  4. * @return: ListNode head of the linked list
  5. */
  6. public static ListNode deleteDuplicates(ListNode head) {
  7. // write your code here
  8. if (null == head) {
  9. return head;
  10. }
  11. ListNode preHead = new ListNode(-1);
  12. preHead.next = head;
  13. ListNode pre = preHead;
  14. ListNode cur = head;
  15. while (cur != null && cur.next != null) {
  16. if (cur.val != cur.next.val) {
  17. cur = cur.next;
  18. pre = pre.next;
  19. continue;
  20. }
  21. while (cur.next != null && cur.val == cur.next.val) {
  22. cur = cur.next;
  23. }
  24. pre.next = cur.next;
  25. cur = cur.next;
  26. }
  27. return preHead.next;
  28. }
  29. }

(2)一刷bug-free;

 

2,-----------reverse linked list II(翻转链表)

(1)

翻转链表1:就是建一个preHead,每次来都插进去。用temp记一下preHead.next;注意的地方是要先把head = head.next。不然一会head.next改变了

  1. public class Solution {
  2. /**
  3. * @param head: The head of linked list.
  4. * @return: The new head of reversed linked list.
  5. */
  6. public ListNode reverse(ListNode head) {
  7. // write your code here
  8. if (null == head || head.next == null) {
  9. return head;
  10. }
  11. ListNode preHead = new ListNode(0);
  12. while (head != null) {
  13. ListNode temp = preHead.next;
  14. preHead.next = head;
  15. head = head.next;
  16. preHead.next.next = temp;
  17. }
  18. return preHead.next;
  19. }
  20. }

翻转链表II:注意不要用next做判断,因为它是会变的。

 (3)二刷bug-free;

 

3,-------partition list(分割链表)

(1)答案:主要是思路是开两个list来操作

  1. public static ListNode partition(ListNode head, int x) {
  2. if (null == head) {
  3. return head;
  4. }
  5. ListNode preHead = new ListNode(0);
  6. preHead.next = head;
  7. ListNode left = preHead;
  8. ListNode right = preHead;
  9. while (left.next != null) {
  10. if (left.next.val < x) {
  11. left = left.next;
  12. continue;
  13. }
  14. right = left;
  15. while (right.next != null && right.next.val >= x) {
  16. right = right.next;
  17. }
  18. if (right.next == null) {
  19. break;
  20. }
  21. ListNode tmp = right.next;
  22. right.next = right.next.next;
  23. ListNode tmp2 = left.next;
  24. left.next = tmp;
  25. tmp.next = tmp2;
  26. left = left.next;
  27. }
  28. head = preHead.next;
  29. return head;
  30. }

 (2)一刷不能bug-free:是因为思路。

(3)二刷bug-free;

4,--------------sort list

(1)答案和思路:因为要求O(nlogn) ,所以用归并来做。

  1. /**
  2. * Definition for ListNode.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int val) {
  7. * this.val = val;
  8. * this.next = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. /**
  14. * @param head: The head of linked list.
  15. * @return: You should return the head of the sorted linked list,
  16. using constant space complexity.
  17. */
  18. public ListNode sortList(ListNode head) {
  19. // write your code here
  20. if (head == null || head.next == null) {
  21. return head;
  22. }
  23. ListNode mid = findMiddle(head);
  24. ListNode right = sortList(mid.next);
  25. mid.next = null;
  26. ListNode left = sortList(head);
  27. return merge(left, right);
  28. }
  29. private ListNode findMiddle(ListNode head) {
  30. ListNode slow = head;
  31. ListNode fast = head.next;
  32. while (fast != null && fast.next != null) {
  33. fast = fast.next.next;
  34. slow = slow.next;
  35. }
  36. return slow;
  37. }
  38. public static ListNode merge(ListNode head1, ListNode head2) {
  39. if (head1 == null) {
  40. return head2;
  41. }
  42. if (head2 == null) {
  43. return head1;
  44. }
  45. ListNode preHead = new ListNode(0);
  46. ListNode cur = preHead;
  47. while (head1 != null || head2 != null) {
  48. if (head1 != null && (head2 == null || head1.val < head2.val)) {
  49. cur.next = new ListNode(head1.val);
  50. head1 = head1.next;
  51. cur = cur.next;
  52. } else {
  53. cur.next = new ListNode(head2.val);
  54. head2 = head2.next;
  55. cur = cur.next;
  56. }
  57. }
  58. return preHead.next;
  59. }
  60. }

(2)一刷AC需要三次:1,归并没有记熟系。2,找中点的时候fast=head.next;

(3)二刷:bug-free;

 

      

5,--------reorder list(重排链表)

(1)答案和思路:因为要本地in-place。所以,对后半部分进行reverse,然后接进去。

  1. public class Solution {
  2. /**
  3. * @param head: The head of linked list.
  4. * @return: void
  5. */
  6. public void reorderList(ListNode head) {
  7. // write your code here
  8. if (head == null || head.next == null) {
  9. return;
  10. }
  11. ListNode middle = findMiddle(head);
  12. ListNode newMiddle = reverseList(middle.next);
  13. middle.next = null;
  14. while (newMiddle != null) {
  15. ListNode temp = head.next;
  16. head.next = newMiddle;
  17. newMiddle = newMiddle.next;
  18. head.next.next = temp;
  19. head = head.next.next;
  20. }
  21. }
  22. public static ListNode findMiddle(ListNode head) {
  23. if (head == null || head.next == null) {
  24. return head;
  25. }
  26. ListNode slow = head;
  27. ListNode fast = head.next;
  28. while (fast != null && fast.next != null) {
  29. slow = slow.next;
  30. fast = fast.next.next;
  31. }
  32. return slow;
  33. }
  34. public static ListNode reverseList(ListNode head) {
  35. if (head == null || head.next == null) {
  36. return head;
  37. }
  38. ListNode preHead = new ListNode(0);
  39. while (head != null) {
  40. ListNode temp = preHead.next;
  41. preHead.next = head;
  42. head = head.next;
  43. preHead.next.next = temp;
  44. }
  45. return preHead.next;
  46. }
  47. }

(2)一刷AC需要两次:错在fast.next.next误写成fast.next:

(3)二刷AC: 第二次所犯错误是reverse函数中返回preHead是错的。

 

6,-------------链表有无环判断(Linked list cycle)

答案:bug-free;

  1. public boolean hasCycle(ListNode head) {
  2. // write your code here
  3. ListNode fast = head;
  4. ListNode slow = head;
  5. while (fast != null && fast.next != null) {
  6. slow = slow.next;
  7. fast = fast.next.next;
  8. if (slow == fast) {
  9. return true;
  10. }
  11. }
  12. return false;
  13. }

 7,------------rotate list(右移动链表)

(1)答案和思路:注意,如果k % length == 0 要特判断。

 

  1. public ListNode rotateRight(ListNode head, int k) {
  2. // write your code here
  3. if (head == null ) {
  4. return head;
  5. }
  6. ListNode preHead = new ListNode(0);
  7. preHead.next = head;
  8. int length = 0;
  9. ListNode pre = preHead;
  10. while (pre.next != null) {
  11. length++;
  12. pre = pre.next;
  13. }
  14. int n = k % length;
  15. if (n == 0) {
  16. return head;
  17. }
  18. for (int i = 0; i < length - n; i++) {
  19. ListNode tmp = preHead.next;
  20. preHead.next = preHead.next.next;
  21. pre.next = tmp;
  22. tmp.next = null;
  23. pre = pre.next;
  24. }
  25. head = preHead.next;
  26. return head;
  27. }

(2)一刷没法AC是因为:k % length == 0 没有判断导致后面出现了空指针。

(3)二刷bug-free;

 

8,--------------Merge k sorted list(归并k个有序链表)

答案: bug-free;但是下次要看一下九章的其他算法。

  1. public ListNode mergeKLists(List<ListNode> lists) {
  2. // write your code here
  3. // error1 : input []
  4. if (lists == null || lists.size() == 0) {
  5. return null;
  6. }
  7. int min = Integer.MAX_VALUE;
  8. int flag = -1;
  9. boolean b = true;
  10. ListNode pre = new ListNode(0);
  11. ListNode cur = pre;
  12. while (b) {
  13. b = false;
  14. int i = 0;
  15. for (ListNode list : lists) {
  16. if (list != null && list.val < min) {
  17. flag = i;
  18. min = list.val;
  19. b = true;
  20. }
  21. i++;
  22. }
  23. if (!b) {
  24. break;
  25. }
  26. cur.next = new ListNode(min);
  27. lists.set(flag, lists.get(flag).next);
  28. cur = cur.next;
  29. min = Integer.MAX_VALUE;
  30. }
  31. return pre.next;
  32. }

 

 9,------------copy list with random pointer(复制带有随机指针的链表)

(1)答案:在原表上进行操作

  1. public static RandomListNode copyRandomList(RandomListNode head) {
  2. // write your code here
  3. RandomListNode cur = head;
  4. while (cur != null) {
  5. RandomListNode tmp = new RandomListNode(cur.label);
  6. RandomListNode next = cur.next;
  7. cur.next = tmp;
  8. tmp.next = next;
  9. cur = cur.next.next;
  10. }
  11. cur = head;
  12. while (cur != null) {
  13. if (cur.random == null) {
  14. cur.next.random = null;
  15. } else {
  16. cur.next.random = cur.random.next;
  17. }
  18. cur = cur.next.next;
  19. }
  20. RandomListNode head2 = head.next;
  21. RandomListNode cur2 = head2;
  22. while (cur2.next != null) {
  23. cur2.next = cur2.next.next;
  24. cur2 = cur2.next;
  25. }
  26. return head2;
  27. }

(2)一刷AC不了是因为:有next.next的时候没有注意判断.next可以搞定不。而且注意题目里面是label和RandomListNode。

(3)二刷: bug-free;

 

14,--------Remove Nth Node From End of List(删除链表中倒数第n个值)

(1)答案:

  1. ListNode removeNthFromEnd(ListNode head, int n) {
  2. // write your code here
  3. int length = 0;
  4. ListNode preHead = new ListNode(0);
  5. preHead.next = head;
  6. ListNode cur = head;
  7. while (cur != null) {
  8. length++;
  9. cur = cur.next;
  10. }
  11. ListNode pre = preHead;
  12. for (int i = 0; i < length - n; i++) {
  13. pre = pre.next;
  14. }
  15. pre.next = pre.next.next;
  16. head = preHead.next;
  17. return head;
  18. }

(2)注意的地方是:如果删除的头结点。所以要建一个新的头结点。

 

15,--------------Linked List cycle(找链表循环的起点)

(1)答案:

  1. public ListNode detectCycle(ListNode head) {
  2. // write your code here
  3. ListNode fast = head;
  4. ListNode slow = head;
  5. boolean isHave = false;
  6. while (fast != null && fast.next != null) {
  7. fast = fast.next.next;
  8. slow = slow.next;
  9. if (fast == slow) {
  10. isHave = true;
  11. break;
  12. }
  13. }
  14. if (!isHave) {
  15. return null;
  16. }
  17. slow = head;
  18. while (slow != fast) {
  19. slow = slow.next;
  20. fast = fast.next;
  21. }
  22. return fast;
  23. }

 (2)一次没有AC是因为没环要返回null,没有注意到。

-------------------------------------------

第五周:动态规划二

-------------------------------------------

1,------------ 分割回文串 II(Palindrome Partitioning II)

(1)答案:第i个字符串的分割dp[i] = 他到前面某一个是回文然后加上前面那一段所需要的最少分割 +1。

  1. public class Solution {
  2. /**
  3. * @param s a string
  4. * @return an integer
  5. */
  6. public int minCut(String s) {
  7. // write your code here
  8. if (null == s || s.length() == 0) {
  9. return 0;
  10. }
  11. int length = s.length();
  12. int[] dp = new int[length + 1];
  13. for (int i = 0; i <= length; i++) {
  14. dp[i] = i - 1;
  15. }
  16. for (int i = 1 ; i <= length; i++) {
  17. for (int j = i; j > 0; j--) {
  18. if (isPalindrome(s, j - 1, i - 1)) {
  19. dp[i] = Math.min(dp[i], dp[j - 1] + 1);
  20. }
  21. }
  22. }
  23. return dp[length];
  24. }
  25. public static boolean isPalindrome(String s, int start, int end) {
  26. if (null == s || s.length() == 0) {
  27. return true;
  28. }
  29. while (start < end) {
  30. // if (!s.get(start).equals(s.get(end))) {
  31. if (s.charAt(start) != s.charAt(end)) {
  32. return false;
  33. }
  34. start++;
  35. end--;
  36. }
  37. return true;
  38. }
  39. }

 

 (2)一刷没有AC。

(3)二刷:bug-free;

2,----------------Word Break(单词切分)

(1)答案:注意要考虑单词的长度

  1. public class Solution {
  2. /**
  3. * @param s: A string s
  4. * @param dict: A dictionary of words dict
  5. */
  6. public boolean wordBreak(String s, Set<String> dict) {
  7. // write your code here
  8. if (s == null || s.length() == 0) {
  9. return true;
  10. }
  11. if (dict == null) {
  12. return false;
  13. }
  14. int length = s.length();
  15. int maxWordLength = 0;
  16. for (String str : dict) {
  17. maxWordLength = Math.max(maxWordLength, str.length());
  18. }
  19. boolean[] dp = new boolean[length + 1];
  20. dp[0] = true;
  21. for (int i = 1; i <= length; i++) {
  22. for (int j = i; j > 0 && i - j <= maxWordLength; j--) {
  23. if (dp[j - 1] && dict.contains(s.substring(j - 1, i))) {
  24. dp[i] = true;
  25. break;
  26. }
  27. }
  28. }
  29. return dp[length];
  30. }
  31. }

(2)一刷LTE了,原因是没有考虑单词的长度。

(3)二刷没有bug-free,是因为j--错写成j++. 

3,---------Longest Common Subsequence(最长公共子序列)

注意:个数是从0个开始。

(1)答案和思路:1,就是取第一个字符串前i个,第二个字符串前j个写递推式。

  1. public int longestCommonSubsequence(String A, String B) {
  2. // write your code here
  3. // error1 : 忘记判断空了
  4. if (A == null || A.length() == 0 || B == null || B.length() == 0) {
  5. return 0;
  6. }
  7. int[][] dp = new int[A.length() + 1][B.length() + 1]; // error3:int[][] dp = new int[A.length()][B.length()];
  8. for (int i = 1; i <= A.length(); i++) {
  9. for (int j = 1; j <= B.length(); j++) {
  10. if (A.charAt(i - 1) == B.charAt(j - 1)) {// error4 :if (A.charAt(i) == B.charAt(j)) {
  11. dp[i][j] = Math.max(Math.max(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1] + 1);
  12. } else {
  13. dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
  14. }
  15. }
  16. }
  17. return dp[A.length()][B.length()];
  18. }

(2)一刷没有AC。

(3)二刷bug-free; 

5,------------Edit Distance(编辑距离)

 (1)答案:

  1. public int minDistance(String word1, String word2) {
  2. // write your code here
  3. // error1: 忘记判断空了
  4. if ((word1 == null || word1.length() == 0) && (word2 == null || word2.length() == 0)) {
  5. return 0;
  6. }
  7. int m = word1.length();
  8. int n = word2.length();
  9. int[][] dp = new int[m + 1][n + 1];
  10. for (int i = 0; i < m + 1; i++) {
  11. dp[i][0] = i;
  12. }
  13. for (int j = 0; j < n + 1; j++) {
  14. dp[0][j] = j;
  15. }
  16. for (int i = 1; i < m + 1; i++) {
  17. for (int j = 1; j < n + 1; j++) {
  18. if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
  19. dp[i][j] = Math.min(Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1]);
  20. } else {
  21. dp[i][j] = Math.min(Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1] + 1);
  22. }
  23. }
  24. }
  25. return dp[m][n];
  26. }

(2)bug-free; 

6,----------distinct subsequences(不同的子序列)

(1)答案和思路: 前i,前j思路。

  1. public int numDistinct(String s, String t) {
  2. // write your code here
  3. if (t == null) {
  4. return 1;
  5. }
  6. if (s == null) {
  7. return 0;
  8. }
  9. int m = s.length();
  10. int n = t.length();
  11. int[][] dp = new int[m + 1][n + 1];
  12. for (int i = 0; i < m + 1; i++) {
  13. dp[i][0] = 1;
  14. }
  15. for (int i = 1; i < m + 1; i++) {
  16. for (int j = 1; j < n + 1; j++) {
  17. if (s.charAt(i - 1) == t.charAt(j - 1)) {
  18. dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
  19. } else {
  20. dp[i][j] = dp[i - 1][j];
  21. }
  22. }
  23. }
  24. return dp[m][n];
  25. }

 (2)没有bug-free,因为错写了charAt(i)应该是i - 1;

(3)二刷bug-free;

 7,---------Interleaving String(交叉字符串)

 (1)答案和思路:也就是前i,j思路

  1. public class Solution {
  2. /**
  3. * Determine whether s3 is formed by interleaving of s1 and s2.
  4. * @param s1, s2, s3: As description.
  5. * @return: true or false.
  6. */
  7. public boolean isInterleave(String s1, String s2, String s3) {
  8. // write your code here
  9. if (s1 == null || s2 == null || s3 == null) {
  10. return false;
  11. }
  12. int len1 = s1.length();
  13. int len2 = s2.length();
  14. int len3 = s3.length();
  15. if (len1 + len2 != len3) {
  16. return false;
  17. }
  18. boolean[][] dp = new boolean[len1 + 1][len2 + 1];
  19. dp[0][0] = true;
  20. for (int i = 1; i < len1 + 1; i++) {
  21. if (s1.charAt(i - 1) == s3.charAt(i - 1)) {
  22. dp[i][0] = true;
  23. } else {
  24. break;
  25. }
  26. }
  27. for (int j = 1; j < len2 + 1; j++) {
  28. if (s2.charAt(j - 1) == s3.charAt(j - 1)) {
  29. dp[0][j] = true;
  30. } else {
  31. break;
  32. }
  33. }
  34. for (int i = 1; i < len1 + 1; i++) {
  35. for (int j = 1; j < len2 + 1; j++) {
  36. dp[i][j] = dp[i - 1][j] && s1.charAt(i - 1) == s3.charAt(i + j - 1) || dp[i][j - 1] && s2.charAt(j - 1) == s3.charAt(i + j - 1);
  37. }
  38. }
  39. return dp[len1][len2];
  40. }
  41. }

(2)一刷没有做对。

(3)二刷bug-free;

 

 

-------------------------------------------

第四周:动态规划一(动态规划三要素:1,求最大最小值;2,判断是否可行;3,统计方案个数;)

不使用动态规划:求具体方案个数,不是序列而是集合。因为dp要求顺序不能变。

-------------------------------------------

1,-----------Triangle( 数字三角形)

(1)答案:

  1. public static int minimumTotal(int[][] triangle) {
  2. int[][] res = new int[triangle.length][triangle[triangle.length - 1].length];
  3. for (int j = 0; j < triangle[triangle.length - 1].length; j++) {
  4. res[triangle.length - 1][j] = triangle[triangle.length - 1][j];
  5. }
  6. for (int i = triangle.length - 2; i >= 0; i--) {
  7. for (int j = 0; j < triangle[i].length; j++) {
  8. res[i][j] = triangle[i][j] + Math.min(res[i + 1][j], res[i + 1][j + 1]);
  9. }
  10. }
  11. return res[0][0];
  12. }

 (2)一刷没有做对是因为注意dp过程中到底是加dp的值还是triangle的值。此外dp比较麻烦可以考虑从后往前。

(3)二刷所犯错误:i--误写成i++;

 

3,-------------Longest Consecutive Sequence( 最长连续序列)

答案:(1)这是自己写的,已经先排序了。要找更优化的答案:

  1. public static int longestConsecutive(int[] num) {
  2. int length = 1;
  3. Arrays.sort(num);
  4. int max = 1;
  5. for (int i = 1; i < num.length; i++) {
  6. if (num[i] - num[i - 1] == 1) {
  7. max++;
  8. } else if (num[i] - num[i -1] == 0) {
  9. continue;
  10. } else {
  11. length = Math.max(length, max);
  12. max = 1;
  13. }
  14. }
  15. return Math.max(length, max);
  16. }

(2)

4,------------Minimum Path Sum(最小路径和)

(1)答案:注意length千万别再写错了。此外注意千万别忘记了加上这一格的值。

  1. public int minPathSum(int[][] grid) {
  2. // write your code here
  3. int[][] dp = new int[grid.length][grid[0].length];
  4. dp[0][0] = grid[0][0];
  5. for (int j = 1; j < grid[0].length; j++) {
  6. dp[0][j] = dp[0][j - 1] + grid[0][j];
  7. }
  8. int sum2 = dp[0][0];
  9. for (int i = 1; i < grid.length; i++) {
  10. dp[i][0] = dp[i - 1][0] + grid[i][0];
  11. }
  12. for (int i = 1; i < grid.length; i++) {
  13. for (int j = 1; j < grid[0].length; j++) {
  14. dp[i][j] = grid[i][j] + Math.min(dp[i - 1][j], dp[i][j - 1]);
  15. }
  16. }
  17. return dp[grid.length - 1][grid[0].length - 1];
  18. }

(2)bug-free; 

 

5,-----------Unique Paths(不同的路径)

(1)答案:一定要注意i,j位置。bug=free;

  1. public int uniquePaths(int m, int n) {
  2. // write your code here
  3. int[][] dp = new int[m][n];
  4. for (int i = 0; i < m; i++) {
  5. dp[i][0] = 1;
  6. }
  7. for (int j = 0; j < n; j++) {
  8. dp[0][j] = 1;
  9. }
  10. for (int i = 1; i < m; i++) {
  11. for (int j = 1; j < n; j++) {
  12. dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
  13. }
  14. }
  15. return dp[m - 1][n - 1];
  16. }

(2)一刷bug-free; 

6,------------Climbing Stairs(爬楼梯)

(1)答案:不要忘记特判断。

  1. public int climbStairs(int n) {
  2. // write your code here
  3. if (n <= 1) {
  4. return 1;
  5. }
  6. int[] dp = new int[n];
  7. dp[0] = 1;
  8. dp[1] = 2;
  9. for (int i = 2; i < n; i++) {
  10. dp[i] = dp[i - 1] + dp[i - 2];
  11. }
  12. return dp[n - 1];
  13. }

 (2)一刷:3次才能AC。所犯错误是:首先,台阶是0的时候应该返回0; 其次是注意下标i从2开始递推。

(3)二刷:bug-free;

 7,---------------Jump Game(跳跃游戏)

(1)答案:注意单词的拼写。length;

  1. public boolean canJump(int[] A) {
  2. // wirte your code here
  3. boolean[] result = new boolean[A.length];
  4. result[0] = true;
  5. for (int i = 0; i < A.length; i++) {
  6. if (!result[i]){
  7. return false;
  8. }
  9. for (int j = i + 1; j < A.length && j < i + 1 + A[i]; j++) {
  10. result[j] = true;
  11. }
  12. }
  13. return result[A.length - 1];
  14. }

 (2)bug-free;

 

8,-------------Jump Game II(跳跃游戏 II)

(1)答案:思路是,dp[0] = 0;然后从1开始,找他前面能到他的min+1;能到等价于:a[j] >= i - j;

  1. public int jump(int[] A) {
  2. // write your code here
  3. int[] dp = new int[A.length];
  4. dp[0] = 0;
  5. for (int i = 1; i < A.length; i++) {
  6. int min = Integer.MAX_VALUE;
  7. for (int j = 0; j < i; j++) {
  8. if (A[j] >= i - j) {
  9. min = Math.min(min, dp[j]);
  10. }
  11. }
  12. dp[i] = min + 1;
  13. }
  14. return dp[A.length - 1];
  15. }

(2)bug-free; 

9,------------Unique Paths II(不同的路径 II)

(1)答案:

  1. public int uniquePathsWithObstacles(int[][] obstacleGrid) {
  2. // write your code here
  3. int m = obstacleGrid.length;
  4. int n = obstacleGrid[0].length;
  5. int[][] dp = new int[m][n];
  6. for (int i = 0; i < m; i++) {
  7. if (obstacleGrid[i][0] != 1) {
  8. dp[i][0] = 1;
  9. } else {
  10. break;
  11. }
  12. }
  13. for (int j = 0; j < n; j++) {
  14. if (obstacleGrid[0][j] != 1) {
  15. dp[0][j] = 1;
  16. } else {
  17. break;
  18. }
  19. }
  20. for (int i = 1; i < m; i++) {
  21. for (int j = 1; j < n; j++) {
  22. if (obstacleGrid[i][j] != 1) {
  23. dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
  24. }
  25. }
  26. }
  27. return dp[m - 1][n - 1];
  28. }

 (2)一刷AC需要2次,所犯错误是:在初始化第一列/行的时候不仅仅是有障碍物的设为0而是他以后的都设置为0,因为不可达到了。

(3)二刷bug-free;

10,---------------------Longest Increasing Subsequence(最长上升子序列)

答案:

  1. public int longestIncreasingSubsequence(int[] nums) {
  2. // write your code here
  3. // error1: 忘记判断空了
  4. // error2: 1 1 1 1 1 1 1
  5. if (null == nums || nums.length == 0) {
  6. return 0;
  7. }
  8. int res = 0;
  9. int[] dp = new int[nums.length];
  10. for (int i = 0; i < dp.length; i++) {
  11. dp[i] = 1;
  12. for (int j = i - 1; j >= 0; j--) {
  13. if (nums[j] <= nums[i]) {
  14. dp[i] = Math.max(dp[i], dp[j] + 1);
  15. }
  16. }
  17. res = Math.max(res, dp[i]);
  18. }
  19. return res;
  20. }

 

-------------------------------------------

第三周:二叉树和分治法

-------------------------------------------

碰到二叉树的问题的时候,就想想整棵树在该问题上的结果和左右儿子在该问题上的结果之间的联系是什么。

1,------------------Binary Tree Preorder Traversal(二叉树的前序遍历)

(1)用递归:bug-free;

 

  1. import java.util.ArrayList;
  2. public class Solution {
  3. /**
  4. * @param root: The root of binary tree.
  5. * @return: Preorder in ArrayList which contains node values.
  6. */
  7. public ArrayList<Integer> preorderTraversal(TreeNode root) {
  8. // write your code here
  9. ArrayList<Integer> preorder = new ArrayList();
  10. preorderTraversal(root, preorder);
  11. return preorder;
  12. }
  13. public static void preorderTraversal(TreeNode root, ArrayList<Integer> preorder) {
  14. if (null == root) {
  15. return;
  16. }
  17. preorder.add(root.val);
  18. preorderTraversal(root.left, preorder);
  19. preorderTraversal(root.right, preorder);
  20. }
  21. }

 

(2)非递归(必考):

 

2,--------------------Binary Tree Inorder Traversal(二叉树的中序遍历)

(1)用递归:bug-free;

  1. public ArrayList<Integer> inorderTraversal(TreeNode root) {
  2. // write your code here
  3. ArrayList<Integer> res = new ArrayList();
  4. inorderTraversal(root, res);
  5. return res;
  6. }
  7. public static void inorderTraversal(TreeNode root, ArrayList<Integer> list) {
  8. if (root == null) {
  9. return;
  10. }
  11. inorderTraversal(root.left, list);
  12. list.add(root.val);
  13. inorderTraversal(root.right, list);
  14. }

 (2)非递归(必考):

3,---------------Binary Tree Postorder Traversal(二叉树的后序遍历)

(1)用递归:bug-free;

  1. public ArrayList<Integer> postorderTraversal(TreeNode root) {
  2. // write your code here
  3. ArrayList<Integer> res = new ArrayList();
  4. postorderTraversal(root, res);
  5. return res;
  6. }
  7. public static void postorderTraversal(TreeNode root, ArrayList<Integer> list) {
  8. if (root == null) {
  9. return;
  10. }
  11. postorderTraversal(root.left, list);
  12. postorderTraversal(root.right, list);
  13. list.add(root.val);
  14. }

(2)非递归(必考): 

4,--------------------Maximum Depth of Binary Tree(二叉树的最大深度)

 答案:bug-free;

  1. public int maxDepth(TreeNode root) {
  2. // write your code here
  3. if (root == null) {
  4. return 0;
  5. }
  6. return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
  7. }

 5,--------------Balanced Binary Tree(平衡二叉树)

(1)答案:

  1. public class Solution {
  2. /**
  3. * @param root: The root of binary tree.
  4. * @return: True if this Binary tree is Balanced, or false.
  5. */
  6. public boolean isBalanced(TreeNode root) {
  7. // write your code here
  8. if (root == null) {
  9. return true;
  10. }
  11. return (Math.abs(height(root.right) - height(root.left)) <= 1) && isBalanced(root.left) && isBalanced(root.right);
  12. }
  13. public static int height(TreeNode root) {
  14. if (null == root) {
  15. return 0;
  16. }
  17. return Math.max(height(root.left), height(root.right)) + 1;
  18. }
  19. }

(2)一刷需要2次才能AC。主要是错在,一些书写错误,少了函数名之类的。

(2)二刷:bug-free;

 

6,----------------Lowest Common Ancestor(最近公共祖先)

(1)答案:思路在代码中注释了

  1. public class Solution {
  2. /**
  3. * @param root: The root of the binary search tree.
  4. * @param A and B: two nodes in a Binary.
  5. * @return: Return the least common ancestor(LCA) of the two nodes.
  6. */
  7. public TreeNode lowestCommonAncestor(TreeNode root, TreeNode a, TreeNode b) {
  8. // write your code here
  9. if (root == null || root == a || root == b) {
  10. return root;
  11. }
  12. TreeNode left = lowestCommonAncestor(root.left, a, b);
  13. TreeNode right = lowestCommonAncestor(root.right, a, b);
  14. if (left != null && right != null) {
  15. return root;
  16. } else if (left != null) {
  17. return left;
  18. } else {
  19. return right;
  20. }
  21. }
  22. }

 

(2)一刷需要三次才能AC: 首先是没搞懂怎么弄。 其次是||与&&的错用。

(3)二刷:bug-free;

7,----------------Binary Tree Maximum Path Sum(二叉树中的最大路径和)

(1)思路:思路:这里的最大路劲和,其实可以这么理解,1,过root的;2,不过root的。这里又分为两种,就是过left的,过right的,然后递归就可以了。可以认为是这条路径总是要从某一个结点开始往下走的

  1. public class Solution {
  2. /**
  3. * @param root: The root of binary tree.
  4. * @return: An integer.
  5. */
  6. static int max = Integer.MIN_VALUE;
  7. public int maxPathSum(TreeNode root) {
  8. // write your code here
  9. // 思路:这里的最大路劲和,其实可以这么理解,1,过root的;2,不过root的。这里又分为两种,就是过left的,过right的,然后递归就可以了。可以认为是这条路径总是要从某一个结点开始往下走的。
  10. if (null == root) {
  11. return 0;
  12. }
  13. path(root);
  14. return max;
  15. }
  16. public static int path(TreeNode root) {
  17. if (root == null) {
  18. return 0;
  19. }
  20. int leftPath = path(root.left);
  21. int rightPath = path(root.right);
  22. int curMax = root.val + Math.max(0, Math.max(leftPath, rightPath));
  23. max = Math.max(max, Math.max(curMax, root.val + leftPath + rightPath));
  24. return curMax;
  25. }
  26. }

 

(2)一刷没做对。

(2)Binary Tree Maximum Path Sum II( 二叉树中的最大路径和)

第二种情况,这里比较easy,因为要求过root:

(1)答案:注意遇到了树的题目,首先考虑他和他的左右子树之间有什么联系。

  1. public int maxPathSum2(TreeNode root) {
  2. // Write your code here
  3. if (null == root) {
  4. return 0;
  5. }
  6. int leftSum = maxPathSum2(root.left);
  7. int rightSum = maxPathSum2(root.right);
  8. return root.val + Math.max(0, Math.max(leftSum, rightSum));
  9. }

(2)一刷没做对;

 

8,------------------Binary Tree Level Order Traversal(二叉树的层次遍历)

(1)答案:注意,其实每一层就是当前queue还有的都是上一层的,所以用好queue.size来做循环。此外注意queue的add是offer。pop是poll。打好基础知识。

  1. public class Solution {
  2. /**
  3. * @param root: The root of binary tree.
  4. * @return: Level order a list of lists of integer
  5. */
  6. public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
  7. // write your code here
  8. ArrayList<ArrayList<Integer>> levelOrder = new ArrayList();
  9. if (null == root) {
  10. return levelOrder;
  11. }
  12. Queue<TreeNode> queue = new LinkedList();
  13. queue.add(root);
  14. while (!queue.isEmpty()) {
  15. ArrayList<Integer> list = new ArrayList();
  16. int size = queue.size();
  17. for (int i = 0; i < size; i++) {
  18. TreeNode top = queue.poll();
  19. if (top.left != null) {
  20. queue.add(top.left);
  21. }
  22. if (top.right != null) {
  23. queue.add(top.right);
  24. }
  25. list.add(top.val);
  26. }
  27. levelOrder.add(list);
  28. }
  29. return levelOrder;
  30. }
  31. }

(2) 一刷需要两次AC:注意一下,queue.size()是一直在改变的,所以,循环条件不能用它,而应该是先记下来上一层的size。

(3)二刷bug-free;

9,--------------------Binary Tree Level Order Traversal II(二叉树的层次遍历 II)

(1) 答案:注意,和上一题的差别就在于利用了list的add插入。

  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. public class Solution {
  4. /**
  5. * @param root: The root of binary tree.
  6. * @return: buttom-up level order a list of lists of integer
  7. */
  8. public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
  9. // write your code here
  10. ArrayList<ArrayList<Integer>> levelOrder = new ArrayList();
  11. if (null == root) {
  12. return levelOrder;
  13. }
  14. Queue<TreeNode> queue = new LinkedList();
  15. queue.add(root);
  16. while (!queue.isEmpty()) {
  17. ArrayList<Integer> list = new ArrayList();
  18. int size = queue.size();
  19. for (int i = 0; i < size; i++) {
  20. TreeNode top = queue.poll();
  21. list.add(top.val);
  22. if (top.left != null) {
  23. queue.add(top.left);
  24. }
  25. if (top.right != null) {
  26. queue.add(top.right);
  27. }
  28. }
  29. levelOrder.add(0, list);
  30. }
  31. return levelOrder;
  32. }
  33. }

(2) 一刷的时候bug-free;

 

10,---------------Binary Tree Zigzag Level Order Traversal(二叉树的锯齿形层次遍历)

答案。需要逆序的那一层就利用add(位置,值)。然后用flag来控制是否逆过来。

  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. public class Solution {
  5. /**
  6. * @param root: The root of binary tree.
  7. * @return: A list of lists of integer include
  8. * the zigzag level order traversal of its nodes' values
  9. */
  10. public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
  11. // write your code here
  12. ArrayList<ArrayList<Integer>> traversal = new ArrayList();
  13. if (root == null) {
  14. return traversal;
  15. }
  16. Queue<TreeNode> queue = new LinkedList();
  17. int level = 0;
  18. queue.add(root);
  19. while (!queue.isEmpty()) {
  20. ArrayList<Integer> list = new ArrayList();
  21. int size = queue.size();
  22. for (int i = 0; i < size; i++) {
  23. TreeNode top = queue.poll();
  24. if (top.left != null) {
  25. queue.add(top.left);
  26. }
  27. if (top.right != null) {
  28. queue.add(top.right);
  29. }
  30. if (level % 2 == 0) {
  31. list.add(top.val);
  32. } else {
  33. list.add(0, top.val);
  34. }
  35. }
  36. traversal.add(list);
  37. level++;
  38. }
  39. return traversal;
  40. }
  41. }

(2)一刷的时候bug-free;

 11,---------------------(验证二叉查找树)

答案:注意,int不够了。

  1. public boolean isValidBST(TreeNode root) {
  2. // write your code here
  3. return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
  4. }
  5. public static boolean isValidBST(TreeNode root, long left, long right) {
  6. if (root == null) {
  7. return true;
  8. }
  9. return (root.val > left && root.val < right) && isValidBST(root.left, left, root.val) && isValidBST(root.right, root.val, right);
  10. }

 12,-------- Inorder Successor in Binary Search Tree(中序后继)

(1)答案:这是search tree;利用好search tree的性质。

  1. public class Solution {
  2. public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
  3. // write your code here
  4. if (root == null) {
  5. return null;
  6. }
  7. TreeNode successor = null;
  8. while (root != null) {
  9. if (root != p && root.val > p.val) {
  10. successor = root;
  11. root = root.left;
  12. } else {
  13. root = root.right;
  14. }
  15. }
  16. return successor;
  17. }
  18. }

 (2)一定要仔细阅读题目,这是search tree;利用好search tree的性质。

 (3)二刷:bug-free;

 13,---------------------Binary Search Tree Iterator( 二叉查找树迭代器)

答案:

  1. public class BSTIterator {
  2. //@param root: The root of binary tree.
  3. ArrayList<TreeNode> list = new ArrayList<TreeNode>();
  4. int flag = 0;
  5. public BSTIterator(TreeNode root) {
  6. // write your code here
  7. list = inorderTraversal(root);
  8. }
  9. //@return: True if there has next node, or false
  10. public boolean hasNext() {
  11. // write your code here
  12. if (list == null || flag >= list.size()) {
  13. return false;
  14. }
  15. return true;
  16. }
  17. //@return: return next node
  18. public TreeNode next() {
  19. // write your code here
  20. if (list == null || flag >= list.size()) {
  21. return null;
  22. }
  23. return list.get(flag++);
  24. }
  25. public static ArrayList<TreeNode> inorderTraversal(TreeNode root) {
  26. // write your code here
  27. ArrayList<TreeNode> res = new ArrayList();
  28. inorderTraversal(root, res);
  29. return res;
  30. }
  31. public static void inorderTraversal(TreeNode root, ArrayList<TreeNode> list) {
  32. if (root == null) {
  33. return;
  34. }
  35. inorderTraversal(root.left, list);
  36. list.add(root);
  37. inorderTraversal(root.right, list);
  38. }
  39. }

 14,-----------------Search Range in Binary Search Tree(二叉查找树中搜索区间)

答案:

  1. public ArrayList<Integer> searchRange(TreeNode root, int k1, int k2) {
  2. // write your code here
  3. ArrayList<Integer> res = new ArrayList();
  4. searchRange(root, k1, k2, res);
  5. return res;
  6. }
  7. public static void searchRange(TreeNode root, int k1, int k2, ArrayList<Integer> res) {
  8. if (root == null) {
  9. return;
  10. }
  11. if (root.val <= k2 && root.val >= k1) {
  12. searchRange(root.left, k1, k2, res);
  13. res.add(root.val);
  14. searchRange(root.right, k1, k2, res);
  15. } else if (root.val > k2) {
  16. searchRange(root.left, k1, k2, res);
  17. } else {
  18. searchRange(root.right, k1, k2, res);
  19. }
  20. }

 

 

-------------------------------------------

 第二周:二分查找:

-------------------------------------------

 

1,Classical Binary Search(二分查找)

答案:bug-free;

  1. public static int findPosition(int[] nums, int target) {
  2. if(nums == null || nums.length == 0) {
  3. return -1;
  4. }
  5. int start = 0;
  6. int end = nums.length - 1;
  7. while (start <= end){
  8. int mid = start + (end - start) / 2;
  9. if (nums[mid] == target){
  10. return mid;
  11. } else if (nums[mid] < target) {
  12. start = mid + 1;
  13. } else {
  14. end = mid - 1;
  15. }
  16. }
  17. return -1;
  18. }

2,First Position of Target(找出现的第一个位置)

 答案:bug-free;

  1. public static int binarySearch(int[] nums, int target) {
  2. if (nums == null || nums.length == 0) {
  3. return -1;
  4. }
  5. int res = -1;
  6. int start = 0;
  7. int end = nums.length - 1;
  8. while (start <= end){
  9. int mid = start + (end - start) / 2;
  10. if (nums[mid] == target){
  11. res = mid;
  12. end = mid - 1;
  13. } else if (nums[mid] < target) {
  14. start = mid + 1;
  15. } else {
  16. end = mid - 1;
  17. }
  18. }
  19. return res;
  20. }

3,Last Position of Target(找出现的最后一个位置)

答案: bug-free;

  1. if(nums == null || nums.length == 0) {
  2. return -1;
  3. }
  4. int res = -1;
  5. int start = 0;
  6. int end = nums.length - 1;
  7. while (start <= end){
  8. int mid = start + (end - start) / 2;
  9. if (nums[mid] == target){
  10. res = mid;
  11. start = mid + 1;
  12. } else if (nums[mid] < target) {
  13. start = mid + 1;
  14. } else {
  15. end = mid - 1;
  16. }
  17. }
  18. return res;

 4,sqrt(x)求平方根

  1. x的平方根
  2.  
  3. 描述
  4. 笔记
  5. 数据
  6. 评测
  7. 实现 int sqrt(int x) 函数,计算并返回 x 的平方根。
  8.  
  9. 您在真实的面试中是否遇到过这个题? Yes
  10. 样例
  11. sqrt(3) = 1
  12.  
  13. sqrt(4) = 2
  14.  
  15. sqrt(5) = 2
  16.  
  17. sqrt(10) = 3

(1)答案和思路:利用二分来做,注意的是,要特判0.而且注意start开始位置是1.结束位置是x,如果要让他是x/2结束,那么特判1.

  1. class Solution {
  2. /**
  3. * @param x: An integer
  4. * @return: The sqrt of x
  5. */
  6. public int sqrt(int x) {
  7. if (x < 0) {
  8. return -1;
  9. }
  10. if (x <= 1) {
  11. return x;
  12. }
  13. int start = 1;
  14. int end = x / 2;
  15. int res = -1;
  16. while (start <= end) {
  17. int mid = start + (end - start) / 2;
  18. if (x / mid == mid) {
  19. return mid;
  20. } else if (x / mid < mid) {
  21. end = mid - 1;
  22. } else {
  23. res = mid;
  24. start = mid + 1;
  25. }
  26. }
  27. return res;
  28. }
  29. }

(2)一刷AC所需次数2次:所犯错误有:1,没有特判0,导致分母为0; 2,start从0开始,导致分母为0;

(3)二刷:bug-free;

5,search a 2D matrix (矩阵里面查找元素)

(1)答案和思路:也是用二分来找找。注意的就是,这里会用到两次二分,所以,start,end,left,right什么的要分清楚了。

答案:

  1. public boolean searchMatrix(int[][] matrix, int target) {
  2. if (null == matrix || matrix.length == 0) {
  3. return false;
  4. }
  5. //find row
  6. int start = 0;
  7. int end = matrix.length - 1;
  8. int row = -1;
  9. while (start <= end) {
  10. int mid = start + (end - start) / 2;
  11. if (matrix[mid][0] == target) {
  12. return true;
  13. } else if (matrix[mid][0] < target) {
  14. row = mid;
  15. start = mid + 1;
  16. } else {
  17. end = mid - 1;
  18. }
  19. }
  20. if (row == -1) {
  21. return false;
  22. }
  23. start = 0;
  24. end = matrix[row].length - 1;
  25. while (start <= end) {
  26. int mid = start + (end - start) / 2;
  27. if (matrix[row][mid] == target) {
  28. return true;
  29. } else if (matrix[row][mid] < target) {
  30. start = mid + 1;
  31. } else {
  32. end = mid - 1;
  33. }
  34. }
  35. return false;
  36. }

(2)一刷所需AC次数3次:所犯错误:1,end,right混淆。导致死循环。2,变量row,res混淆。

(3)二刷:bug-free;

 5,search insert position(搜索要插入的位置)

(1)答案和思路:注意的是无论大小都要记下index可以取的位置

(2)一刷要AC次数3次:所犯错误:1,当a == null || a.length == 0时候,记住不是返回-1,而是0;

(3)二刷:bug-free;

 6,Count of Smaller Number(统计比给定整数小的数的个数)

(1)答案和思路:利用二分来做。注意的地方:如果a == null。那么不能return null;所以一定要注意特判地方不要随时返回-1;

  1. import java.util.Arrays;
  2. public class Solution {
  3. /**
  4. * @param A: An integer array
  5. * @return: The number of element in the array that
  6. * are smaller that the given integer
  7. */
  8. public ArrayList<Integer> countOfSmallerNumber(int[] a, int[] queries) {
  9. // write your code here
  10. ArrayList<Integer> res = new ArrayList();
  11. if (a == null || queries == null) {
  12. return res;
  13. }
  14. Arrays.sort(a);
  15. for (int i = 0; i < queries.length; i++) {
  16. res.add(binarySearch(a, queries[i]));
  17. }
  18. return res;
  19. }
  20. public static int binarySearch(int[] a, int target) {
  21. if (a == null || a.length == 0) {
  22. return 0;
  23. }
  24. int start = 0;
  25. int end = a.length - 1;
  26. int res = 0;
  27. while (start <= end) {
  28. int mid = start + (end - start) / 2;
  29. if (a[mid] < target) {
  30. res = mid + 1;
  31. start = mid + 1;
  32. } else {
  33. end = mid - 1;
  34. }
  35. }
  36. return res;
  37. }
  38. }

 (2)一刷AC需要三次。所犯错误a==null,return res;

(3)二刷:没有bug-free。所犯错误,a==null || a.length == 0,不能return -1;

7,------------------Search for a Range(搜索区间)

 答案:bug-free;

  1. public class Solution {
  2. /**
  3. *@param A : an integer sorted array
  4. *@param target : an integer to be inserted
  5. *return : a list of length 2, [index1, index2]
  6. */
  7. public int[] searchRange(int[] a, int target) {
  8. // write your code here
  9. int[] res = new int[2];
  10. res[0] = -1;
  11. res[1] = -1;
  12. if (a == null || a.length == 0) {
  13. return res;
  14. }
  15. res[0] = findStartPosition(a, target);
  16. res[1] = findEndPosition(a, target);
  17. return res;
  18. }
  19. public static int findStartPosition(int[] a, int target) {
  20. if (a == null || a.length == 0) {
  21. return -1;
  22. }
  23. int start = 0;
  24. int end = a.length - 1;
  25. int res = -1;
  26. while (start <= end) {
  27. int mid = start + (end - start) / 2;
  28. if (a[mid] == target) {
  29. res = mid;
  30. end = mid - 1;
  31. } else if (a[mid] < target) {
  32. start = mid + 1;
  33. } else {
  34. end = mid - 1;
  35. }
  36. }
  37. return res;
  38. }
  39. public static int findEndPosition(int[] a, int target) {
  40. if (a == null || a.length == 0) {
  41. return -1;
  42. }
  43. int start = 0;
  44. int end = a.length - 1;
  45. int res = -1;
  46. while (start <= end) {
  47. int mid = start + (end - start) / 2;
  48. if (a[mid] == target) {
  49. res = mid;
  50. start = mid + 1;
  51. } else if (a[mid] < target) {
  52. start = mid + 1;
  53. } else {
  54. end = mid - 1;
  55. }
  56. }
  57. return res;
  58. }
  59. }

 8,------------------First Bad Version(第一个错误的代码版本)

答案:注意start从1开始; bug-free;

  1. public int findFirstBadVersion(int n) {
  2. if (n < 0) {
  3. return -1;
  4. }
  5. int start = 0;
  6. int end = n;
  7. int res = -1;
  8. while (start <= end) {
  9. int mid = start + (end - start) / 2;
  10. if (SVNRepo.isBadVersion(mid)) {
  11. res = mid;
  12. end = mid - 1;
  13. } else {
  14. start = mid + 1;
  15. }
  16. }
  17. return res;
  18. }

 9,-----------------Search in a Big Sorted Array(在大数组中查找)

(1)答案和思路:利用二分。注意的地方有:1,不要错把continue用成了break。2,注意二分题目,找的往往是第一个位置

  1. public class Solution {
  2. /**
  3. * @param reader: An instance of ArrayReader.
  4. * @param target: An integer
  5. * @return : An integer which is the index of the target number
  6. */
  7. public int searchBigSortedArray(ArrayReader reader, int target) {
  8. // write your code here
  9. if (reader == null) {
  10. return -1;
  11. }
  12. int start = 0;
  13. int end = Integer.MAX_VALUE;
  14. int res = -1;
  15. while (start <= end) {
  16. int mid = start + (end - start) / 2;
  17. if (reader.get(mid) == -1) {
  18. end = mid - 1;
  19. continue;
  20. }
  21. if (reader.get(mid) == target) {
  22. res = mid;
  23. end = mid - 1;
  24. } else if (reader.get(mid) < target) {
  25. start = mid + 1;
  26. } else {
  27. end = mid - 1;
  28. }
  29. }
  30. return res;
  31. }
  32. }

 (2)一刷AC需要次数:3次;所犯错误有:1,break;2,没有注意有重复元素。

(3)二刷:bug-free;

 10,-------------Find Minimum in Rotated Sorted Array(寻找旋转排序数组中的最小值)

答案:注意end的位置是length - 1;bug-free;

  1. public static int findMin(int[] num) {
  2. int res = Integer.MAX_VALUE;
  3. if (num == null || num.length == 0) {
  4. return -1;
  5. }
  6. if (num[0] < num[num.length - 1]) {
  7. return num[0];
  8. } else {
  9. int start = 0;
  10. int end = num.length - 1; // error
  11. while (start <= end) {
  12. int mid = start + (end - start) / 2;
  13. if (num[mid] <= num[end]) {
  14. res = Math.min(res, num[mid]);
  15. end = mid - 1;
  16. } else {
  17. start = mid + 1;
  18. }
  19. }
  20. }
  21. return res;
  22. }

 

 11,------------Search in Rotated Sorted Array(搜索旋转排序数组)

答案:记住end从length - 1开始。bug-free;

  1. public class Solution {
  2. /**
  3. *@param A : an integer rotated sorted array
  4. *@param target : an integer to be searched
  5. *return : an integer
  6. */
  7. public int search(int[] a, int target) {
  8. // write your code here
  9. if (a == null || a.length == 0) {
  10. return -1;
  11. }
  12. int start = 0;
  13. int end = a.length - 1;
  14. while (start <= end) {
  15. int mid = start + (end - start) / 2;
  16. if (a[mid] == target) {
  17. return mid;
  18. } else if (a[mid] <= a[end]) {
  19. if (target > a[mid] && target <= a[end]) {
  20. start = mid + 1;
  21. } else {
  22. end = mid - 1;
  23. }
  24. } else {
  25. if (target >= a[start] && target < a[mid]) {
  26. end = mid - 1;
  27. } else {
  28. start = mid + 1;
  29. }
  30. }
  31. }
  32. return -1;
  33. }
  34. }

 

 12,---------------Find Peak Element( 寻找峰值)

答案:切记不要忽略了最重要的条件,不然代码好长好丑。bug-free;

  1. class Solution {
  2. /**
  3. * @param A: An integers array.
  4. * @return: return any of peek positions.
  5. */
  6. public int findPeak(int[] a) {
  7. // write your code here
  8. if (a == null || a.length <= 2) {
  9. return -1;
  10. }
  11. int start = 1;
  12. int end = a.length -2;
  13. while (start <= end) {
  14. int mid = start + (end - start) / 2;
  15. if (a[mid] > a[mid - 1] && a[mid] > a[mid + 1]) {
  16. return mid;
  17. } else if (a[mid] < a[mid - 1]) {
  18. end = mid - 1;
  19. } else {
  20. start = mid + 1;
  21. }
  22. }
  23. return -1;
  24. }
  25. }

 

 

 13,-----------Recover Rotated Sorted Array(恢复旋转排序数组)

(1)答案:注意最新答案,利用了二分查到到开始,然后利用逆序的逆序来解题。这是不重复元素的做法。是错的。

  1. public class Solution {
  2. /**
  3. * @param nums: The rotated sorted array
  4. * @return: void
  5. */
  6. public static void recoverRotatedSortedArray(ArrayList<Integer> nums) {
  7. int index = findMin(nums);
  8. reverseList(nums, 0, index - 1);
  9. reverseList(nums, index, nums.size() - 1);
  10. reverseList(nums, 0, nums.size() - 1);
  11. }
  12. public static void reverseList(ArrayList<Integer> num, int start, int end) {
  13. for (int i = start, j = end; i < j; i++, j--) {
  14. int temp = num.get(i);
  15. num.set(i, num.get(j));
  16. num.set(j, temp);
  17. }
  18. }
  19. public static int findMin(ArrayList<Integer> num) {
  20. int res = Integer.MAX_VALUE;
  21. int ans = 0;
  22. if (num == null || num.size() == 0) {
  23. return -1;
  24. }
  25. if (num.get(0) < num.get(num.size() - 1)) {
  26. return 0;
  27. } else {
  28. int start = 0;
  29. int end = num.size() - 1; // error
  30. while (start <= end) {
  31. int mid = start + (end - start) / 2;
  32. if (num.get(mid) <= num.get(end)) {
  33. if (num.get(mid) < res) {
  34. res = num.get(mid);
  35. ans = mid;
  36. }
  37. end = mid - 1;
  38. } else {
  39. start = mid + 1;
  40. }
  41. }
  42. }
  43. return ans;
  44. }
  45. }

当含有重复元素的时候, 答案是这个:

  1. import java.util.ArrayList;
  2. public class Solution {
  3. /**
  4. * @param nums: The rotated sorted array
  5. * @return: void
  6. */
  7. public void recoverRotatedSortedArray(ArrayList<Integer> nums) {
  8. // write your code
  9. if (null == nums || nums.size() == 0) {
  10. return;
  11. }
  12. for (int i = 0; i < nums.size() - 1; i++) {
  13. if (nums.get(i) > nums.get(i + 1)) {
  14. reverseArrayList(nums, 0, i);
  15. reverseArrayList(nums, i + 1, nums.size() - 1);
  16. reverseArrayList(nums, 0, nums.size() - 1);
  17. }
  18. }
  19. }
  20. // reverse the ArrayList
  21. public static void reverseArrayList(ArrayList<Integer> a, int start, int end) {
  22. // swap the a.get(start) and the a.get(end)
  23. /** Error:
  24. *while (start < end) {
  25. * a.get(start) = a.get(start) ^ a.get(end);
  26. * a.get(end) = a.get(start) ^ a.get(end);
  27. * a.get(start) = a.get(start) ^ a.get(end);
  28. * start++;
  29. * end--;
  30. * }
  31. */
  32. while (start < end) {
  33. a.set(start, a.get(start) ^ a.get(end));
  34. a.set(end, a.get(start) ^ a.get(end));
  35. a.set(start, a.get(start) ^ a.get(end));
  36. start++;
  37. end--;
  38. }
  39. }
  40. }

(2)一刷不能bug-free,因为用了二分来做,是错的。

(3)二刷,bug-free;

 14.-------------Rotate String(旋转字符串)

(1)答案

  1. public class Solution {
  2. /**
  3. * @param str: an array of char
  4. * @param offset: an integer
  5. * @return: nothing
  6. */
  7. public void rotateString(char[] str, int offset) {
  8. if (null == str || str.length <= 1) {
  9. return;
  10. }
  11. offset = offset % str.length;
  12. reverseString(str, 0, str.length - offset - 1);
  13. reverseString(str, str.length - offset, str.length - 1);
  14. reverseString(str, 0, str.length - 1);
  15. }
  16. public static void reverseString(char[] str, int start, int end) {
  17. if (null == str || str.length == 0) {
  18. return;
  19. }
  20. while (start < end) {
  21. // Error:
  22. // str[start] = str[start] ^ str[end];
  23. // str[end] = str[start] ^ str[end];
  24. // str[start] = str[start] ^str[end];
  25. str[start] = (char) (str[start] ^ str[end]);
  26. str[end] = (char) (str[start] ^ str[end]);
  27. str[start] = (char) (str[start] ^ str[end]);
  28. start++;
  29. end--;
  30. }
  31. }
  32. }

(2)1刷需要3次才能AC:因为,1:当字符串^时候结果得到的是整数,需要(char);

(2)2刷。bug-free;

 

-------------------------------------------

第一周:入门式。字符串,子集,排列

-------------------------------------------

 

1,strStr(字符串查找)

  1. 字符串查找
  2.  
  3. 描述
  4. 笔记
  5. 数据
  6. 评测
  7. 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1

(1)答案和思路:就是常规的一一对比,注意一下循环的结束位置即可。

  1. import java.lang.String;
  2. class Solution {
  3. /**
  4. * Returns a index to the first occurrence of target in source,
  5. * or -1 if target is not part of source.
  6. * @param source string to be scanned.
  7. * @param target string containing the sequence of characters to match.
  8. */
  9. public int strStr(String source, String target) {
  10. if (source == null || target == null) {
  11. return -1;
  12. }
  13. for (int i = 0; i < source.length() - target.length() + 1; i++) {
  14. int j = 0;
  15. for (j = 0; j < target.length(); j++) {
  16. if (source.charAt(i + j) != target.charAt(j)) {
  17. break;
  18. }
  19. }
  20. if (j == target.length()) {
  21. return i;
  22. }
  23. }
  24. return -1;
  25. }
  26. }

 

(2)AC所需次数:4.所犯错误有:String的length,应该是length(); 第一个字符串的终止位置应该是他的长度减去比较字符串的长度。所以注意不是<而是<=。import java.lang.String;

 (3) 二刷AC所需次数:bug-free;

2,Subsets(子集)

  1. 子集
  2.  
  3. 描述
  4. 笔记
  5. 数据
  6. 评测
  7. 给定一个含不同整数的集合,返回其所有的子集
  8.  
  9. 注意事项
  10.  
  11. 子集中的元素排列必须是非降序的,解集必须不包含重复的子集

(1)答案和思路:每放入一个元素,除了保留已有的那些子集之外,在这些已有的每一个子集上加上新来的元素。

 

  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. class Solution {
  4. /**
  5. * @param S: A set of numbers.
  6. * @return: A list of lists. All valid subsets.
  7. */
  8. public ArrayList<ArrayList<Integer>> subsets(int[] nums) {
  9. Arrays.sort(nums);
  10. ArrayList<ArrayList<Integer>> subsets = new ArrayList();
  11. ArrayList<Integer> subset = new ArrayList();
  12. subsets.add(subset);
  13. if (nums == null || nums.length == 0) {
  14. return subsets;
  15. }
  16. for (int i = 0; i < nums.length; i++) {
  17. ArrayList<ArrayList<Integer>> tempSubsets = new ArrayList(subsets);
  18. for (ArrayList<Integer> preSubset : subsets) {
  19. ArrayList<Integer> curSubset = new ArrayList(preSubset);
  20. curSubset.add(nums[i]);
  21. tempSubsets.add(curSubset);
  22. }
  23. subsets = new ArrayList(tempSubsets);
  24. }
  25. return subsets;
  26. }
  27. }

 

(2)一刷AC所需次数:3次。所犯错误:加入新来的元素,在遍历的时候加,那样已经改变了原来的子集。所以要遍历出一个子集,然后new出一个子集,再add。 还有一个错误就是因为要保证子集的非降序,所以要先排序。

 (3) 二刷AC所需次数:2次。所犯错误:sort。

 

3,Subsets II(带重复元素的子集)

  1. 带重复元素的子集
  2.  
  3. 描述
  4. 笔记
  5. 数据
  6. 评测
  7. 给定一个可能具有重复数字的列表,返回其所有可能的子集

(1)答案和思路:与1不同的地方就是在add进去的时候,用一下contains。

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. class Solution {
  4. /**
  5. * @param S: A set of numbers.
  6. * @return: A list of lists. All valid subsets.
  7. */
  8. public ArrayList<ArrayList<Integer>> subsetsWithDup(ArrayList<Integer> s){
  9. ArrayList<ArrayList<Integer>> subsets = new ArrayList();
  10. ArrayList<Integer> subset = new ArrayList();
  11. subsets.add(subset);
  12. if (s == null || s.size() == 0) {
  13. return subsets;
  14. }
  15. Collections.sort(s);
  16. for (int num : s) {
  17. ArrayList<ArrayList<Integer>> tempSubsets = new ArrayList(subsets);
  18. for (ArrayList<Integer> preSubset : subsets) {
  19. ArrayList<Integer> curSubset = new ArrayList(preSubset);
  20. curSubset.add(num);
  21. if (!tempSubsets.contains(curSubset)) {
  22. tempSubsets.add(curSubset);
  23. }
  24. }
  25. subsets = new ArrayList(tempSubsets);
  26. }
  27. return subsets;
  28. }
  29. }

(2)一刷AC需要三次,所犯错误为:1,忘记排序;2,不会对list排序:import java.util.Collections; Collections.sort(list);3,tempSubsets 错写temp;

(3)二刷:bug-free;

 

4,Permutations(全排列)

  1. 全排列
  2.  
  3. 描述
  4. 笔记
  5. 数据
  6. 评测
  7. 给定一个数字列表,返回其所有可能的排列。

(1)答案和思路:就是每次来一个数,就把他插入到已有的permutation中。

  1. import java.util.ArrayList;
  2. class Solution {
  3. /**
  4. * @param nums: A list of integers.
  5. * @return: A list of permutations.
  6. */
  7. public ArrayList<ArrayList<Integer>> permute(ArrayList<Integer> nums) {
  8. // write your code here
  9. ArrayList<ArrayList<Integer>> permutations = new ArrayList();
  10. ArrayList<Integer> permutation = new ArrayList();
  11. if (nums == null || nums.size() == 0) {
  12. return permutations;
  13. }
  14. permutations.add(permutation);
  15. for (int num : nums) {
  16. ArrayList<ArrayList<Integer>> tempPermutations = new ArrayList();
  17. for (ArrayList<Integer> prePermutation : permutations) {
  18. for (int i = 0; i <= prePermutation.size(); i++) {
  19. ArrayList<Integer> curPermutation = new ArrayList(prePermutation);
  20. curPermutation.add(i, num);
  21. tempPermutations.add(curPermutation);
  22. }
  23. }
  24. permutations = new ArrayList(tempPermutations);
  25. }
  26. return permutations;
  27. }
  28. }

(2)一刷AC需要两次:所犯错误:1,当输入null的时候全排列和子集是不一样的,子集是[[]]。全排列是[],也就是[null]。所以,在判断之前不能add任何东西。

(3)二刷:bug-free;

5,Permutations(带重复元素的全排列)

  1. 带重复元素的排列
  2.  
  3. 描述
  4. 笔记
  5. 数据
  6. 评测
  7. 给出一个具有重复数字的列表,找出列表所有不同的排列。

(1)答案和思路:在add之前进行一次判断就可以了。

  1. import java.util.ArrayList;
  2. class Solution {
  3. /**
  4. * @param nums: A list of integers.
  5. * @return: A list of permutations.
  6. */
  7. public ArrayList<ArrayList<Integer>> permuteUnique(ArrayList<Integer> nums) {
  8. // write your code here
  9. ArrayList<ArrayList<Integer>> permutations = new ArrayList();
  10. ArrayList<Integer> permutation = new ArrayList();
  11. if (nums == null || nums.size() == 0) {
  12. return permutations;
  13. }
  14. permutations.add(permutation);
  15. for (int num : nums) {
  16. ArrayList<ArrayList<Integer>> tempPermutations = new ArrayList();
  17. for (ArrayList<Integer> prePermutation : permutations) {
  18. for (int i = 0; i <= prePermutation.size(); i++) {
  19. ArrayList<Integer> curPermutation = new ArrayList(prePermutation);
  20. curPermutation.add(i, num);
  21. if (!tempPermutations.contains(curPermutation)) {
  22. tempPermutations.add(curPermutation);
  23. }
  24. }
  25. }
  26. permutations = new ArrayList(tempPermutations);
  27. }
  28. return permutations;
  29. }
  30. }

(2)一刷:bug-free;

 

(lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)的更多相关文章

  1. “全栈2019”Java第九十九章:局部内部类与继承详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  2. “全栈2019”Java第四十九章:重载与重写对比详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  3. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

  4. 栈 队列 hash表 堆 算法模板和相关题目

    什么是栈(Stack)? 栈(stack)是一种采用后进先出(LIFO,last in first out)策略的抽象数据结构.比如物流装车,后装的货物先卸,先转的货物后卸.栈在数据结构中的地位很重要 ...

  5. LeetCode题目解答

    LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...

  6. Python之路【第十九章】:Django进阶

    Django路由规则 1.基于正则的URL 在templates目录下创建index.html.detail.html文件 <!DOCTYPE html> <html lang=&q ...

  7. 第十九章——使用资源调控器管理资源(1)——使用SQLServer Management Studio 配置资源调控器

    原文:第十九章--使用资源调控器管理资源(1)--使用SQLServer Management Studio 配置资源调控器 本系列包含: 1. 使用SQLServer Management Stud ...

  8. 第十九章——使用资源调控器管理资源(2)——使用T-SQL配置资源调控器

    原文:第十九章--使用资源调控器管理资源(2)--使用T-SQL配置资源调控器 前言: 在前一章已经演示了如何使用SSMS来配置资源调控器.但是作为DBA,总有需要写脚本的时候,因为它可以重用及扩展. ...

  9. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

随机推荐

  1. go http.Get请求 http.Post请求 http.PostForm请求 Client 超时设置

    http中有Get/Post/PostForm方法 也可以通过http包中设置client 请求配置 ,然后通过client.Do方法实现请求 下demo中功能都实现,其中有详细说明: package ...

  2. Winscp开源的SSH|SFTP

    WinSCP 主要功能 图形用户界面 多语言与 Windows 完美集成(拖拽, URL, 快捷方式) 支持所有常用文件操作,支持基于 SSH-1.SSH-2 的 SFTP 和 SCP 协议 支持批处 ...

  3. CentOS安装Redis详细教程

    构建 Redis redis 目前没有官方 RPM 安装包,我们需要从源代码编译,而为了要编译就需要安装 Make 和 GCC. 如果没有安装过 GCC 和 Make,那么就使用 yum 安装. yu ...

  4. 理解OAuth 2.0

    转自:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛 ...

  5. [基础] Array.prototype.indexOf()查询方式

    背景 最近在看Redux源码,createStore用于注册一个全局store,其内部维护一个Listeren数组,存放state变化时所有的响应函数. 其中store.subscribe(liste ...

  6. 精选30道Java笔试题解答

    转自:http://www.cnblogs.com/lanxuezaipiao/p/3371224.html 都 是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我 ...

  7. Django进阶(三)

    ORM 众所周知有很多不同的数据库系统,并且其中的大部分系统都包含Python接口,能够让我们更好的利用它们的功能,而这些系统唯一的缺点就是需要你了解SQL,如果你是一个更愿意操纵Python对象,而 ...

  8. ORB-SLAM(六)回环检测

    上一篇提到,无论在单目.双目还是RGBD中,追踪得到的位姿都是有误差的.随着路径的不断延伸,前面帧的误差会一直传递到后面去,导致最后一帧的位姿在世界坐标系里的误差有可能非常大.除了利用优化方法在局部和 ...

  9. cx_Oracle摘记

    由于想使用python操作oracle所以查看了cx_Oracle的官方文档,同时也查看了twisted中cx_Oracle的使用.下面是摘自文档中一些我认为有用的内容 cx_Oracle is a ...

  10. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...