leetcode 633. Sum of Square Numbers
来源:互联网 发布:淘宝海外直邮是正品吗 编辑:程序博客网 时间:2024/06/07 01:21
Given a non-negative integer c
, your task is to decide whether there're two integers a
and b
such that a2 + b2 = c.
Example 1:
Input: 5Output: TrueExplanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: 3Output: False
这道题要注意两个测试用例:
2 , return true 。
2147482647,return false。
public boolean judgeSquareSum(int c) { HashSet<Integer> set = new HashSet<Integer>(); for (int i = 0; i <= Math.sqrt(c); i++) { set.add(i * i); if (set.contains(c - i * i)) { return true; } } return false;}有大神用了类似于二分查找的思想。
public class Solution { public boolean judgeSquareSum(int c) { if (c < 0) { return false; } int left = 0, right = (int)Math.sqrt(c); while (left <= right) { int cur = left * left + right * right; if (cur < c) { left++; } else if (cur > c) { right--; } else { return true; } } return false; }}
Approach #3 Using sqrt function [Accepted]
Algorithm
Check if √c−a2 turns out to be an integer.
Java
public class Solution { public boolean judgeSquareSum(int c) { for (long a = 0; a * a <= c; a++) { double b = Math.sqrt(c - a * a); if (b == (int) b) return true; } return false; }}
Complexity Analysis
Time complexity : O(√clog(c)). We iterate over √c values for choosing a. For every a chosen, finding square root of c−a2 takes O(log(c)) time in the worst case.
Space complexity : O(1). Constant extra space is used.
Approach #4 Using Binary Search [Accepted]
Algorithm
Another method to check if c−a2 is a perfect square, is by making use of Binary Search.
Java
public class Solution { public boolean judgeSquareSum(int c) { for (long a = 0; a * a <= c; a++) { int b = c - (int)(a * a); if (binary_search(0, b, b)) return true; } return false; } public boolean binary_search(long s, long e, int n) { if (s > e) return false; long mid = s + (e - s) / 2; if (mid * mid == n) return true; if (mid * mid > n) return binary_search(s, mid - 1, n); return binary_search(mid + 1, e, n); }}
Complexity Analysis
Time complexity : O(√clog(c)). Binary search taking O(log(c)) in the worst case is done for √c values of a.
Space complexity : O(log(c)). Binary Search will take O(log(c)) space.
Approach #5 Fermat Theorem [Accepted]:
Algorithm
这个方法基于费马的平方和定理:
任何正整数 n 是两个平方数之和的充分必要条件是: n 的任意满足 (4k+3) 的素数因子都满足出现了偶数次。
Interested reader can refer to this documentation.Java
public class Solution { public boolean judgeSquareSum(int c) { for (int i = 2; i * i <= c; i++) { int count = 0; if (c % i == 0) { while (c % i == 0) { count++; c /= i; } if (i % 4 == 3 && count % 2 != 0) return false; } } return c % 4 != 3; }}
Complexity Analysis
Time complexity : O(√clog(c)). We find the factors of c and their count using repeated division. We check for the factors in the range [0,√c]. The maximum number of times a factor can occur(repeated division can be done) is log(n)(considering 2 as the only factor, c=2x. Thus, x=log(c)).
Space complexity : O(1). Constant space is used.
- [leetcode]633. Sum of Square Numbers
- [LeetCode]633. Sum of Square Numbers
- LeetCode 633. Sum of Square Numbers
- [leetcode]633. Sum of Square Numbers
- leetcode 633. Sum of Square Numbers
- LeetCode 633. Sum of Square Numbers
- leetcode 633. Sum of Square Numbers
- leetcode 633. Sum of Square Numbers
- [LeetCode]633. Sum of Square Numbers
- leetcode[Sum of Square Numbers]
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers。
- 【leetcode】633. Sum of Square Numbers(Python & C++)
- leetcode 633. Sum of Square Numbers 二分查找
- 【译】交叉验证(Crossd_validation)
- 使用maven搭建ssm框架的javaweb项目
- Hystrix
- php代码优化的方法
- 用TensorFlow构建基础的神经网络(二):LeNet-5
- leetcode 633. Sum of Square Numbers
- 内推君整理数据库面试知识点
- Dumpbin命令查看软件32位/64位
- 文章标题
- 工作心得
- 关于bootstrap模态框的初始化事件
- C++函数学习
- 高级算法日记5:二叉树
- HttpClient包访问网络数据