题目:

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.

Example 1:

Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5

Example 2:

Input: 3
Output: False

分析:

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

我们可以给定一个a,来判断c-a*a是不是等于(sqrt(c-a*a))^2,因为一个数如果可以开方,且这个数开方后再平方的话和原来相等就是完全平方数,也就是我们所求的b。

程序:

class Solution {
public:
bool judgeSquareSum(int c) {
for(double a = ; a*a <= c; ++a){
int b = sqrt(c - a*a);
if(b*b == (c - a*a))
return true;
}
return false;
}
};

LeetCode 633. Sum of Square Numbers平方数之和 (C++)的更多相关文章

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

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

  3. Leetcode633.Sum of Square Numbers平方数之和

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

  4. #Leetcode# 633. Sum of Square Numbers

    https://leetcode.com/problems/sum-of-square-numbers/ Given a non-negative integer c, your task is to ...

  5. 【Leetcode_easy】633. Sum of Square Numbers

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

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

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

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

  8. 【LeetCode】633. Sum of Square Numbers

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

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

随机推荐

  1. PyQt5 的几个核心模块作用

    QtCore  包含了核心的非GUI功能.此模块用于处理时间.文件和目录.各种数据类型.流.URL.MIME类型.线程或进程. QtGui  包含类窗口系统集成.事件处理.二维图形.基本成像.字体和文 ...

  2. [转载]ArcGIS SERVER 9.3如何清除REST缓存

    有时候,发布了一个服务后,但是点击服务后,不能显示出来 http://hostname/ArcGIS/rest/services/服务名称/MapServer 这时候,十有八九是因为REST缓存没有清 ...

  3. R语言(资源)

    #学习 R 的方法 知识和耐心,是成为强者的唯一方法. - 通过阅读来学习.包括了阅读经典的教材.代码.论文.学习公开课.- 通过牛人来学习.包括同行的聚会.讨论.大牛的博客.微博.twitter.R ...

  4. PHP(Mysql/Redis)消息队列的介绍及应用场景案例--转载

    郑重提示:本博客转载自好友博客,个人觉得写的很牛逼所以未经同意强行转载,原博客连接 http://www.cnblogs.com/wt645631686/p/8243438.html 欢迎访问 在进行 ...

  5. MediaPlayer: Couldn't open /storage/emulated/0/kgmusic/download/独家记忆.mp3: java.io.FileNotFoundExcept

    写了一个音乐播放器,播放的时候,会出现这样的问题:比如说我点击第三首歌曲,结果没有播放第三首歌曲,而直接播放了第四首歌曲.看了一下日志.发现报错:MediaPlayer: Couldn't open ...

  6. Recent papers on Action Recognition | 行为识别最新论文

    CVPR2019 1.An Attention Enhanced Graph Convolutional LSTM Network for Skeleton-Based Action Recognit ...

  7. poj 3169 Layout(线性差分约束,spfa:跑最短路+判断负环)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15349   Accepted: 7379 Descripti ...

  8. HDU 1421 搬寝室(经典DP,值得经常回顾)

    搬寝室 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status D ...

  9. Linux SSH远程文件/文件夹传输命令scp

    相信各位VPSer在使用VPS时会常常在不同VPS间互相备份数据或者转移数据,大部分情况下VPS上都已经安装了Nginx或者类似的web server,直接将要传输的文件放到web server的文件 ...

  10. html元素的分类以及特点

    解释几个概念: 替换元素:官方解释是,一个内容不受css视觉格式化模型控制,css渲染模型并不考虑对此内容的渲染,且元素一般拥有固定的尺寸,(高度,宽度)的元素,被称为置换元素.通俗来说就是浏览器根据 ...