본문 바로가기
Problem Solving/LeetCode

[LeetCode] 69. Sqrt(x) - Java

by graycode 2026. 5. 22.

 문제 링크

 

Sqrt(x) - LeetCode

Can you solve this real interview question? Sqrt(x) - 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. You must not use any built-in exponent function or o

leetcode.com

 

 풀이 코드

public class Solution {

    public int mySqrt(int x) {
        long n = x;
        while (n * n > x) n = (n + x / n) >> 1;

        return (int) n;
    }

}

댓글