Skip to content

Latest commit

 

History

History
31 lines (22 loc) · 640 Bytes

69 Sqrt(x) 04fa56dd0fab42cb81284ae760067ca1.md

File metadata and controls

31 lines (22 loc) · 640 Bytes

69. Sqrt(x)

Sqrt(x) - LeetCode

package com.mycompany.firstproject;

public class test3 {

    public static int mySqrt(int x) {
        int low = 1 , high = x  , ans = 0;

            while (low <= high){
              int mid = low + (high - low) /2;

                if (x/mid == mid) return mid;
                else if (x/mid < mid)  high = mid-1;
                else {
                    low= mid + 1; ans=mid;
                }
            }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(mySqrt(16));

    }

}