当前位置:网站首页>Leetcode(633)——平方数之和
Leetcode(633)——平方数之和
2022-06-29 23:02:00 【SmileGuy17】
Leetcode(633)——平方数之和
题目
给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a 2 + b 2 = c a^2 + b^2 = c a2+b2=c。
示例 1:
输入:c = 5
输出:true
解释:1 * 1 + 2 * 2 = 5
示例 2:
输入:c = 3
输出:false
提示:
- 0 0 0 <= c <= 2 31 − 1 2^{31 - 1} 231−1
题解
对于给定的非负整数 c c c,需要判断是否存在整数 a a a 和 b b b,使得 a 2 + b 2 = c a^2 + b^2 = c a2+b2=c。可以枚举 a a a 和 b b b 所有可能的情况,时间复杂度为 O ( c 2 ) O(c^2) O(c2)。但是暴力枚举有一些情况是没有必要的。例如:当 c = 20 c = 20 c=20 时,当 a = 1 a = 1 a=1 的时候,枚举 b b b 的时候,只需要枚举到 b = 5 b = 5 b=5 就可以结束了,这是因为 1 2 + 5 2 = 25 > 20 1^2 + 5^2 = 25 > 20 12+52=25>20。当 b > 5 b > 5 b>5 时,一定有 1 2 + b 2 > 20 1^2 + b^2 > 20 12+b2>20。
注意到这一点,可以使用 sqrt \texttt{sqrt} sqrt 函数或者双指针降低时间复杂度。
方法一:使用标准库函数 sqrt \texttt{sqrt} sqrt
思路
在枚举 a a a 的同时,使用 sqrt \texttt{sqrt} sqrt 函数找出 b b b。注意:本题 c c c 的取值范围在 [ 0 , 2 31 − 1 ] [0,2^{31} - 1] [0,231−1],因此在计算的过程中可能会发生 int \texttt{int} int 型溢出的情况,需要使用 long \texttt{long} long 型避免溢出。
代码实现
Leetcode 官方题解:
class Solution {
public:
bool judgeSquareSum(int c) {
for (long a = 0; a * a <= c; a++) {
double b = sqrt(c - a * a);
if (b == (int)b) {
return true;
}
}
return false;
}
};
复杂度分析
时间复杂度: O ( c ) O(\sqrt{c}) O(c)。枚举 a a a 的时间复杂度为 O ( c ) O(\sqrt{c}) O(c),对于每个 a a a 的值,可在 O ( 1 ) O(1) O(1) 的时间内寻找 b b b
空间复杂度: O ( 1 ) O(1) O(1)
方法二:反向双指针
思路
不失一般性,可以假设 i ≤ m a x i \le max i≤max。初始时 i = 0 i = 0 i=0, m a x = c max = \sqrt{c} max=c,进行如下操作:
- 如果 m a x 2 + i 2 = c max^2 + i^2 = c max2+i2=c,我们找到了题目要求的一个解,返回 true \text{true} true;
- 如果 m a x 2 + i 2 < c max^2 + i^2 < c max2+i2<c,此时需要将 i i i 的值加 1 1 1,继续查找;
- 如果 m a x 2 + i 2 > c max^2 + i^2 > c max2+i2>c,此时需要将 m a x max max 的值减 1 1 1,继续查找。
- 当 m a x = i max = i max=i 时,结束查找,此时如果仍然没有找到整数 m a x max max 和 i i i 满足 m a x 2 + i 2 = c max^2 + i^2 = c max2+i2=c,则说明不存在题目要求的解,返回 false \text{false} false。
至于反向双指针遍历已排序好的序列的正确性(即如何确保不会漏掉其中的正确答案): 可以参考这里。
所以用反向双指针要考虑两个问题:每次移动指针排除掉了哪些情况?这些情况中是否可能包含正确答案?
代码实现
我的:
class Solution {
public:
bool judgeSquareSum(int c) {
// 找出平方根小于或等于 c 的最大整数 max
int max = sqrt(c), i = 0;
while(i <= max){
// 不用 max*max + i*i 和 c 比较,因为可能会产生类型溢出
if(c - max*max == i*i) return true;
c - max*max < i*i? max--: i++;
}
return false;
}
};
复杂度分析
时间复杂度: O ( c ) O(\sqrt{c}) O(c)。最坏情况下 m a x max max 和 i i i 一共枚举了 0 0 0 到 c \sqrt{c} c 里的所有整数。
空间复杂度: O ( 1 ) O(1) O(1)
方法三:数学
思路
费马平方和定理告诉我们:
一个非负整数 c c c 如果能够表示为两个整数的平方和,当且仅当 c c c 的所有形如 4 k + 3 4k + 3 4k+3 的质因子的幂均为偶数。
证明请见 这里。
因此我们需要对 c c c 进行质因数分解,再判断所有形如 4 k + 3 4k + 3 4k+3 的质因子的幂是否均为偶数即可。
代码实现
Leetcode 官方题解:
class Solution {
public:
bool judgeSquareSum(int c) {
for (int base = 2; base * base <= c; base++) {
// 如果不是因子,枚举下一个
if (c % base != 0) {
continue;
}
// 计算 base 的幂
int exp = 0;
while (c % base == 0) {
c /= base;
exp++;
}
// 根据 Sum of two squares theorem 验证
if (base % 4 == 3 && exp % 2 != 0) {
return false;
}
}
// 例如 11 这样的用例,由于上面的 for 循环里 base * base <= c ,base == 11 的时候不会进入循环体
// 因此在退出循环以后需要再做一次判断
return c % 4 != 3;
}
};
复杂度分析
时间复杂度: O ( c ) O(\sqrt{c}) O(c)
空间复杂度: O ( 1 ) O(1) O(1)
边栏推荐
- How to solve the problem that the computer time is not automatically updated after proofreading
- Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
- Procurement intelligence is about to break out, and the "3+2" system of Alipay helps enterprises build core competitive advantages
- 均值、方差、标准差、协方差的概念及意义
- 记一次排查线上MySQL死锁过程,不能只会curd,还要知道加锁原理
- Solr基础操作2
- Qdomdocument and qdomnode are used in QT to read XML
- 触摸按键与按键控制对应的LED状态翻转
- Mysql database: use the show profile command to analyze performance
- An in-depth analysis of the election mechanism in kubernetes
猜你喜欢

