码迷,mamicode.com
首页 > 其他好文 > 详细

69. Sqrt(x) && 367. Valid Perfect Square

时间:2016-07-20 06:29:35      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

69. Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

Hide Tags
 Binary Search Math
Hide Similar Problems
 (M) Pow(x, n) (M) Valid Perfect Square
 
 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  
Hide Tags
 Binary Search Math
Hide Similar Problems
 (M) Sqrt(x)
 
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;
    }
}

 

 
 

69. Sqrt(x) && 367. Valid Perfect Square

标签:

原文地址:http://www.cnblogs.com/neweracoding/p/5686863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!