【方法】

字写大点,先注释框架

链表:指针走就行了,最多是两个同时一起走。
两个链表求交点

  1. //corner case
  1. if (headA == null || headB == null) {
  2. return null;
  3. }
  4. //keep the same length
  5. int A_len = getLength(headA);
  6. int B_len = getLength(headB);
  7.  
  8. while (A_len > B_len) {
  9. headA = headA.next;
  10. A_len--;
  11. }
  12. while (A_len < B_len) {
  13. headB = headB.next;
  14. B_len--;
  15. }
  16. //find the same node
  17. while (headA != headB) {
  18. headA = headA.next;
  19. headB = headB.next;
  20. }

LCA:用两个点和要找的方向来DC

  1. public TreeNode lowestCommonAncestor(TreeNode root, TreeNode A, TreeNode B) {
  2. if (root == null || A == root || B == root) {//
  3. return root;
  4. }
  5. //divide
  6. TreeNode left = lowestCommonAncestor(root.left, A, B);
  7. TreeNode right = lowestCommonAncestor(root.right, A, B);
  8.  
  9. //conquer
  10. if (left != null && right != null) {
  11. return root;//
  12. }
  13. else if (left != null) {
  14. return left;
  15. }
  16. else if (right != null) {
  17. return right;
  18. }
  19. else {
  20. return null;
  21. }
  22. }

diameter of tree树的周长

  1. public int depth(TreeNode root) {
  2. //corner case
  3. if (root == null) {
  4. return 0;
  5. }
  6. //define left, right
  7. int left = depth(root.left);
  8. int right = depth(root.right);
  9. //renew max, don't add 1 since it's a sum of two lines
  10. max = Math.max(max, left + right);
  11. //return val for root, notice
  12. return Math.max(left, right) + 1;
  13. }

对称的树:先判断相等

  1. class Solution {
  2. public boolean isSubtree(TreeNode s, TreeNode t) {
  3. //corner case
  4. if (s == null) {
  5. return false;
  6. }
  7. if (isSame(s,t)) {
  8. return true;
  9. }
  10. return isSubtree(s.left, t) || isSubtree(s.right, t);
  11. }
  12.  
  13. public boolean isSame(TreeNode s, TreeNode t) {
  14. //both null
  15. if (s == null && t == null) {
  16. return true;
  17. }
  18. //one is null
  19. if (s == null || t == null) {
  20. return false;
  21. }
  22. //false
  23. if (s.val != t.val) {
  24. return false;
  25. }
  26. //default
  27. return isSame(s.left, t.left) && isSame(s.right, t.right);
  28. }
  29. }

同时要判断平衡和深度,自定义新的数据类型:

  1. /**
  2. * Definition of TreeNode:
  3. * public class TreeNode {
  4. * public int val;
  5. * public TreeNode left, right;
  6. * public TreeNode(int val) {
  7. * this.val = val;
  8. * this.left = this.right = null;
  9. * }
  10. * }
  11. */
  12.  
  13. public class Solution {
  14. /*
  15. * @param root: The root of binary tree.
  16. * @return: True if this Binary tree is Balanced, or false.
  17. */
  18. class ResultType {
  19. boolean isBalanced;
  20. int maxDepth;
  21. ResultType(boolean isBalanced,int maxDepth) {
  22. this.isBalanced = isBalanced;
  23. this.maxDepth = maxDepth;
  24. }
  25. };
  26.  
  27. public boolean isBalanced(TreeNode root) {
  28. return helper(root).isBalanced;
  29. }
  30.  
  31. private ResultType helper (TreeNode root) {
  32. if (root == null) {
  33. return new ResultType(true, 0);
  34. }
  35.  
  36. ResultType left = helper(root.left);
  37. ResultType right = helper(root.right);
  38.  
  39. if (!left.isBalanced || !right.isBalanced) {
  40. return new ResultType(false, - 1);
  41. }
  42.  
  43. else if (Math.abs(left.maxDepth - right.maxDepth) > 1) {
  44. return new ResultType(false, - 1);
  45. }
  46. else {
  47. return new ResultType(true, Math.max(left.maxDepth,right.maxDepth) + 1);
  48. }
  49. }
  50. }

Binary Tree Paths 用iteration

  1. void findBT(TreeNode root, String path, List<String> ans) {
  2. if (root.left == null && root.right == null)
  3. ans.add(path + root.val);//add here since it's an sign of end
  4. if (root.left != null) findBT(root.left, path + root.val + "->"参数是path,每次新加一个节点, ans);
  5. if (root.right != null) findBT(root.right, path + root.val + "->", ans);
  6. }

