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. PL/SQL复合类型

    一.PL/SQL记录:一条记录. 可简化单行多列的数据的处理.当使用pl/sql记录时,应用开发人员即可以自定义记录类型和记录变量,也可以使用%rowtype属性直接定义记录变量. 1.当使用自定义的 ...

  2. spark操作hive方式(scala)

    第一种方式: def operatorHive: Unit = { Class.forName("org.apache.hive.jdbc.HiveDriver") val url ...

  3. luogu 1144

    最短路计数 #include <bits/stdc++.h> using namespace std; , M = 2e6 + ; << ); #define gc getch ...

  4. js 的 二进制

    1. 整数 例如十进制的 30 30/2  .......... 0 15/2 ............ 1 7/2 ............ 1 3/2 .............. 1 1/2 . ...

  5. 一些scala的操作

    Scala获取当前目录下所有文件 import java.io.File //获取目录下的所有文件,当前项目目录输入new File(".") def getFiles1(dir: ...

  6. C++标准库分析总结(一)

    之前学习过标准库,最近身边有人问到相关话题,故在此做一个总结 1 标准库介绍 C++标准库:C++ Standard Library C++标准模板库:Standard Template Librar ...

  7. NOIP2014提高组 题解报告

    D1 T1 无线网路发射器选址 题目大意:找一个矩形,使其覆盖的目标点最大. 题目过水,直接暴力搞过去,代码就不贴了. 但我TM居然有个地方SB了,调了半天才发现输入有问题: scanf(" ...

  8. 使用vscode快速建立vue模板

    当我们希望每次新建.vue文件后,vscode能够根据配置,自动生成我们想要的内容. 打开vscode编辑器,依次选择“文件 -> 首选项 -> 用户代码片段”,此时,会弹出一个搜索框,我 ...

  9. 深入了解JVM虚拟机8:Java的编译期优化与运行期优化

    java编译期优化 java语言的编译期其实是一段不确定的操作过程,因为它可以分为三类编译过程:1.前端编译:把.java文件转变为.class文件2.后端编译:把字节码转变为机器码3.静态提前编译: ...

  10. qt QThread

    QThread类提供了一个平台无关的方式来管理线程. 一个QThread对象在程序控制中管理一个线程.线程在run()中开始执行.默认情况下,run()通过调用exec()启动事件循环并在线程里运行一 ...