Question 1
Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
We need to consider a lot of cases of input. One particular example is how to deal with many zeros in the end of that number.
The most direct way is use string or char array to store the int number and then convert it. But I prefer to use the pure math way to do it as cleaner and easier.
1 public class Solution { 2 public int reverse(int x) { 3 if (x>=0 && x < 10) 4 return x; 5 6 boolean flag = true; 7 8 if (x < 0){ 9 x = 0-x; 10 flag = false; 11 } 12 13 double result = 0; 14 15 while (x > 0){ 16 int mod = x % 10; 17 result = result * 10 + mod; 18 x = x / 10; 19 } 20 21 if (flag) 22 return result > Integer.MAX_VALUE ? 0 : (int)result; 23 else 24 return 0-result < Integer.MIN_VALUE ? 0 : (int)(0-result); 25 26 } 27 }
Question 2
Permutation Sequence
The set[1,2,3,…,n]
contains a total of n! unique permutations.By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
I still prefer to use pure math solution instead of recursive method. What confuses me a lot here is the k-- and index get. It takes me some time to figure the relationship among n, k and index. And use mod % is very clever.
public String getPermutation(int n, int k) { k--;//to transfer it as begin from 0 rather than 1 ArrayList<Integer> numList = new ArrayList<Integer>(); for(int i = 1; i<= n; i++) numList.add(i); int factorial = 1; for(int i = 2; i < n; i++) factorial *= i; String res = ""; n--; while(n>=0){ int indexInList = k/factorial; res += numList.get(indexInList); numList.remove(indexInList); k = k%factorial;//new k for next turn if(n!=0) factorial = factorial/n;//new (n-1)! n--; } return res; }
Question 3
Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
To determine whether a point is in a defined line is by the slope between this point and the defined point. So we need to iterate the point[] array for each element with i and j to get two points and their slope.Use a hashmap to store (slope, number of same line points) that will be more efficient than other way. But remember this hashmap should be re-initiate as a new hashmap everytime after you change to next i value in point[] array.
1 /** 2 * Definition for a point. 3 * class Point { 4 * int x; 5 * int y; 6 * Point() { x = 0; y = 0; } 7 * Point(int a, int b) { x = a; y = b; } 8 * } 9 */ 10 public class Solution { 11 public int maxPoints(Point[] points) { 12 if (points.length == 0) 13 return 0; 14 15 int max = 1; 16 for (int i = 0; i < points.length; i ++){ 17 int sameNum = 0; 18 int eachMax = 1; 19 HashMap<Float, Integer> map = new HashMap<Float, Integer>(); 20 for (int j = 0; j < points.length; j++ ){ 21 if (i==j) 22 continue; 23 else{ 24 if (points[i].x == points[j].x && points[i].y == points[j].y){ 25 sameNum ++; 26 continue; 27 } 28 else{ 29 float slope = (float) (points[i].y - points[j].y) / (points[i].x - points[j].x); 30 if (map.containsKey(slope)) 31 map.put(slope, map.get(slope) + 1); 32 else 33 map.put(slope, 2); 34 } 35 } 36 37 } 38 for (int value : map.values()){ 39 eachMax = Math.max(eachMax, value); 40 } 41 eachMax += sameNum; 42 max = Math.max(eachMax, max); 43 } 44 return max; 45 } 46 47 }