merge binary tree

  1. class Node
  2. {
  3. int data;
  4. Node left, right;
  5.  
  6. public Node(int data, Node left, Node right) {
  7. this.data = data;
  8. this.left = left;
  9. this.right = right;
  10. }
  11.  
  12. /* Helper method that allocates a new node with the
  13. given data and NULL left and right pointers. */
  14. static Node newNode(int data)
  15. {
  16. return new Node(data, null, null);
  17. }
  18.  
  19. /* Given a binary tree, print its nodes in inorder*/
  20. static void inorder(Node node)
  21. {
  22. if (node == null)
  23. return;
  24.  
  25. /* first recur on left child */
  26. inorder(node.left);
  27.  
  28. /* then print the data of node */
  29. System.out.printf("%d ", node.data);
  30.  
  31. /* now recur on right child */
  32. inorder(node.right);
  33. }
  34.  
  35. /* Method to merge given two binary trees*/
  36. static Node MergeTrees(Node t1, Node t2)
  37. {
  38. if (t1 == null)
  39. return t2;
  40. if (t2 == null)
  41. return t1;
  42. t1.data += t2.data;
  43. t1.left = MergeTrees(t1.left, t2.left);
  44. t1.right = MergeTrees(t1.right, t2.right);
  45. return t1;
  46. }
217. Contains Duplicate
  1. class Solution {
  2. public boolean containsDuplicate(int[] nums) {
  3. //cc
  4. if (nums.length == 0 || nums == null) {
  5. return false;
  6. }
  7. //ini = sort
  8. Arrays.sort(nums);记得先排序
  9. //for
  10. for (int i = 1; i < nums.length; i++) {
  11. if (nums[i] == nums[i - 1]) {
  12. 直接return true;
  13. }
  14. }
  15. return false;
  16. }
  17. }
 
Excel Sheet Column Number
  1. class Solution {
  2. public int titleToNumber(String s) {
  3. int res = 0;
  4. for (int i = 0; i < s.length(); i++) {
            26进制
  5. res = res * 26 + (s.charAt(i) - 'A' + 1);
  6. }
  7. return res;
  8. }
  9. }

2的倍数,三种方法:

  1. Last Edit: October 22, 2018 8:24 PM
  2.  
  3. motorix
  4. motorix
  5. 260
  6. This question is not an difficult one, and there are many ways to solve it.
  7.  
  8. Method 1: Iterative
  9.  
  10. check if n can be divided by 2. If yes, divide n by 2 and check it repeatedly.
  11.  
  12. if (n == 0) return false;
  13. while (n%2 == 0) n/=2;
  14. return n == 1;
  15. Time complexity = O(log n)
  16.  
  17. Method 2: Recursive
  18.  
  19. return n > 0 && (n == 1 || (n%2 == 0 && isPowerOfTwo(n/2)));
  20. Time complexity = O(log n)
  21.  
  22. Method 3: Bit operation
  23.  
  24. If n is the power of two:
  25.  
  26. n = 2 ^ 0 = 1 = 0b0000...00000001, and (n - 1) = 0 = 0b0000...0000.
  27. n = 2 ^ 1 = 2 = 0b0000...00000010, and (n - 1) = 1 = 0b0000...0001.
  28. n = 2 ^ 2 = 4 = 0b0000...00000100, and (n - 1) = 3 = 0b0000...0011.
  29. n = 2 ^ 3 = 8 = 0b0000...00001000, and (n - 1) = 7 = 0b0000...0111.
  30. we have n & (n-1) == 0b0000...0000 == 0
  31.  
  32. Otherwise, n & (n-1) != 0.
  33.  
  34. For example, n =14 = 0b0000...1110, and (n - 1) = 13 = 0b0000...1101.
  35.  
  36. return n > 0 && ((n & (n-1)) == 0);
  37. Time complexity = O(1)

二进制中1的个数 往右移动获得数字

  1. public class Solution {
  2. // you need to treat n as an unsigned value
  3. public int hammingWeight(int n) {
  4. int ones = 0;
  5. while (n != 0) {
  6. ones += (n & 1);
  7. n = n >>> 1;
  8. }
  9. return ones;
  10. }
  11. }

3.变换符号
变换符号就是正数变成负数,负数变成正数。

如对于-11和11,可以通过下面的变换方法将-11变成11

1111 0101(二进制) –取反-> 0000 1010(二进制) –加1-> 0000 1011(二进制)

同样可以这样的将11变成-11

0000 1011(二进制) –取反-> 0000 0100(二进制) –加1-> 1111 0101(二进制)

因此变换符号只需要取反后加1即可

找两个字符串的区别:用异或就对了

  1. class Solution {
  2. public char findTheDifference(String s, String t) {
  3. char c = 0;
  4. for (int i = 0; i < s.length(); ++i) {
  5. c ^= s.charAt(i);
  6. }
  7. for (int i = 0; i < t.length(); ++i) {
  8. c ^= t.charAt(i);
  9. }
  10. return c;
  11. }
  12. }

连续自然数数组中缺失的数字 :异或2次

  1. //268. Missing Number
  2. //for loop, judge if nums[i + 1] != nums[i] + 1
  3. class Solution {
  4. public int missingNumber(int[] nums) { //xor
  5. int res = nums.length;
  6. System.out.println(" res = " + res);
  7.  
  8. for(int i=0; i<nums.length; i++){
  9. System.out.println(" i = " + i);
  10. System.out.println(" nums[i] = " + nums[i]);
  11.  
  12. res ^= i;
  13. System.out.println(" res ^= i = " + res);
  14. res ^= nums[i];
  15. System.out.println(" res ^= nums[i] = " + res);
  16. System.out.println(" ");
  17. }
  18. return res;
  19. }
  20. }
  21. //n 1
191. Number of 1 Bits 往右移动
  1. public class Solution {
  2. // you need to treat n as an unsigned value
  3. public int hammingWeight(int n) {
  4. int ones = 0;
  5. while (n != 0) {
           计算每一位数与1的结果
  6. ones += (n & 1);
  7. n = n >>> 1;
  8. }
  9. return ones;
  10. }
  11. }
104. Maximum Depth of Binary Tree 最基本的DC了,DC就记住这个就行
  1. public class Solution {
  2. /**
  3. * @param root: The root of binary tree.
  4. * @return: An integer.
  5. */
  6. public int maxDepth(TreeNode root) {
  7. if (root == null) {
  8. return 0;
  9. }
  10.  
  11. int left = maxDepth(root.left);
  12. int right = maxDepth(root.right);
  13.  
  14. int max = Math.max(left,right);
  15. return max + 1;
  16. }
  17. }
108. Convert Sorted Array to Binary Search Tree
  1. class Solution {
  2. public TreeNode sortedArrayToBST(int[] nums) {
  3. //TreeNode node = new TreeNode(0);
  4. //corner case
  5. if (nums.length == 0) {
  6. return null;
  7. }
  8. TreeNode node = helper(nums, 0, nums.length - 1);// -1 should be noticed ahead
  9. return node;
  10. }
  11. 必须要有三个参数,不同的函数才能被称作helper
  12. public TreeNode helper(int[] nums, int low, int high) {
  13. //corner case : low > high
  14. if (low > high) {
  15. return null;
  16. }
  17. int mid = low + (high - low) / 2;
  18. TreeNode root = new TreeNode(nums[mid]);
  19. root.left = helper(nums, low, mid - 1);
  20. root.right = helper(nums, mid + 1, high);
  21. 要用到函数里给的参数
  22. return root;
  23. }
  24. }

118. Pascal's Trianglclass Solution

public List<List<Integer>> generate(int numRows)

  1. //ini
  2. List<List<Integer>> triangle = new ArrayList<List<Integer>>();
  3. //cc
  4. if (numRows <= 0) return triangle;
  5. //for
  6. for (int i = 0; i < numRows; i++) {
  7. List<Integer> row = new ArrayList<Integer>();
  8. for (int j = 0; j < i + 1; j++) {
              //首先写corner case
  9. if (j == 0 || j == i) {
  10. row.add(1);
  11. }else {
  12. row.add(triangle.get(i - 1).get(j - 1) + triangle.get(i - 1).get(j));
  13. }
  14. }
  15. //add again
            
    add方法
  16. triangle.add(new ArrayList(row));
  17. }
  18. //return
  19. return triangle;
  20. }
  21. }

