Genius ask the Java problem question in Typescript relate server

I currently learning JAVA, so i rewriting my old JS algorithm from leetcode to practice JAVA.
the problem is this code right below make "Time Limit Exceeded" error.
class Solution {
    public int mySqrt(int x) {
        long left = 0;
        long right = x;
        while(left <= right){
            long mid = left + (right - left) / 2;
            long doub = mid * mid;
            if(doub == x){
                return (int) mid;
            }
            if(doub > x){
                left =  mid - 1;
            }
            if(doub < x){
                right = mid + 1;
            }
        }
        return (int) right;
    }
}

but when you use same code but update else if else instead of just if guards it works fine.
class Solution {
    public int mySqrt(int x) {
        long left = 0;
        long right = x;

        while (left <= right) {
            long mid = left + (right - left) / 2;
            long doub = mid * mid;

            if (doub == x) {
                return (int) mid;
            } else if (doub > x) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return (int) right;
    }
}

i really can not understand why, because i tried to add "continue" at the end of every if statement and the same error occur when is should skip in that logic cicle.
Thanks in advance
Was this page helpful?