-
Notifications
You must be signed in to change notification settings - Fork 24
/
0633-sum-of-square-numbers.js
55 lines (41 loc) · 1.1 KB
/
0633-sum-of-square-numbers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 633. Sum of Square Numbers
// Easy 32%
// Given a non-negative integer c, your task is to decide whether there're two
// integers a and b such that a^2 + b^2 = c.
// Example 1:
// Input: 5
// Output: True
// Explanation: 1 * 1 + 2 * 2 = 5
// Example 2:
// Input: 3
// Output: False
/**
* @param {number} c
* @return {boolean}
*/
const judgeSquareSum = function(c) {
const s = Math.floor(Math.sqrt(c / 2))
for (let i = 0; i <= s; i++) {
if (Number.isInteger(Math.sqrt(c - i * i))) return true
}
return false
}
;[
5, // true
3, // false
2, // true
32, // true
].forEach(c => {
console.log(judgeSquareSum(c))
})
// Solution:
// 已知:非负整数 c, 满足 a^2 + b^2 = c,
// 求:整数 a,b 是否存在
// 因为 a^2 <= c
// 所以 0 <= a <= sqrt(c) (b 同理)
// 若假设 a <= b,
// 则 a^2 <= (c / 2)
// 则 0 <= a <= sqrt(c / 2)
// 迭代 a 从 0 到 sqrt(c / 2),计算 b = sqrt(c - a^2),
// 如果 b 为整数,则返回 true
// Submission Result: Accepted