给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c。

示例1:

输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5

示例2:

输入: 3 输出: False

方法一:
class Solution {
public:
bool judgeSquareSum(int c) {
for(int i = sqrt(c); i >= 0; i--)
{
int j = sqrt(c - i * i);
if(i * i + j * j == c)
return true;
}
return false;
}
};
方法二:
class Solution {
public:
bool judgeSquareSum(int c) {
int low = 0;
int high = sqrt(c);
while(low <= high)
{
int mid = low * low + high * high;
if(mid == c)
return true;
else if(mid > c)
{
high--;
}
else
{
low++;
}
}
return false;
}
};

Leetcode633.Sum of Square Numbers平方数之和的更多相关文章

  1. [LeetCode] 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 ...

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

  3. LeetCode 633. Sum of Square Numbers平方数之和 (C++)

    题目: Given a non-negative integer c, your task is to decide whether there're two integers a and b suc ...

  4. LeetCode633. Sum of Square Numbers(双指针)

    题意:给定一个非负整数c,确定是否存在a和b使得a*a+b*b=c. class Solution { typedef long long LL; public: bool judgeSquareSu ...

  5. C#刷遍Leetcode面试题系列连载(4) No.633 - 平方数之和

    上篇文章中一道数学问题 - 自除数,今天我们接着分析 LeetCode 中的另一道数学题吧~ 今天要给大家分析的面试题是 LeetCode 上第 633 号问题, Leetcode 633 - 平方数 ...

  6. 【Leetcode_easy】633. Sum of Square Numbers

    problem 633. Sum of Square Numbers 题意: solution1: 可以从c的平方根,注意即使c不是平方数,也会返回一个整型数.然后我们判断如果 i*i 等于c,说明c ...

  7. C#版 - Leetcode 633. 平方数之和 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  8. Java实现 LeetCode 633 平方数之和(暴力大法)

    633. 平方数之和 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 ...

  9. 【JavaScript】Leetcode每日一题-平方数之和

    [JavaScript]Leetcode每日一题-平方数之和 [题目描述] 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c . 示例1: 输入:c = 5 ...

随机推荐

  1. js实现获取两个日期之间所有日期的方法

    function getDate(datestr){ var temp = datestr.split("-"); var date = new Date(temp[0],temp ...

  2. 杂项-公司:SAMSUNG

    ylbtech-杂项-公司:SAMSUNG 三星集团是韩国最大的跨国企业集团,同时也是上市企业全球500强,三星集团包括众多的国际下属企业,旗下子公司有:三星电子.三星物产.三星航空.三星人寿保险等, ...

  3. <jsp:forward page=""></jsp:forward>标签失效异常

    解决方案:在web.xml <filter-mapping>          <filter-name>struts2</filter-name>         ...

  4. 删除文件夹时提示“You need permission to perform this action。。。”,如何解决?

    Win10系统,有时,要删除某个文件夹,却提示“You need permission to perform this action...” 以下是我Google之后找到的解决方案 1.创建一个文本文 ...

  5. 玩转大数据系列之Apache Pig高级技能之函数编程(六)

    原创不易,转载请务必注明,原创地址,谢谢配合! http://qindongliang.iteye.com/ Pig系列的学习文档,希望对大家有用,感谢关注散仙! Apache Pig的前世今生 Ap ...

  6. windows 遍历目录下的所有文件 FindFirstFile FindNextFile

    Windows下遍历文件时用到的就是FindFirstFile 和FindNextFile 首先看一下定义: HANDLE FindFirstFile( LPCTSTR lpFileName, // ...

  7. linux系统下重要的分区及其作用

    下面列出来的是linux系统下重要的分区及其作用/bin :bin是binary的缩写;/boot :存放启动Linux时使用的一些核心文件;/root :root(超级管理员)的用户主目录;/sbi ...

  8. vbox虚拟机复制&&虚拟机指定静态IP

    一.复制镜像(假设源镜像已经用桥接方式,可以访问互联网). 注意需要重新生成mac地址 二.复制完成,启动复制好的镜像(注意,此时的镜像无法联网) vi /etc/udev/rules.d/70-pe ...

  9. 装配SpringBean(四)--注解装配之组件扫描

    前两篇文章我总结了通过XML方式装配bean的实现方式,虽然比较简单,但是需要配置很多,很多时候我们都会使用注解进行装配.使用注解的方式可以减少XML的配置,既能实现XML的功能,还提供了自动装配功能 ...

  10. SpringData初探

    前言 项目中用到这个,没有学过,手动搭建,测试执行流程, 理论的东西有时间再补充 Maven依赖 <?xml version="1.0" encoding="UTF ...