problem

633. Sum of Square Numbers

题意:

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的更多相关文章

  1. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  2. 【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. ...

  3. 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...

  4. 【Leetcode_easy】985. Sum of Even Numbers After Queries

    problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...

  5. 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 ...

  6. 【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 ...

  7. [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 ...

  8. 【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 ...

  9. 【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找规律 日期 题目地址:https://lee ...

随机推荐

  1. docker-compose.yml的使用

    docker-compose.yml包含version.services.networks3大部分 services的书写规则 1.iamge services: web: # 服务名称,用户自定义 ...

  2. Hive的安装和配置

    前提是:hadoop必须已经启动了***         1°.解压hive的安装包            [crxy@master soft]# tar -zxvf apache-hive-0.14 ...

  3. Oracle 11g 禁用 SQL Tuning Advisor 与 auto space advisor

    生产上有一套11g数据库alert.log报错ORA-16957: SQL Analyze time limit interrupt.  查询MOS相关文档Troubleshooting: ORA-1 ...

  4. The Boot Process at a Glance x86/x64系统启动过程解析

    哥又来干体力活了.人肉翻译一下: The Boot Process at a Glance This section explains the boot process in sufficient d ...

  5. ST表 「 从入门到入门 · 浅显理解 」

    ST 表是个好东西,虽然前些天 ldq 学长已经讲完啦,但是那天他讲了那么多,让智商受限的我完全没有全部接受,选择性的扔掉了一部分(其实不舍的扔,记不住QAQ). ST 表最简单的应用就是查询区间最大 ...

  6. MySQL 聚集拼接

    GROUP_CONCAT()函数 示例: 假设现在有这样一个表结构: 其中`student`.`school_id`是逻辑外键 想要检索出所有学校,其中学校下的学生名需要拼接在一起,作为结果集的字段 ...

  7. PHP 之Html标签转义与反转义

    1.htmlentities()函数转义html 2.html_entity_decode()函数反转义html 我这里是用来反转义富文本编辑器的内容

  8. 关于bootstrap table 固定列宽

    首先为table 设置 style="table-layout: fixed;" <table id="assessStage" data-height= ...

  9. JAVA基础知识|synchronized和lock

    一.synchronized 是jvm的一个关键字,使用过程均由jvm控制 有三种使用方式: 修饰实例方法,作用于当前实例加锁,进入同步代码前要获得当前实例的锁 修饰代码块,同方法 修饰静态方法,作用 ...

  10. 一起来学习React-Native之react-navigation基本解析

    前言   不久前自己也完整开发了一个React-Native项目,对其中的一些知识存在疑惑,再加上项目时间比较紧张,来不及做系统的学习.现在来回顾自己开发当中存在的疑惑点,和大家分享.第一篇是关于路由 ...