leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 6f7d7ab5663f2880f3b18dc75dfd236f4417f272
parent e2d6125e0cfc3e5b0d3f60bb7721cf06dfcc536e
Author: jack <jack@pngu.org>
Date:   Sun, 16 Jun 2024 21:50:35 -0600

630. sum of square numbers

Diffstat:
Asum_square_numbers.c | 22++++++++++++++++++++++
Asum_square_numbers.py | 26++++++++++++++++++++++++++
2 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/sum_square_numbers.c b/sum_square_numbers.c @@ -0,0 +1,22 @@ +// 633. Sum of Square Numbers +#include "leetcode.h" + +/* + * given a non-negative integer 'c', decide whether there are two integers 'a' + * and 'b' such that 'a^2 + b^2 = c' + */ + +bool judgeSquareSum(int c) { + int bound = sqrt(c); + for (int i = 0; i <= bound; i++) { + int a = i * i, b = sqrt(c - a); + if (b * b == c - a) + return true; + } + return false; +} + +int main() { + printf("%d\n", judgeSquareSum(5)); // expect: 1 + printf("%d\n", judgeSquareSum(3)); // expect: 0 +} diff --git a/sum_square_numbers.py b/sum_square_numbers.py @@ -0,0 +1,26 @@ +# 633. Sum of Square Numbers +from math import sqrt + +""" +given a non-negative integer 'c', decide whether there are two integers 'a' +and 'b' such that 'a^2 + b^2 = c' +""" + + +class Solution(object): + def judgeSquareSum(self, c): + """ + :type c: int + :rtype: bool + """ + for a in range(int(sqrt(c)) + 1): + b = sqrt(c - a * a) + if b == int(b): + return True + return False + + +if __name__ == "__main__": + obj = Solution() + print(obj.judgeSquareSum(5)) # expect: true + print(obj.judgeSquareSum(3)) # expect: false