avatar

superorange0707

  • Homepage
  • Tag
  • Category
Home Leetcode-69 - Sqrt(x)
文章

Leetcode-69 - Sqrt(x)

Posted 2025-04-9
6~8 min read

[69 - Sqrt(x)]

🔗 LeetCode Link


Problem Description

Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well. Constraints: 0 <= x <= 231 - 1


Idea

Since the array is sorted, binary search is the most efficient choice (O(log n)).


Logic

  1. Define your search range (0 - x)
  2. Use a loop (while (left <= right) or while (left < right))
  3. Calculate mid, check if square(mid) == x
  4. change to the boundary accordingly
  5. • Use a variable ans to store the best valid value seen so far (ans = mid when mid * mid <= x)
    • This helps when the square root isn’t exact — we round down
  6. Use (long) mid * mid to prevent integer overflow in large input cases (e.g. x = 2147395599)

Code (Java - Closed Interval)

class Solution {
    public int mySqrt(int x) {
        // set search range
        int left = 0;
        int right = x;
        int ans = -1;

        // improve effiency
        if (x<2){
            return x;
        }

        while(left <= right){
            int mid = left + ((right - left)/2);

            // use (long)mid * mid to avoid int overflow(int is 2^31 - 1 ) 
            if( (long)mid * mid > x){
                // move to left boundary
                right = mid -1;
            }
            else if ((long)mid * mid < x){

                // make the result will round to nearest
                ans = mid;

                // move to right boundary
                left = mid + 1;
            }
            else{
                return mid;
            }

        }
        return ans;
    }
}


Code (Java - Half-Open Interval)

class Solution {
    public int mySqrt(int x) {
        // set search range
        int left = 0;
        int right = x;

    // improve effiency
        if (x<2){
            return x;
        }

        while(left < right){
            int mid = left + ((right - left)/2);

            // use (long)mid * mid to avoid int overflow(int is 2^31 - 1 ) 
            if( (long)mid * mid > x){
                // move to left boundary
                right = mid;
            }
            else if ((long)mid * mid < x){
                // move to right boundary
                left = mid + 1;
            }
            else{
                return mid;
            }

        }

        // to round down
        return left - 1;
    }
}
Binary Search, Leetcode
Share

Further Reading

Apr 10, 2025

34 - Find First and Last Position of Element in Sorted Array

Apr 10, 2025

367-Valid Perfect Square

Apr 9, 2025

Leetcode-69 - Sqrt(x)

[69 - Sqrt(x)]🔗 LeetCode LinkProblem DescriptionGiven a non-negative integer x, return the square root of x rounded down to the nearest integer. The

OLDER

Leetcode-35 -Search Insert Position

NEWER

367-Valid Perfect Square

Recently Updated

  • Migrating Jenkins SCM Using GitLab from Bitbucket: SCM URL Bulk Replacement
  • 283 - Move Zero
  • 27 - Remove Element
  • 26 - Remove Duplicates from Sorted Array
  • Migrating from Bitbucket to GitLab? Here’s how to keep your teams moving without missing a beat!

Trending Tags

Course two pointer Binary Tree Hash SQL Leetcode Error Recording Gitlab Bitbucket Devops

Contents