classSolution { public: // 判断是否为完全平方数 boolisPerfectSquare(int x){ int y = sqrt(x); return y * y == x; }
// 判断是否能表示为 4^k*(8m+7) boolcheckAnswer4(int x){ while (x % 4 == 0) { x /= 4; } return x % 8 == 7; }
intnumSquares(int n){ if (isPerfectSquare(n)) { return1; } if (checkAnswer4(n)) { return4; } for (int i = 1; i * i <= n; i++) { int j = n - i * i; if (isPerfectSquare(j)) { return2; } } return3; } };
classSolution { publicintnumSquares(int n) { if (isPerfectSquare(n)) { return1; } if (checkAnswer4(n)) { return4; } for (inti=1; i * i <= n; i++) { intj= n - i * i; if (isPerfectSquare(j)) { return2; } } return3; }
// 判断是否为完全平方数 publicbooleanisPerfectSquare(int x) { inty= (int) Math.sqrt(x); return y * y == x; }
// 判断是否能表示为 4^k*(8m+7) publicbooleancheckAnswer4(int x) { while (x % 4 == 0) { x /= 4; } return x % 8 == 7; } }
publicclassSolution { publicintNumSquares(int n) { if (IsPerfectSquare(n)) { return1; } if (CheckAnswer4(n)) { return4; } for (int i = 1; i * i <= n; i++) { int j = n - i * i; if (IsPerfectSquare(j)) { return2; } } return3; }
// 判断是否为完全平方数 publicboolIsPerfectSquare(int x) { int y = (int) Math.Sqrt(x); return y * y == x; }
// 判断是否能表示为 4^k*(8m+7) publicboolCheckAnswer4(int x) { while (x % 4 == 0) { x /= 4; } return x % 8 == 7; } }
var numSquares = function(n) { if (isPerfectSquare(n)) { return1; } if (checkAnswer4(n)) { return4; } for (let i = 1; i * i <= n; i++) { let j = n - i * i; if (isPerfectSquare(j)) { return2; } } return3; }
// 判断是否为完全平方数 constisPerfectSquare = (x) => { const y = Math.floor(Math.sqrt(x)); return y * y == x; }
// 判断是否能表示为 4^k*(8m+7) constcheckAnswer4 = (x) => { while (x % 4 == 0) { x /= 4; } return x % 8 == 7; }
// 判断是否为完全平方数 boolisPerfectSquare(int x) { int y = sqrt(x); return y * y == x; }
// 判断是否能表示为 4^k*(8m+7) boolcheckAnswer4(int x) { while (x % 4 == 0) { x /= 4; } return x % 8 == 7; }
intnumSquares(int n) { if (isPerfectSquare(n)) { return1; } if (checkAnswer4(n)) { return4; } for (int i = 1; i * i <= n; i++) { int j = n - i * i; if (isPerfectSquare(j)) { return2; } } return3; }