plusOne 数组加一,从最后开始进位

  1. public class Solution {
  2. /**
  3. * @param digits: a number represented as an array of digits
  4. * @return: the result
  5. */
  6. public int[] plusOne(int[] digits) {
  7. //not carry
  8. int n = digits.length;
  9. //0 to n-1 whenever
  10. for (int i = n - 1; i >= 0; i--) {
  11. if (digits[i] < 9) {
  12. digits[i]++;
  13. return digits;直接在这里return即可
  14. }else {
  15. digits[i] = 0;
  16. }
  17. }
  18. //carry, need new array
  19. int[] answer = new int[n + 1];
  20. answer[0] = 1;
  21. return answer;
  22. }
  23. }

deleteNode

  1. public class Solution {
  2. /*
  3. * @param node: the node in the list should be deletedt
  4. * @return: nothing
  5. */
  6. public void deleteNode(ListNode node) {
  7. // write your code here
  8. if (node == null || node.next == null) {
  9. return ;
  10. }
    注意删除的是next而不是当前的node
  11. ListNode next = node.next;
  12. node.val = next.val;
  13. node.next = next.next;
  14. }
  15. }

190. Reverse Bits

  1. public class Solution {
  2. // you need treat n as an unsigned value
  3. public int reverseBits(int n) {
  4. //ini res
  5. int res = 0;
  6.  
  7. //for loop: 32 32位数,做移动32次
  8. for (int i = 0; i < 32; i++) {
  9. res <<= 1; 左移留出最后一位为0来相加
  10. if ((n & 1) == 1) res += 1;
  11. n >>= 1; 右移留出最后一位来操作
  12. }
  13.  
  14. return res;
  15. }
  16. }

101. Symmetric Tree

  1. class Solution {
  2. public boolean isSymmetric(TreeNode root) {
  3. //corner case
  4. if (root == null) {
  5. return true;
  6. }
  7. return isSymmetricHelper(root.left, root.right);
  8. }
  9.  
  10. public boolean isSymmetricHelper(TreeNode left, TreeNode right) {
  11. //all null
  12. if (left == null && right == null) {
  13. return true;
  14. }
  15. //one null
  16. if (left == null || right == null) {
  17. return false;
  18. }
  19. //not same
  20. if (left.val != right.val) {
  21. return false;
  22. }
  23. //same
  24. return isSymmetricHelper(left.left, right.right) && isSymmetricHelper(left.right, right.left);
  25. //don't forget the function name
  26. }
  27. }

234. Palindrome Linked List 两边开始

  1. public class Solution {
  2. /**
  3. * @param head: A ListNode.
  4. * @return: A boolean.
  5. */
  6. public boolean isPalindrome(ListNode head) {
  7. // corner case
  8. if (head == null) {
  9. return true;//true
  10. }
  11. //p1 go with p2
  12. ListNode middle = findMiddle(head);
  13. middle.next = reverse(middle.next);//
  14.  
  15. ListNode p1 = head, p2 = middle.next;
  16. while (p2 != null && p1.val == p2.val) {//order
  17. p1 = p1.next;
  18. p2 = p2.next;
  19. }
  20. return p2 == null;
  21. }
  22.  
  23. private ListNode findMiddle(ListNode head) {
  24. // corner case
  25. if (head == null) {
  26. return null;
  27. }无处不在的corner case
  28. ListNode fast = head.next, slow = head; 先走一步
  29. while (fast != null && fast.next != null) {
  30. slow = slow.next;
  31. fast = fast.next.next;
  32. }
  33. return slow;
  34. }
  35.  
  36. private ListNode reverse(ListNode head) {
  37. // corner case
  38. if (head == null) {
  39. return null;
  40. }
  41. ListNode prev = null;
  42. while (head != null) {
  43. ListNode temp = head.next;
  44. head.next = prev;
  45. prev = head;
  46. head = temp;
  47. }
  48. return prev;
  49. }
  50. }

136. Single Number 出现一次 用异或

  1. class Solution {
  2. public int singleNumber(int[] nums) {
  3. //ini n
  4. int n = 0;
  5.  
  6. //for loop ^=
  7. for (int i = 0; i < nums.length; i++) {
  8. n ^= nums[i]; 不同2次会返回原来的值
  9. }
  10.  
  11. //return
  12. if (n != 0) return n;
  13.  
  14. return 0;
  15. }
  16. }

169. Majority Element(n/2 times)

  1. class Solution {
  2. public int majorityElement(int[] nums) {
  3. //ini
  4. int major = nums[0];
  5. int count = 1;
  6. //1 - n
  7. for (int i = 1; i < nums.length; i++) {
  8. //nums[i] != major, count--,if count == 0
  9. if (nums[i] != major) {
  10. if (count == 0) {
  11. major = nums[i];
  12. count++;
  13. }
  14. count--;抵消当前的
  15. }else {
  16. //nums[i] == major, count++
  17. count++;
  18. }
  19. }
  20. return major;
  21. }
  22. }

160. Intersection of Two Linked Lists 想想图,对着写就行了

  1. A: a1 a2

  2. c1 c2 c3

  3. B: b1 b2 b3

 

  1. public class Solution {
  2. /*
  3. * @param headA: the first list
  4. * @param headB: the second list
  5. * @return: a ListNode
  6. */
  7. public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
  8. //corner case
  9. if (headA == null || headB == null) {
  10. return null;
  11. }
  12. //keep the same length
  13. int A_len = getLength(headA);
  14. int B_len = getLength(headB);
  15.  
  16. while (A_len > B_len) {
  17. headA = headA.next;
  18. A_len--;
  19. }
  20. while (A_len < B_len) {
  21. headB = headB.next;
  22. B_len--;
  23. }
  24. //find the same node
  25. while (headA != headB) {
  26. headA = headA.next;
  27. headB = headB.next;
  28. }
  29.  
  30. return headA;
  31. }
  32.  
  33. //getLength
  34. public int getLength(ListNode node) {
  35. int length = 0;
  36. while(node != null) {
  37. length++;
  38. node = node.next;
  39. }
  40. return length;
  41. }
  42. }

26. Remove Duplicates from Sorted Array

  1. class Solution {
  2. public int removeDuplicates(int[] nums) {
  3. if (nums == null || nums.length == 0) {
  4. return 0;
  5. }
  6. int size = 0;
  7. for (int i = 0; i < nums.length; i++) {
  8. if (nums[i] != nums[size]) { 总是去和size代表的最后一位相比较
  9. nums[++size] = nums[i];
  10. }
  11. }
  12. return size + 1;剩一个0
  13. }
  14. }

