Skip to content

Commit 0e7ce14

Browse files
committed
day 25
1 parent fa27dc5 commit 0e7ce14

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Sum of Square Numbers
3+
=====================
4+
5+
Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c.
6+
7+
Example 1:
8+
Input: c = 5
9+
Output: true
10+
Explanation: 1 * 1 + 2 * 2 = 5
11+
12+
Example 2:
13+
Input: c = 3
14+
Output: false
15+
16+
Example 3:
17+
Input: c = 4
18+
Output: true
19+
20+
Example 4:
21+
Input: c = 2
22+
Output: true
23+
24+
Example 5:
25+
Input: c = 1
26+
Output: true
27+
28+
Constraints:
29+
0 <= c <= 231 - 1
30+
*/
31+
32+
class Solution {
33+
public:
34+
bool judgeSquareSum(int c) {
35+
int sqrt_c = sqrt(c);
36+
if(sqrt_c * sqrt_c == c) return true;
37+
38+
for(int a = 0; a <= sqrt(c); ++a) {
39+
int b_sq = c - (a*a);
40+
int b = sqrt(b_sq);
41+
if(b*b == b_sq) return true;
42+
}
43+
44+
return false;
45+
}
46+
};

Leetcode Daily Challenge/August-2021/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@
2626
| 21. | [Sudoku Solver](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3905/) | [cpp](./20Sudoku%20Solver.%20.cpp) |
2727
| 23. | [Two Sum IV - Input is a BST](https://leetcode.com/explore/featured/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3908/) | [cpp](./23.%20Two%20Sum%20IV%20-%20Input%20is%20a%20BST.cpp) |
2828
| 24. | [Complex Number Multiplication](https://leetcode.com/explore/featured/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3917/) | [cpp](./24.%20Complex%20Number%20Multiplication.cpp) |
29+
| 25. | [Sum of Square Numbers](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3918/) | [cpp](./25.%20Sum%20of%20Square%20Numbers.cpp) |

0 commit comments

Comments
 (0)