Title:Sum of Square Numbers 633
Difficulty:Easy
原题leetcode地址: https://leetcode.com/problems/perfect-number/
1. 假设问题
时间复杂度:O(n),一次一层for循环,循环最长为根号n。
空间复杂度:O(1),没有申请额外空间。
/**
* 先假设a是,再判断b是不是
* @param c
* @return
*/
public static boolean judgeSquareSum(int c) {
if (c < 0) {
return false;
}
for (int a = (int) Math.sqrt(c); a >= 0; a--) {
if (a * a == c) {
return true;
}
int db = c - a * a;
int b = (int) Math.sqrt(db);
if (b * b == db) {
return true;
}
}
return false;
}