[关闭]
@xmruibi 2015-02-13T03:43:36.000000Z 字数 4637 阅读 748

Mathematical Method

Algorithm


This is the conclusion for those interview questions which contain mathematical method.

Part One: Geometry

1. CC 10.3: Determine whter the two lines would intersect on Cartesian plane.

  1. design a Line class:
  2. double epsilon = 0.0000001;
  3. double slope;
  4. double yIntercept;
  5. Line(double slope, double yIntercept):
  6. this.slope = slope;
  7. this.yIntercept = yIntercept;
  8. boolean intersect(Line line2):
  9. return Math.abs(this.slope-line2.slope) > epsilon
  10. ||Math.abs(this.yIntercept - line2.yIntercept) < epsilon;

2. CC 10.5: find a line that would cut these two squares in half

  1. class Point:
  2. double x;
  3. double y;
  4. public Point(double x, double y){
  5. this.x = x;
  6. this.y = y;
  7. }
  8. class Line:
  9. Point p1;
  10. Point p2;
  11. public Line(Point p1, Point p2){
  12. this.p1 = p1;
  13. this.p2 = p2;
  14. }
  15. class Square:
  16. double left;
  17. double right;
  18. double top;
  19. double bottom;
  20. public Square(double left, double top, double size){
  21. this.left = left;
  22. this.top = top;
  23. this.right = left+size;
  24. this.bottom = top - size;
  25. }
  26. public Point middle(){
  27. return new Point((this.left+this.right)
  28. >>1,(this.top+this.bottom)>>1);
  29. }
  30. public Line cut(Square other){
  31. Point middle_s = this.middle();
  32. Point middle_t = other.middle();
  33. if(middle_s == middle_t)
  34. return new Line(new Point(left,top),
  35. new Point(right,bottom));
  36. else
  37. return new Line(middle_s,middle_t);
  38. }
  39. }

3. CC 10.6, LEETCODE: Max Points on a Line

a line can be represented by two ways a). a pairing of points; b) a slope and a y-intersect
b): every line segment on the sam greater line will have identical slopes and y-intercept
O(n^2) Time complexity, O(n) Space Complexity

  1. class Line{
  2. double epsilon = 0.00000001;
  3. double slope;
  4. double intercept;
  5. private boolean INFINITE_SLOPE = false;
  6. public Line(Point p, Point q){
  7. if(Math.abs(p.x - q.x) > epsilon){
  8. slope = (p.y - q.y) / (p.x - q.x);
  9. intercept = p.y - slope*p.x;
  10. }else{
  11. INFINITE_SLOPE = true;
  12. intercept = p.x;
  13. }
  14. }
  15. public boolean isEqual(double a, double b){
  16. return (Math.abs(a-b)<epsilon)
  17. }
  18. @Override
  19. public int hashCode(){
  20. int sl = (int) slope*1000;
  21. int in = (int) intercept*1000;
  22. return sl|in;
  23. }
  24. @Override
  25. public boolean equals(Object o){
  26. Line l = (Line)o;
  27. if(isEqual(l.slope,this.slope)&&
  28. isEqual(l.intercept,this.intercept)&&
  29. (this.INFINITE_SLOPE == l.INFINITE_SLOPE))
  30. return true;
  31. return false;
  32. }
  33. }
  34. int findMaxPoint(Point[] points){
  35. int max = Integer.MIN_VALUE;
  36. for(int i = 0;i<points.length;i++){
  37. int localMax = Integer.MIN_VALUE;
  38. HashMap<Line,Integer> map = new HashMap<Line,Integer>();
  39. for(int j=i+1;j<points.length;j++){
  40. Line line = new Line(points[i],points[j]);
  41. if(!map.containsKey(line)){
  42. map.put(line,1);
  43. }else{
  44. map.put(line,map.get(line)+1);
  45. }
  46. for(int x:map.values())
  47. localMax = Math.max(localMax,x); max = Math.max(localMax,max);
  48. }
  49. }
  50. return max;
  51. }

Part Two: Algebra

4. CC 10.4: USE "+" to implement *,/,- operation

subtraction:a+ (-1)*b but whether it is negative? and how to avoid *(-1)?
  1. int minus(int a, int b):
  2. int newb = 0;
  3. int neg = 0;
  4. neg = b<0? 1:-1;
  5. while(b!=0):
  6. b+=neg; // b -> 0
  7. newb+=neg; // newb = b‘s opposite number
  8. return a+newb;
