【Leetcode_easy】633. Sum of Square Numbers
problem
题意:
solution1:
可以从c的平方根,注意即使c不是平方数,也会返回一个整型数。然后我们判断如果 i*i 等于c,说明c就是个平方数,只要再凑个0,就是两个平方数之和,返回 true;如果不等于的话,那么算出差值 c - i*i,如果这个差值也是平方数的话,返回 true。遍历结束后返回 false,
class Solution {
public:
bool judgeSquareSum(int c) {
for(int i=sqrt(c); i>=; --i)
{
if(i*i == c) return true;
int d = c - i*i, t = sqrt(d);
if(t*t == d) return true;
}
return false;
}
};
solution2:
使用hash set遍历保存至平方根的所有数值的平方,判断是否余值存在即可;
疑问:保存的时候同时进行判断,那么需要判断的余值确定已经在set里了嘛?
class Solution {
public:
bool judgeSquareSum(int c) {
unordered_set<int> sq;//err.
//for(int i=sqrt(c); i>=0; --i)//or...
for(int i=; i<=sqrt(c); ++i)
{
sq.insert(i*i);
if(sq.count(c-i*i)) return true;
}
return false;
}
};
solution3:左右向中间求解,类似于二分法;
疑问:不知道为什么必须使用long类型,否则出错!
class Solution {
public:
bool judgeSquareSum(int c) {
long left = , right = sqrt(c);//err.
while(left<=right)
{
if(left*left + right*right == c) return true;//
else if(left*left+right*right<c) ++left;
else --right;
}
return false;
}
};
solution4:
费马平方和定理 Fermat's theorem on sums of two squares 的一般推广形式:当某个数字的 4k+3 型的质数因子的个数均为偶数时,其可以拆分为两个平方数之和(each prime that is congruent to 3 mod 4 appears with an even exponent in the prime factorization of the number)。那么我们只要统计其质数因子的个数,并且判读,若其为 4k+3 型且出现次数为奇数的话直接返回 false。
参考
1. Leetcode_easy_633. Sum of Square Numbers;
2. Grandyang;
完
【Leetcode_easy】633. Sum of Square Numbers的更多相关文章
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【leetcode】633. Sum of Square Numbers(two-sum 变形)
Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c. ...
- 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...
- 【Leetcode_easy】985. Sum of Even Numbers After Queries
problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...
- 633. Sum of Square Numbers【Easy】【双指针-是否存在两个数的平方和等于给定目标值】
Given a non-negative integer c, your task is to decide whether there're two integers a and bsuch tha ...
- 【Leetcode_easy】1022. Sum of Root To Leaf Binary Numbers
problem 1022. Sum of Root To Leaf Binary Numbers 参考 1. Leetcode_easy_1022. Sum of Root To Leaf Binar ...
- [LeetCode] 633. Sum of Square Numbers 平方数之和
Given a non-negative integer c, your task is to decide whether there're two integers a and b such th ...
- 【leetcode】985. Sum of Even Numbers After Queries
题目如下: We have an array A of integers, and an array queries of queries. For the i-th query val = quer ...
- 【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找规律 日期 题目地址:https://lee ...
随机推荐
- jquery.cookie广告弹窗点击关闭后一天弹一次
jquery.cookie广告弹窗点击关闭后一天弹一次 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...
- 29、[源码]-AOP原理-AnnotationAwareAspectJAutoProxyCreatovi
29.[源码]-AOP原理-AnnotationAwareAspectJAutoProxyCreatovi
- MySQL-数据库三范式
数据库三范式 (1)第一范式(1NF): 定义:每一列都是不可分割的原子数据项(强调的是列的原子性): 例:一个表:[联系人](姓名,性别,电话) 如果在实际场景中,一个联系人有家庭电话和公司电话,那 ...
- Laravel 中使用 Repository 模式
在本文中,我会向你展示如何在 Laravel 中从头开始实现 repository 设计模式.我将使用 Laravel 5.8.3 版,但 Laravel 版本不是最重要的.在开始写代码之前,你需要了 ...
- PHP全栈学习笔记23
php,基础,流程控制,函数,字符串,数组,web交互,mysql数据库,PHP数据库编程,cookie与session,日期和时间,图形图形处理,文件和目录处理,面向对象,pdo数据库抽象层,sma ...
- 小程序开发--WePy框架
现如今mvvm框架如此火热,其核心思想即js逻辑层不直接操作DOM,只改变组件状态:而视图层则通过模板template进行渲染. 1.WePy项目的目录结构 ├── dist 小程序运行代码目录 ├─ ...
- JAVA基础知识|String、StringBuilder、StringBuffer比较
一.简介 String:不可变字符序列 StringBuffer:线程安全的可变字符序列 StringBuilder:非线程安全的可变字符序列 二.示例分析 [示例1] String A = &quo ...
- LeetCode 第 152 场周赛
一.质数排列(LeetCode-1175) 1.1 题目描述 1.2 解题思路 先统计出1-n中有多少个质数,得到质数个数\(x\),剩下的数\(y = n - x\): 使用排列组合公式得出结果 \ ...
- centos6和centos7中常用命令区别
以前一直接触的是centos6,最近因为新项目接触到centos7,发现有些命令还是有差异的(从centos7开始使用systemctl来管理服务和程序,包括了service和chkconfig),现 ...
- python 将数据写入excel
摘要链接: python第三方库——xlrd和xlwt操作Excel文件学习 :http://blog.csdn.net/wangkai_123456/article/details/50457284 ...