387. First Unique Character in a String

  1. public class Solution {
  2. /**
  3. * @param s: a string
  4. * @return: it's index
  5. */
  6. public int firstUniqChar(String s) {
  7. //corner case
  8. if (s == null) {
  9. return 0;
  10. }
  11. //put into cnt[]
  12. char[] c = s.toCharArray();字母是一个单独的数组
  13. int[] cnt = new int[256];表示所有字符
  14. for (int i = 0; i < s.length(); i++) {
  15. cnt[c[i]]++;
  16. }
  17. //return
  18. for (int i = 0; i < s.length(); i++) {
  19. if (cnt[c[i]] == 1) {
  20. return i;
  21. //break;
  22. }
  23. }
  24. return -1;
  25. }
  26. }

14. Longest Common Prefix

  1. Input: ["flower","flow","flight"]
  2. Output: "fl"
  1. public class Solution {
  2. /**
  3. * @param strs: A list of strings
  4. * @return: The longest common prefix
  5. */
  6. public String longestCommonPrefix(String[] strs) {
  7. //corner case
  8. if (strs == null) {
  9. return "";
  10. }
  11. if (strs.length == 0) {
  12. return "";
  13. }
  14. //define pre
  15. String pre = strs[0];
  16. int n = strs.length;
  17. //shorten pre
  18. for (int i = 1; i < n; i++) {
  19. while (strs[i].indexOf(pre) != 0) {
  20. pre = pre.substring(0, pre.length() - 1);
  21. }
  22. }
  23. //return
  24. return pre;
  25. }
  26. }

283. Move Zeroes

  1. class Solution {
  2. public void moveZeroes(int[] nums) {
  3. //cc
  4. if (nums == null || nums.length == 0) return;
  5. //ini
  6. int insertPos = 0;
  7.  
  8. for (int num : nums) {
  9. //no 0s
  10. if (num != 0) {
  11. nums[insertPos++] = num;
  12. }
  13. }
  14. //0s 分开就行
  15. while (insertPos < nums.length) {
  16. nums[insertPos++] = 0;
  17. }
  18. //return
  19. }
  20. }

//88. Merge Sorted Array
//add the bigger number from backward 从后往前

  1. public class Solution {
  2. /*
  3. * @param A: sorted integer array A which has m elements, but size of A is m+n
  4. * @param m: An integer
  5. * @param B: sorted integer array B which has n elements
  6. * @param n: An integer
  7. * @return: nothing
  8. */
  9. public void merge(int[] A, int m, int[] B, int n) {
  10. // write your code here
  11. int index = m + n - 1;
  12. int i = m - 1;
  13. int j = n - 1;
  14. //add the bigger number from backward
  15. while (i >= 0 && j >= 0) {最重要的是这一步
  16. if (A[i] > B [j]) {
  17. A[index--] = A[i--];
  18. }
  19. else {
  20. A[index--] = B[j--];
  21. }
  22. }
  23. //if there's number remaning in just A or B, append to the result
  24. while (i >= 0) {
  25. A[index--] = A[i--];
  26. }
  27. while (j >= 0) {
  28. A[index--] = B[j--];
  29. }
  30. }
  31. }

Merge Two Sorted Lists 从前往后

  1. public class Solution {
  2. /*
  3. * @param l1: ListNode l1 is the head of the linked list
  4. * @param l2: ListNode l2 is the head of the linked list
  5. * @return: ListNode head of linked list
  6. */
  7. public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
  8. // write your code here
  9. ListNode dummy = new ListNode(0);
  10. ListNode tail = dummy;得用dummy node
  11. while(l1 != null && l2 != null) {
  12. if (l1.val < l2.val) {
  13. tail.next = l1;
  14. l1 = l1.next;
  15. }
  16. else {
  17. tail.next = l2;
  18. l2 = l2.next;
  19. }
  20. tail = tail.next;一样的,注意要有主变量
  21. }
  22. if (l1 != null) {
  23. tail.next = l1;
  24. }
  25. if (l2 != null) {
  26. tail.next = l2;
  27. }
  28. return dummy.next;
  29. }
  30. }

121. Best Time to Buy and Sell Stock

  1. //121. Best Time to Buy and Sell Stock
  2. //use greedy to solve
  3. class Solution {
  4. public int maxProfit(int[] prices) {
  5. if (prices.length == 0 || prices == null) {
  6. return 0;
  7. }
  8.  
  9. int min = Integer.MAX_VALUE;
  10. int profit = 0;
  11. //update the min price and the max profit each time
  12. for (int i = 0; i < prices.length; i++) {
          两个三元运算符
  13. min = min < prices[i] ? min : prices[i];
  14. profit没有i = (prices[i] - min) > profit ? prices[i] - min : profit;
  15. }
  16.  
  17. return profit;
  18. }
  19. }
  20. //n 1

Reverse Integer: 我不熟悉的对每一位数进行的操作,步骤都是:取数、迭代、改数

  1. public int reverse(int x) {
  2. //ini
  3. int res = 0, newRes = 0;
  4. while (x != 0) {
  5. int tail = x % 10;先余后除,中间是newres 先背吧
  6. newRes = res * 10 + tail;
  7. if ((newRes - tail) / 10 != res) {
  8. return 0;
  9. }
  10. res = newRes;
  11. x = x / 10;
  12. }
  13. //return
  14. return newRes;
  15. }

Maximum Subarray

  1. class Solution {
  2. public int maxSubArray(int[] nums) {
  3. if (nums.length == 0 || nums == null) {
  4. return -1;
  5. }
  6.  
  7. int sum = 0;
  8. int minSum = 0;
  9. int max = Integer.MIN_VALUE;
  10. for (int i = 0; i < nums.length; i++) {
  11. sum += nums[i];
  12. max = Math.max(max, sum - minSum);先减之前的
  13. minSum = Math.min(minSum, sum);
  14. }
  15.  
  16. return max;
  17. }
  18. }

Two Sum

  1. class Solution {
  2. public int[] twoSum(int[] nums, int target) {
  3. int[] result = new int[2];
  4. HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
  5.  
  6. for (int i = 0; i < nums.length; i++) {
  7. if (map.containsKey(target - nums[i])) {
  8. result[0] = map.get(target - nums[i]);模板index
  9. result[1] = i;
  10.  
  11. return result;
  12. }
  13. map.put(nums[i],i);
  14. }
  15. return result;
  16. }
  17. }

