69. Sqrt(x) && 367. Valid Perfect Square && 50. Pow(x, n) && 372. Super Pow

69. Sqrt(x)

Implement  int sqrt(int x).

Compute and return the square root of x.

 
 Time Limit Exceed Solution if start from 0.
public class Solution {
    public int mySqrt(int x) {
        if(x == 0)
            return 0;
        int lowerBound = 1;
        int higherBound = 2;
    
        //search for a boundary
        while (true) {
          int square = lowerBound * lowerBound;
          if (square == x)
            return lowerBound;
          if (square < x) {
            lowerBound = higherBound;
            higherBound = higherBound * 2;
          } else {
            lowerBound = lowerBound / 2;
            higherBound = higherBound / 2;
            break;
          }
        }
    
        int left = lowerBound + 1;
        int right = higherBound - 1;
        while (left <= right) {
          int mid = (left + right) / 2;
          int square = mid * mid;
          if (square == x)
            return mid;
          if (square < x)
            left = mid + 1;
          else
            right = mid - 1;
        }
        return left - 1;
    }
}

Another solution if start from both ends of Integer range.

public class Solution {
    public int mySqrt(int x) {
        if (x == 0)
            return 0;
        int left = 1, right = Integer.MAX_VALUE;
        while (left <= right) {
            int mid = left + (right - left)/2;
            int temp = x/mid;//use division because mid*mid may > Integer.max
            if(mid == temp)
                return mid;
            if (mid > temp) 
                right = mid - 1;
            else
                left = mid + 1;
        }
        return left-1;
    }
}

 

367. Valid Perfect Square

Given a positive integer num, write a function which returns True if num is a perfect square else False.

Note: Do not use any built-in library function such as sqrt.

Example 1:

Input: 16
Returns: True

Example 2:

Input: 14
Returns: False  
 
 
public class Solution {
    public boolean isPerfectSquare(int num) {
        if (num == 0)
            return true;
        int left = 1, right = Integer.MAX_VALUE;
        while (left <= right) {
            int mid = left + (right - left)/2;
            int temp = num/mid;//use division because mid*mid may > Integer.max
            if(mid == temp)
            {
                if(mid*mid == num)
                    return true;
                return false;//Here is the trap! e.g. num = 5, mid = 2
            }
            if (mid > temp) 
                right = mid - 1;
            else
                left = mid + 1;
        }
        return false;
    }
}

 

50. Pow(x, n)

 
public class Solution {
    public double myPow(double x, int n) {
        if(n == 0)
            return 1;
        if(n>0) {
            if(n%2==0) {
                double result = myPow(x, n/2);
                return result*result;
            }
            else {
                double result = myPow(x, (n-1)/2);
                return x*result*result;
            }
        }
        else {
            if(n==Integer.MIN_VALUE)
                return 1.0/x*myPow(x,n+1);
            return 1.0/myPow(x,-1*n);
        }
    }
}

 

 

372. Super Pow

Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.

Example1:

a = 2
b = [3]
Result: 8

Example2:

a = 2
b = [1,0]
Result: 1024
  Math
 
Reference: https://discuss.leetcode.com/topic/50504/java-4ms-solution-using-the-remainder-repeat-pattern 

If a > 1337, we can let a = a mod 1337.
Because if we let a = (1337x + c) where c = a mod 1337,
(1337
x + c)(1337x + c)(1337x + c)...(1337x + c) mod 1337 == ccc...c mod 1337.

public class Solution {
  int DIV = 1337;

  List<Integer> findLoop(int a) {
    List<Integer> index = new ArrayList<>();
    boolean[] set = new boolean[DIV];
    int rem = a % DIV;
    while (!set[rem]) {
      set[rem] = true;
      index.add(rem);
      rem = (rem * a) % DIV;
    }
    return index;
  }

  int modBy(int[] b, int m) {
    int rem = 0;
    for (int i = 0; i < b.length; i++) {
      rem = (rem * 10 + b[i]) % m;
    }
    return rem;
  }

  public int superPow(int a, int[] b) {
    if (a == 0 || a == DIV || b == null || b.length == 0)
      return 0;
    if (a == 1)
      return 1;
    if (a > DIV)
      return superPow(a % DIV, b);
    List<Integer> index = findLoop(a);
    int loopsize = index.size();
    int rem = modBy(b, loopsize);
    rem = rem == 0 ? loopsize : rem;
    return index.get(rem - 1);
  }
}

 

 
 

转载于:https://www.cnblogs.com/neweracoding/p/5686863.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值