数学、计算几何、位运算常见问题详解
• 矩阵上的问题(3题)
Search a 2D Matrix II
public int searchMatrix(int[][] matrix, int target) { // write your code here int n = matrix.length; if (n == 0) { return 0; } int m = matrix[0].length; if (m == 0) { return 0; } int i = n - 1; int j = 0; int res = 0; while (i >= 0 && j < m) { if (matrix[i][j] == target) { res++; i--; j++; } else if (matrix[i][j] > target) { i--; } else { j++; } } return res; }
Rotate Image
public void rotate(int[][] matrix) { // write your code here int n = matrix.length; for (int i = 0; i < n / 2; i++) { for (int j = 0; j < n; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[n - 1 - i][j]; matrix[n - 1 - i][j] = temp; } } for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } }
Sparse Matrix Multiplication
public int[][] multiply(int[][] a, int[][] b) { int n = a.length; int m = b[0].length; int t = a[0].length; int[][] res = new int[n][m]; List<List<Integer>> col = new ArrayList<>(); List<List<Integer>> val = new ArrayList<>(); for (int i = 0; i < t; i++) { col.add(new ArrayList<>()); val.add(new ArrayList<>()); for (int j = 0; j < m; j++) { if (b[i][j] != 0) { col.get(i).add(j); val.get(i).add(b[i][j]); } } } for (int i = 0; i < n; i++) { for (int k = 0; k < t; k++) { if (a[i][k] == 0) { continue; } for (int j = 0; j < col.get(k).size(); j++) { int c = col.get(k).get(j); int v = col.get(k).get(j); res[i][j] += a[i][k] * v; } } } }
public int[][] multiply(int[][] a, int[][] b){ int m = a.length; int n = b[0].length; int t = a[0].length; List<List<Integer>> col = new ArrayList<>(); List<List<Integer>> val = new ArrayList<>(); for (int i = 0; i < t; i++) { col.add(new ArrayList<>()); val.add(new ArrayList<>()); for (int j = 0; j < n; j++) { if (b[i][j] != 0) { col.get(i).add(j); val.get(i).add(b[i][j]); } } } int[][] res = new int[m][n]; for (int i = 0; i < m; i++) { for (int k = 0; k < t; k++) { if (a[i][k] == 0) { continue; } for (int p = 0; p < col.get(k).size(); p++) { int j = col.get(k).get(p); int v = val.get(k).get(p); res[i][j] += a[i][k] * v; } } } return res; }
• 高精度运算(4题)
Big Integer Addition
public String addStrings(String num1, String num2) { // write your code here String res = ""; int carry = 0; for (int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0; i--, j--) { int sum = carry; sum += i >= 0 ? num1.charAt(i) - '0' : 0; sum += j >= 0 ? num2.charAt(j) - '0' : 0; res = sum % 10 + res; carry = sum / 10; } if (carry > 0) { res = carry + res; } return res; }
Big Integer multiplication
public String multiply(String num1, String num2) { // write your code here int l1 = num1.length(); int l2 = num2.length(); int[] res = new int[l1 + l2 + 1]; for (int i = 0; i < l1; i++) { for (int j = 0; j < l2; j++) { res[i + j] += (num1.charAt(l1 - 1 - i) - '0') * (num2.charAt(l2 - 1 - j) - '0'); } } for (int i = 0; i < l1 + l2; i++) { res[i + 1] += res[i] / 10; res[i] = res[i] % 10; } int i = l1 + l2; while (res[i] == 0 && i >= 1) { i--; } String str = ""; while (i >= 0) { str += res[i--]; } return str; }
Add Binary
public String addBinary(String a, String b) { String res = ""; int carry = 0; for (int i = a.length() - 1, j = b.length() - 1; i >= 0 || j >= 0; j--, i--) { int sum = carry; sum += i >= 0 ? (a.charAt(i) - '0') : 0; sum += j >= 0 ? (b.charAt(j) - '0') : 0; res = sum % 2 + res; carry = sum / 2; } if (carry > 0) { res = carry + res; } return res; }
Add Two Numbers
public ListNode addLists(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0); ListNode tail = dummy; int carry = 0; for (ListNode i = l1, j = l2; i != null || j != null;) { int sum = carry; sum += i != null ? i.val : 0; sum += j != null ? j.val : 0; tail.next = new ListNode(sum % 10); tail = tail.next; i = i == null ? i : i.next; j = j == null ? j : j.next; carry = sum / 10; } if (carry > 0) { tail.next = new ListNode(carry); } return dummy.next; }
• 快速幂(1题)
Pow(x, n)
public double myPow(double x, int n) { if (n < 0) { x = 1 / x; n = -n; } double res = 1, temp = x; while (n != 0) { if (n % 2 == 1) { res *= temp; } n /= 2; temp = temp * temp; } return res; }
public double myPow(double x, int m) { long n = m; if (n < 0) { x = 1 / x; n = -n; } double res = 1, temp = x; while (n != 0) { if (n % 2 == 1) { res *= temp; } n /= 2; temp = temp * temp; } return res; }