Linked List Cycle

  1. public class Solution {
  2. /*
  3. * @param head: The first node of linked list.
  4. * @return: True if it has a cycle, or false
  5. */
  6. public boolean hasCycle(ListNode head) {
  7. // write your code here
  8. if (head == null) {
  9. return false;
  10. }
  11. ListNode fast = head.next;
  12. ListNode slow = head;
  13. while(fast != slow) {
  14. if (fast == null || fast.next == null) {
  15. return false;
  16. }
  17. fast = fast.next.next;
  18. slow = slow.next;
  19. }
  20.  
  21. return true;
  22. }
  23. }

Add Two Numbers 从前往后加,carry在最后面

  1. public class Solution {
  2. /*
  3. * @param l1: the first list
  4. * @param l2: the second list
  5. * @return: the sum list of l1 and l2
  6. */
  7. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  8. //intialize
  9. ListNode dummy = new ListNode(0);
  10. ListNode tail = dummy;//
  11. //sum
  12. int carry = 0;
  13. for (ListNode i = l1, j = l2; i != null || j != null;) {
  14. int sum = carry;
  15. sum += (i != null)? i.val: 0;注意一下是否为空再加
  16. sum += (j != null)? j.val: 0;
  17. // %
  18. tail.next = new ListNode(sum % 10);
  19. tail = tail.next;
  20. // /
  21. carry = sum / 10;
  22. i = (i == null)? i : i.next;//i, j should go down
  23. j = (j == null)? j : j.next;//judge itself
  24. }
  25. //carry 用于最后结尾收尸
  26. if (carry != 0) {
  27. tail.next = new ListNode(carry);
  28. }//turn carry into a node
  29.  
  30. return dummy.next;//forget
  31. }
  32. }

万年不会的翻转链表

  1. class Solution {
  2. public ListNode reverseList(ListNode head) {
  3.  
  4. ListNode prev = null;
  5. ListNode curt = head;
  6.  
  7. while(curt != null) {
  8. ListNode temp = curt.next;
  9. curt.next = prev;
  10. prev = curt;
  11. curt = temp;
  12. }
  13. return prev;
  14. }
  15. }

1. 找两个array不同的element: 就用双重for循环,没有啥高级的办法,就这样!

  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3.  
  4. int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6 };
  5. int[] arr2 = new int[] { 5, 6, 7, 8 };
  6.  
  7. boolean contains = false;
  8. List<Integer> list = new ArrayList<Integer>();
  9. for (int i = 0; i < arr1.length; i++) {
  10. for (int j = 0; j < arr2.length; j++) {
  11. if (arr1[i] == arr2[j]) {
  12. contains = true;
  13. break;
  14. }
  15. }
  16.  
  17. if(!contains){
  18. list.add(arr1[i]);
  19. }
  20. else{
  21. contains = false;
  22. }
  23. }
  24. System.out.println(list);
  25.  
  26. }

2. 怎么实现hashmap,big O是多少

key- index - bucket[index] array

  1. private int getBucketIndex(K key)
  2. {
  3. int hashCode = key.hashCode();
  4. int index = hashCode % numBuckets;
  5. return index;
  6. }

好像不一样,不用看了

  1. public class Book implements Comparable<Book>{
  2. String title, author; int year;
  3. Book(String title, String author, int year) {
  4. //the usual stuff
  5. }
  6. @Override
  7. public int compareTo(Book b) {
  8. return (this.title.compareTo(b.title));
  9. }
  10. @Override
  11. public int hashCode() {
  12. return Objects.hash(title, author, year);
  13. }
  14. @Override
  15. public boolean equals(Object o) {
  16. if (o == null) return false;
  17. if (this == o) return true;
  18. if (getClass() != o.getClass()) return false;
  19. Book b = (Book) o;
  20. return title.equals(b.title)
  21. && author.equals(b.author)
  22. && (year == b.year);
  23. }}

3. friend circle

  1. class Solution {
  2. public int findCircleNum(int[][] M) {
  3. //corner case
  4. if (M == null || M.length == 0) return 0;
  5.  
  6. //initialization: count = n, each id = id
  7. int m = M.length;
  8. int count = m;
  9. int[] roots = new int[m];
  10. for (int i = 0; i < m; i++) roots[i] = i;
  11.  
  12. //for loop and union find
  13. for (int i = 0; i < m; i++) {
  14. for (int j = i + 1; j < m; j++) {
  15. //if there is an edge, do union find
  16. if (M[i][j] == 1) {
    //注意下是怎么来的
  17. int root0 = find (roots, i);
  18. int root1 = find (roots, j);
  19.  
  20. if (root0 != root1) {
  21. roots[root1] = root0;
  22. count--;
  23. }
  24. }
  25. }
  26. }
  27.  
  28. //return count
  29. return count;
  30. }
  31.  
  32. public int find (int[] roots, int id) {
  33. while (id != roots[id]) {
  34. id = roots[roots[id]];
  35. }
  36. return id;
  37. }
  38. }

4. 找array倒数第k个element;找array连续element前面的element

只让我写了一个sql和一个merge sort...我觉得那不算算法题