Leetcode 1385. 两个数组间的距离值

为什么 JSX 语法这么香?
Talk about auto in MySQL in detail_ What is the function of increment

Ansible automatic operation and maintenance
Evolution from stand-alone to distributed database storage system

SQL question brushing 595 Big country

Regular expressions: characters (2)

error: C2665: “QMessageBox::critical”: 4 个重载中没有一个可以转换所有参数类型

均值、方差、标准差、协方差的概念及意义

采购数智化爆发在即,支出宝“3+2“体系助力企业打造核心竞争优势
随机推荐
记一次排查线上MySQL死锁过程,不能只会curd,还要知道加锁原理
Wechat applet: big red festive UI guessing lantern riddles is also called guessing character riddles
Taishan Office Technology Lecture: all elements in a row have the same height
数据库-玩转数据-Pgsql 使用UUID做主键
写论文工具:LaTex在线网站
GWD: rotating target detection based on Gaussian Wasserstein distance | ICML 2021
海外数字身份验证服务商ADVANCE.AI入选EqualOcean《2022品牌出海服务市场研究报告》
按头安利 好看又实用的点胶机 SolidWorks模型素材看这里
Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
How ZABBIX 5.0 adds esxi6.7 to monitoring
Leetcode 1385. 两个数组间的距离值
C language tutorial – -6 loop statement
Shell -- text processing command
Regular expressions: characters (2)
语音信号处理(二): 发声生理、听觉生理与听觉心理
Static keyword continuation, inheritance, rewrite, polymorphism
Constexpr function
How to solve the problem that the computer time is not automatically updated after proofreading
提供有效的绩效评估
uniapp复制内容到剪贴板