avoid overflow


  • 0
    L

    class Solution {
    public:
    int mySqrt(int x) {
    if(x==0) return 0;
    double r = x;
    // 注意r要是double类型的
    for(int i=1;i<=r/2+1;i++){
    if(r/i>=i && r/(i+1)<(i+1)) return i;
    }
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.