leetcode 367. Valid Perfect Square

二分。

    bool isPerfectSquare(int num) {
        if (num == 0 || num == 1)
            return true;

        int start = 0;
        int end = num;
        while (start <= end) {
            int chu = start + (end - start) / 2;
            int res = num / chu;
            if (res == chu && num % chu == 0)
                return true;
            if (chu > res)
                end = chu - 1;
            else
                start = chu + 1;
        }
        return false;
    }

 

posted on 2018-02-09 16:26  willaty  阅读(86)  评论(0编辑  收藏  举报

导航