mergesort: 新建两个数组,recursively merge之后再拼起来

  1. //recursive and append
  2. public static void mergeSort(int[] a, int n) {
  3. if (n < 2) {
  4. return;
  5. }
  6. int mid = n / 2;
  7. int[] l = new int[mid];
    //不一定是双倍,要用n-mid
  8. int[] r = new int[n - mid];
  9.  
  10. for (int i = 0; i < mid; i++) {
  11. l[i] = a[i];
  12. }
  13. for (int i = mid; i < n; i++) {
  14. r[i - mid] = a[i];
    //for loop里就不用++了
  15. }
    //sort - merge
  16. mergeSort(l, mid);
  17. mergeSort(r, n - mid);
  18.  
  19. merge(a, l, r, mid, n - mid);
  20. }
  21.  
  22. public static void merge(
  23. int[] a, int[] l, int[] r, int left, int right) {
  24. //list 5 variables here
  25. int i = 0, j = 0, k = 0;
  26. while (i < left && j < right) {
  27. if (l[i] < r[j]) {
  28. a[k++] = l[i++];
  29. }
  30. else {
  31. a[k++] = r[j++];
  32. }
  33. }
    //should be outside
  34. while (i < left) {
  35. a[k++] = l[i++];
  36. }
  37. while (j < right) {
  38. a[k++] = r[j++];
  39. }
  40. }
  1. quicksort:移动index, index还是原来的i < j就得交换后继续
    package com.java2novice.sorting;
  2.  
  3. public class MyQuickSort {
  4.  
  5. private int array[];
  6. private int length;
  7.  
  8. public void sort(int[] inputArr) {
  9.  
  10. if (inputArr == null || inputArr.length == 0) {
  11. return;
  12. }
  13. this.array = inputArr;
  14. length = inputArr.length;
  15. quickSort(0, length - 1);
  16. }
  17.  
  18. private void quickSort(int lowerIndex, int higherIndex) {
  19.  
  20. int i = lowerIndex;
  21. int j = higherIndex;
  22. // calculate pivot number, I am taking pivot as middle index number
  23. int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
    pivot应该是数组中的一个元素
  24. // Divide into two arrays
    //写完i j的关系之后要写整个数组的recursive
  25. while (i <= j) {
  26. /**
  27. * In each iteration, we will identify a number from left side which
  28. * is greater then the pivot value, and also we will identify a number
  29. * from right side which is less then the pivot value. Once the search
  30. * is done, then we exchange both numbers.
  31. */
  32. while (array[i] < pivot) {
  33. i++;
  34. }
  35. while (array[j] > pivot) {
  36. j--;
  37. }
    //array[i] is bigger than pivot, so exchange and continue
  38. if (i <= j) {
  39. exchangeNumbers(i, j);
  40. //move index to next position on both sides
  41. i++;
  42. j--;
  43. }
  44. }
  45. // call quickSort() method recursively
  46. if (lowerIndex < j)
  47. quickSort(lowerIndex, j);
  48. if (i < higherIndex)
  49. quickSort(i, higherIndex);
  50. }
  51.  
  52. private void exchangeNumbers(int i, int j) {
  53. int temp = array[i];
  54. array[i] = array[j];
  55. array[j] = temp;
  56. }
  57.  
  58. public static void main(String a[]){
  59.  
  60. MyQuickSort sorter = new MyQuickSort();
  61. int[] input = {24,2,45,20,56,75,2,56,99,53,12};
  62. sorter.sort(input);
  63. for(int i:input){
  64. System.out.print(i);
  65. System.out.print(" ");
  66. }
  67. }
  68. }

说给100首歌,设计一个shuffle/repeat播放音乐的逻辑.本文原创自1point3acres论坛
没有写代码,就说了说思路,也并不在乎你用啥语言,我用的python问能不能用buildin func 或者numpy都允许了hhh.

就用numpy random一个整数范围是0~n然后每次放了这个歌以后直接把这条数据移到最后然后n-1就行,最后n=1的时候播放最后一首歌,同时n再置最开始的那个数值就好。感觉这题是现场出的,说是考数据结构我一开始想用map,还迂回了很久= =。希望不要觉得我笨

【valid parenthesis】
class Solution {
public boolean isValid(String s) {
//corner case
if (s == null) {
return true;
}
Stack<Character> stack = new Stack<Character>();
//push into stack
for (char c : s.toCharArray()) {
//left cases
if (c == '(') {
stack.push(')');
}else if (c == '[') {

//每一种都要写
stack.push(']');
}else if (c == '{') {
stack.push('}');
}else if (stack.isEmpty() || stack.pop() != c) {
//right case
return false;
}
}
return stack.isEmpty();
}
}

【invert binary tree】
class TreeNode {
int val; TreeNode left, right; //parameter is another item
TreeNode(int item) { val = item; left = right = null; }}
class Solution {
public TreeNode invertTree(TreeNode root) {

//careful with corner case
if (root == null) {
return null;
}
TreeNode temp = root.left;
root.left = invertTree(root.right);
root.right = invertTree(temp);
return root;
}
}

【给两个String,找相同的字符】就是双重for循环
public static void main(String[] args) {

String str1 = "刘烨,孙坚,王二小,蜘蛛侠,钢铁侠,毛剑卿";
String str2 = "王二小,李占军,刘胡兰,毛剑卿";

String[] arr1 = str1.split(",") ;
String[] arr2 = str2.split(",") ;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr2.length; i++){
for (int j = 0; j < arr1.length; j++){
if (arr1[j].equals(arr2[i])){
sb.append(arr1[j] + ",") ;
}
}
}
System.out.println("结果:" + sb.toString().substring(0, sb.toString().length() - 1));

}
【reverse vowels】典型双指针了
class Solution {
public String reverseVowels(String s) {
//cc
if (s == null) {
return s;
}
//ini
String vowels = "aeiouAEIOU";
char[] chars = s.toCharArray();
int start = 0, end = s.length() - 1;

while (start < end) {
//adjust start, end
while (start < end && !vowels.contains(chars[start] + "")) {
start++;
}
while (start < end && !vowels.contains(chars[end] + "")) {
end--;
}
//exchange
char temp = chars[start];
chars[start] = chars[end];
chars[end] = temp;
//push to move on
start++;
end--;
}

//return
return new String(chars);
}
}

【missing number】短路了吧?
class Solution {
public int missingNumber(int[] nums) {

//ini = sort
Arrays.sort(nums);

//cc
if (nums[0] != 0) {
return 0;
}

//for
for (int i = 0; i < nums.length - 1; i++) {
//notice
if (nums[i + 1] != nums[i] + 1) {
return nums[i] + 1;
}
}

//return
return nums.length;
}
}

【reverse string】
class Solution {
public String reverseString(String s) {
//corner case
if (s == null) {
return null;
}
int i = 0, j = s.length() - 1;
//convert to char[]
char[] chars = s.toCharArray();
while (i < j) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
//notice here
i++;
j--;
}
//convert again
return new String(chars);
//return
}
}

【方法2】
public class Solution {
public String reverseString(String s) {
return new StringBuilder(s).reverse().toString();
}
}