multiplication: a*b = a+...+a with b times
  1. int getOppo(int i)
  2. int newi = 0;
  3. int neg = 0;
  4. neg = i<0? 1:-1;
  5. while(i!=0):
  6. i+=neg; // i -> 0
  7. newi+=neg; // newi = i‘s opposite number
  8. return newi;
  9. int multiple(int a, int b):
  10. // whether a,b is negtive
  11. int neg = 1;
  12. neg = a>=0?1:-1;
  13. neg += b>=0?1:-1;
  14. if(a<0)
  15. a = getOppo(a); // keep a positive
  16. if(b>0)
  17. b = getOppo(b); // keep b negtive
  18. int sum = 0;
  19. while(b!=0):
  20. sum+=a;
  21. b+=1;
  22. if(neg==0)
  23. sum = getOppo(sum);
  24. return sum;
  25. division: a/b = a-b-...-b=0 with ? times
  26. int division(int a, int b)
  27. if(b==0)
  28. throw new Exception;
  29. int neg = 1;
  30. neg = a>=0?1:-1;
  31. neg += b>=0?1:-1;
  32. if(a<0)
  33. a = getOppo(a); // keep a positive
  34. if(b>0)
  35. b = getOppo(b); // keep b negtive
  36. int quotient = 0;
  37. while(a>1):
  38. a += b;
  39. quotient++;
  40. if(neg==0)
  41. quotient = getOppo(quotient);
  42. return quotient;

5. ** CC 10.7: find the kth number such that the only prime factors are 3, 5 and 7.

找到质因数为3、5、7的第k个数
    1. 初始化结果res=1和队列q3,q5,q7
    2. 分别往q3,q5,q7插入1*3,1*5,1*7
    3. 求出三个队列的队头元素中最小的那个x,更新结果res=x
    4. 如果x在:
        q3中,那么从q3中移除x,并向q3,q5,q7插入3*x,5*x,7*x
        q5中,那么从q5中移除x,并向q5,q7插入5*x,7*x
        q7中,那么从q7中移除x,并向q7插入7*x
    5. 重复步骤3-5,直到找到第k个满足条件的数
  1. int getKthMagicNumber(int k){
  2. if(k<=0)
  3. return 0;
  4. int val = 1;
  5. Queue<Integer> q3 = new LinkedList<Integer>();
  6. Queue<Integer> q5 = new LinkedList<Integer>();
  7. Queue<Integer> q7 = new LinkedList<Integer>();
  8. q3.add(3);
  9. q5.add(5);
  10. q7.add(7);
  11. k--;
  12. while(k>0){
  13. val = Math.min(q3.peek(),Math.min(q5.peek(),q7.peek()));
  14. if(val = q7.peek()){
  15. q7.remove();
  16. }else{
  17. if(val = q5.peek()){
  18. q5.remove();
  19. }else{
  20. q3.remove();
  21. q3.add(val*3);
  22. }
  23. q5.add(val*7);
  24. }
  25. q7.add(val*7);
  26. k--;
  27. }
  28. return val;
  29. }

6. GCD - Great Common Divisor

  1. Euclidean ALgorithm: - Recursion
  2. int gcd(int a, int b):
  3. if(b==0)
  4. return a;
  5. return (b, a%b);
  6. - Iteration
  7. int gcd(int a, int b):
  8. while(b>0):
  9. int tmp = a%b;
  10. a = b;
  11. b = tmp;
  12. return a;
  13. // based on subtraction:
  14. while(a!=b):
  15. if(a>b):
  16. a -= b;
  17. else
  18. b -= a;
  19. return a;
  20. LCM(Least Common Multiple) = a*b/gcd(a,b);

7. Extended Euclidean:

        ax + by = gcd(a, b) 
        因为 gcd(a,b)=gcd(b,a mod b)
        所以有 => ax1 + by1 = bx2 + (a-(a/b)*b)y2 = ay2 + bx2 - (a/b)*by2 
                = ay2 + bx2 - (a/b)*by2 
                = ay2 + b(x2 - (a/b)*y2)

        所以有 x1 = y2;        
                y1 = x2 - (a/b)*y2;
  1. int extend_Eulid(int a,int b){
  2. if (b==0){
  3. x=1; y=0; return a;
  4. }else{
  5. int r = extend_Eulid(b,a%b);
  6. int temp=x;
  7. x=y;
  8. y=temp-a/b*y;
  9. return r;
  10. }
  11. }

8. isPrime:

            n cannot be divided by any number in 1~n or 1~sqrt(n)

Notice:

  1. **两个浮点数不能直接用=来进行比较,而应该用差值是否小于一个特定的小数来确定是否相等!epsilon < a certain value **
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注