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. vmware 共享文件夹不显示文件的问题

    上海SEO:安装vmtools后还是不显示执行以下操作//但是只有root权限才行 1:输入命令  sudo apt install open-vm-tools 安装工具2:输入命令 sudo vmh ...

  2. vue app.xxx.js 较大问题

    线上build之后发现app.XXX.js 文件特别大. 包我都改为cdn了 其他空间就是路由改为懒加载了. { path: '/a/b', name: 'ab', component: () =&g ...

  3. 在Vue中加入国际化(i18n)中英文功能

    1.npm安装方法 npm install vue-i18n --save 2.在src资源文件下创建文件夹i18n,i18n下面创建index.js文件,引入VueI18n和导入语言包(按开发需求可 ...

  4. React应用数据传递的方式

    1. props属性 典型的React应用,数据通过props按照自上而下(父->子)的顺序传递数据. 2. Context传值 1. 应用场景 对于一些应用中全局性的属性(如UI主题.语言.登 ...

  5. SpringMVC返回类型

    7.SpringMVC的返回值类型和参数传递 1.SpringMVC的返回值类型 (1)ModelAndView返回值类型: 1.1当返回为null时,页面不跳转. 1.2当返回值没有指定视图名时,默 ...

  6. 十三.基础邮件服务、parted分区工具、交换分区、链路聚合

    1.基础邮件服务 DNS服务器:虚拟机classroom   以server0.example.com 为例  yg@server0.example.com    xln@server0.exampl ...

  7. 节点(node)操作

    一.节点的属性 节点值页面中的所有内容,包括标签.属性.文本 nodeType,节点类型:如果是标签,则是1:如果是属性.则是2:如果是文本,则是3 nodeName,节点名字:如果是标签,则是大写的 ...

  8. loj #136

    最小瓶颈路 做最小生成树是进行特判即可 时间复杂度 n * k #include <bits/stdc++.h> const int N = 1010, M = 1e5 + 10; str ...

  9. LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律

    二次联通门 : LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律 /* LibreOJ #516. 「LibreOJ β Round #2」DP 一般看规律 set ...

  10. linux系列(五):rm命令

    rm是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比如在/(根目录)下执行rm * -rf).所以,我们在执行rm之前最好先确认一下在哪个目录,到底要删除什么东西 ...