【Valid Palindrome】
public class Solution {
/**
* @param s: A string
* @return: Whether the string is a valid palindrome
*/
public boolean isPalindrome(String s) {
//cc
if (s.isEmpty()) {
return true;
}

//define
int head = 0, tail = s.length() - 1;

while (head < tail) {
char cHead = s.charAt(head), cTail = s.charAt(tail);
//judge:punction or not
if (!Character.isLetterOrDigit(cHead)) {
head++;
}else if (!Character.isLetterOrDigit(cTail)) {
tail--;
}else {
//tolowercase and judge
if (Character.toLowerCase(cHead) != Character.toLowerCase(cTail)) {
return false;
}
//continue
head++;
tail--;
}
}

//return
return true;
}

【二分法解根号2】
class Solution {
public int mySqrt(int x) {
//bs
long start = 1, end = x;
while (start + 1 < end) {
long mid = start + (end - start) / 2;
if (mid * mid <= x) {
start = mid;
}else {
end = mid;
}
}

//return end or start
if (end * end <= x) {
return (int)end;
}
return (int)start;
}
}

【斐波那契数列 recursion】
//Fibonacci Series using Recursion
class fibonacci
{
static int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}

public static void main (String args[])
{
int n = 9;
System.out.println(fib(n));
}
}
Time Complexity: T(n) = T(n-1) + T(n-2) which is exponential.

【Use Dynamic Programming】
// Fibonacci Series using Dynamic Programming
class fibonacci
{
static int fib(int n)
{
/* Declare an array to store Fibonacci numbers. */
int f[] = new int[n+2]; // 1 extra to handle case, n = 0
int i;

/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;

for (i = 2; i <= n; i++)
{
/* Add the previous 2 numbers in the series
and store it */
f[i] = f[i-1] + f[i-2];
}

return f[n];
}

public static void main (String args[])
{
int n = 9;
System.out.println(fib(n));
}
}
//n n

【井字棋】
public class TicTacToe {
private int[] rows;
private int[] cols;
private int diagonal;
private int antiDiagonal;

/** Initialize your data structure here. */
public TicTacToe(int n) {
rows = new int[n];
cols = new int[n];
}

/** Player {player} makes a move at ({row}, {col}).
@param row The row of the board.
@param col The column of the board.
@param player The player, can be either 1 or 2.
@return The current winning condition, can be either:
0: No one wins.
1: Player 1 wins.
2: Player 2 wins. */
public int move(int row, int col, int player) {
int toAdd = player == 1 ? 1 : -1;

rows[row] += toAdd;
cols[col] += toAdd;
if (row == col)
{
diagonal += toAdd;
}

if (col == (cols.length - row - 1))
{
antiDiagonal += toAdd;
}

int size = rows.length;
if (Math.abs(rows[row]) == size ||
Math.abs(cols[col]) == size ||
Math.abs(diagonal) == size ||
Math.abs(antiDiagonal) == size)
{
return player;
}

return 0;
}
}

【happy number】
class Solution {
public boolean isHappy(int n) {
//cc
if (n == 0) {
return false;
}

//ini
int squareSum, remain;
Set set = new HashSet();

//while loop, contains

//每个n 在set中只加一次,不重复
while (set.add(n)) {
squareSum = 0;
//每次加一个n,都重新算squareSum
while (n > 0) {
remain = n % 10;
squareSum += remain * remain;
n = n / 10;
}

if (squareSum == 1) return true;
n = squareSum;最后才把数字给n
}

return false;
}
}

【number permutation】
public class Solution {
/*
* @param nums: A list of integers.
* @return: A list of permutations.
*/
public List<List<Integer>> permute(int[] nums) {
//corner case
List<List<Integer>> results = new ArrayList<>();//
List<Integer> permutations = new ArrayList<Integer>();
HashSet<Integer> set = new HashSet<Integer>();

if (nums == null) {
return results;
}
if (nums.length == 0) {
results.add(new ArrayList<Integer>());
return results;
}
//helper
helper(nums, permutations, set, results);
return results;
}

//helper :NSTRS
public void helper(int[] nums, int start, List<Integer> temp, List<List<Integer>> results,HashSet<Integer> set用于检验重复) {
if (permutations.size() == nums.length) {//exit
results.add(new ArrayList<Integer>(permutations));
return ; 记得此处要返回,有return
}

for (int i = 0; i < nums.length; i++) {

//continue when there is duplicate 注意重复的情况,这也是一种corner case
if (set.contains(nums[i])) {
continue;
}

temp.add(nums[i]);
set.add(nums[i]);
helper(nums, i+1, temp, results, set);
set.remove(nums[i]);
temp.remove(temp.size() - 1);
}
}

位运算

  1. public ArrayList<String> permute(char[] s, ArrayList<String> list, int index) {
  2.  
  3. list.add(String.valueOf(s));
  4. if (index >= s.length)
  5. return list;
  6.  
  7. for (int i = index; i < s.length; i++) {
  8.  
  9. if (Character.isAlphabetic(s[i])) {
  10.  
  11. s[i] ^= 32;
  12. permute(s, list, i + 1);
  13. s[i] ^= 32;
  14. }
  15. }
  16. return list;
  17.  
  18. }

subset:

private void backtrack(int [] nums, int start,List<Integer> tempList, List<List<Integer>> list){
list.add(new ArrayList<>(tempList));
for(int i = start; i < nums.length; i++){
tempList.add(nums[i]);
backtrack(list, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}

【search in 2 2D array】
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {

if (matrix == null || matrix.length == 0) {
return false;
}
if (matrix[0] == null || matrix[0].length == 0) {
return false;
}

int m = matrix.length;
int n = matrix[0].length;
int x = m - 1;
int y = 0;
int count = 0;

while (x >= 0 && y < n) {
if (matrix[x][y] > target) {

大动
x--;
}
else if(matrix[x][y] < target) {

小动

y++;
}
else {
count++;
x--;
y++;
}
}

if (count > 0) {
return true;
}
return false;
}
}

【group anagrams】
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
//cc
if (strs == null || strs.length == 0) return new ArrayList<List<String>>();

//ini: map, char[]
Map<String, List<String>> map = new HashMap<>();

//for loop: add to char, to map
for (String str : strs) {
char[] chars = str.toCharArray();

//顺序统一以后都一样
Arrays.sort(chars);
String anagram = String.valueOf(chars);

//then add to map
if (!map.containsKey(anagram)) map.put(anagram, new ArrayList<String>());
map.get(anagram).add(str);
}

//return (map)
return new ArrayList<List<String>>(map.values());
}
}

