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; }
【本文章出自博客园willaty,转载请注明作者出处,误差欢迎指出~】