【find second maximum】固定2个数
// JAVA Code for Find Second largest
// element in an array
class GFG {

/* Function to print the second largest
elements */
public static void print2largest(int arr[],
int arr_size)
{
int i, first, second;

/* There should be atleast two elements */
if (arr_size < 2)
{
System.out.print(" Invalid Input ");
return;
}

first = second = Integer.MIN_VALUE;
for (i = 0; i < arr_size ; i++)
{
/* If current element is smaller than
first then update both first and second */
if (arr[i] > first)
{
second = first;
first = arr[i];
}

/* If arr[i] is in between first and
second then update second */
else if (arr[i] > second && arr[i] != first)
second = arr[i];
}

if (second == Integer.MIN_VALUE)
System.out.print("There is no second largest"+
" element\n");
else
System.out.print("The second largest element"+
" is "+ second);
}

【Find All Duplicates in an Array】

class Solution {
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int size = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != nums[size]) {

//compare with the last number
nums[++size] = nums[i];

//注意是先加再给
}
}
return size + 1;
}
}

【difference】
比较基础 递归 迭代:本身、堆栈、基础条件
基本 函数体中的语句调用函数本身。 允许重复执行指令集。
格式 在递归函数中,仅指定终止条件(基本情况)。 迭代包括初始化,条件,循环内语句的执行以及控制变量的更新(递增和递减)。
终止 条件语句包含在函数体中,以强制函数返回而不执行递归调用。 重复执行迭代语句,直到达到某个条件。
条件 如果函数没有收敛到某个称为(基本情况)的条件,则会导致无限递归。 如果迭代语句中的控制条件永远不会变为false,则会导致无限次迭代。
无限重复 无限递归可能会导致系统崩溃。 无限循环重复使用CPU周期。
应用的 递归始终应用于函数。 迭代应用于迭代语句或“循环”。
堆 每次调用函数时,堆栈用于存储一组新的局部变量和参数。 不使用堆栈。
高架 递归拥有重复函数调用的开销。 没有重复函数调用的开销。
速度 执行缓慢。 快速执行。
代码大小 递归减少了代码的大小。 迭代使代码更长。

【】

高盛昂赛 算法题先写corner case的更多相关文章

  1. python算法题

    python几道简单的算法题   最近看了python的语法,但是总感觉不知道怎么使用它,还是先来敲敲一些简单的程序吧. 1.题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都 ...

  2. 2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告

    2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告 勘误1:第6题第4个 if最后一个条件粗心写错了,答案应为1580. 条件应为abs(a[3]-a[7])!=1,宝宝心理苦啊.!感谢zzh ...

  3. HDU-5532//2015ACM/ICPC亚洲区长春站-重现赛-F - Almost Sorted Array/,哈哈,水一把区域赛的题~~

    F - Almost Sorted Array Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  4. FCC上的初级算法题

    核心提示:FCC的算法题一共16道.跟之前简单到令人发指的基础题目相比,难度是上了一个台阶.主要涉及初步的字符串,数组等运算.仍然属于基础的基础,官方网站给出的建议完成时间为50小时,超出了之前所有非 ...

  5. 解决一道leetcode算法题的曲折过程及引发的思考

    写在前面 本题实际解题过程是 从 40秒 --> 24秒 -->1.5秒 --> 715ms --> 320ms --> 48ms --> 36ms --> ...

  6. 经典算法题每日演练——第十一题 Bitmap算法

    原文:经典算法题每日演练--第十一题 Bitmap算法 在所有具有性能优化的数据结构中,我想大家使用最多的就是hash表,是的,在具有定位查找上具有O(1)的常量时间,多么的简洁优美, 但是在特定的场 ...

  7. 经典算法题每日演练——第八题 AC自动机

    原文:经典算法题每日演练--第八题 AC自动机 上一篇我们说了单模式匹配算法KMP,现在我们有需求了,我要检查一篇文章中是否有某些敏感词,这其实就是多模式匹配的问题. 当然你也可以用KMP算法求出,那 ...

  8. 经典算法题每日演练——第七题 KMP算法

    原文:经典算法题每日演练--第七题 KMP算法 在大学的时候,应该在数据结构里面都看过kmp算法吧,不知道有多少老师对该算法是一笔带过的,至少我们以前是的, 确实kmp算法还是有点饶人的,如果说红黑树 ...

  9. C#有意思的算法题

    年底了,特贡献一些C#有意思的算法题   2013年,即将要过去了.屌丝C#程序员们拿到了年终奖不?是不是又想蠢蠢欲动了?是不是想通过跳槽来为自己实现加薪的梦想?好吧,跳槽之前还是做点准备吧,准备好C ...

随机推荐

  1. Leetcode 题解 Jump Game

    一,笨方法  o(n^2).果然看完别人的解法就自惭形秽啊!! 我用的动态规划方法. 比如输入 i: 0 1 2 3 4 ———————————————— a[i]: 2 3 1 0 4 直接利用原来 ...

  2. es6 初级之展开运算符

    1.1 先看一个求最大值的例子 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  3. javascript:控制一个元素高度始终等于浏览器高度

    window.onresize = function(){ this.opHtight()} //给浏览器添加窗口大小改变事件window.onresize = function(){ this.op ...

  4. JSFL 禁止脚本运行时间太长的警告

    fl.showIdleMessage(false);

  5. avalon2学习教程05属性操作

    avalon2与avalon1的属性操作虽然都是使用ms-attr,但用法完全不一样. avalon1是这样操作属性的 其语法为 ms-attr-valueName="vmProp" ...

  6. python实现根据文件关键字进行切分为多个文件

    来源:在工作过程中,需要统计一些trace信息,也就是一些打点信息,而打点是通过关键字进行的,因此对一个很大的文件进行分析时,想把两个打点之间的内容单独拷贝出来进行分析 #!/usr/bin/env ...

  7. Celery + RabbitMq 示意图

    一直搞不清楚消息队列和任务队列是如何结合的,直到碰到了 :http://www.cnblogs.com/jijizhazha/p/8086119.html 中的图,恍然大悟,凭借自己的理解,画了这幅组 ...

  8. 吴裕雄 python oracle子查询的用法(3)

    import cx_Oracle conn = cx_Oracle.connect("scott/admin@localhost:1521/orcl")cursor = conn. ...

  9. centos 卸载和安装软件

    rpm -qa 列出所有已安装软件包 rpm -e packagename  删除软件包 rpm -e --nodeps packagename  强制删除软件和依赖包 rpm -q 包名     查 ...

  10. 如何用java完成一个中文词频统计程序

    要想完成一个中文词频统计功能,首先必须使用一个中文分词器,这里使用的是中科院的.下载地址是http://ictclas.nlpir.org/downloads,由于本人电脑系统是win32位的,因此下 ...