题目:

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. eclispe快捷键

    ① Ctrl+Left/Right 向左或向右跳跃一个单词,这是解决横向光标定位速度问题最主要的快捷键!(特别对于喜欢写超过80个字符一行代码的人来说)需要配合使用同样用于同行光标定位的Home/En ...

  2. SVN 图标不显示的解决办法

    SVN 的图标没办法显示了.经搜索,发现需要修改注册表 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Sh ...

  3. volatile和synchronized的区别与联系[转]

    volatile是一个变量修饰符,而synchronized是一个方法或块的修饰符.所以我们使用这两种关键字来指定三种简单的存取变量的方式. int i1;                       ...

  4. 1.Dubbo2.5.3源码编译

    转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.安装JAVA.Git.Maven 安装过程省略,请自行百度.       2.编译dubbo (1)从ht ...

  5. webstorm 搭建es6开发环境

    本文转自:http://www.jianshu.com/p/26601581e152 1:新建一个Empty Project项目es6 ,然后在src目录下新建了一个es.js: 2:打开设置pref ...

  6. 2017-2018-2 20165318 实验三《Java面向对象程序设计》实验报告

    2017-2018-2 20165318 实验三<Java面向对象程序设计>实验报告 一.实验报告封面 课程:Java程序设计        班级:1653班        姓名:孙晓暄  ...

  7. 【转】网络安全——一图看懂HTTPS建立过程

    阅读目录 准备工作(对应图中prepare1234) 发起链接 最后 关于网络安全加密的介绍可以看之前文章: 1. 网络安全——数据的加密与签名,RSA介绍2. Base64编码.MD5.SHA1-S ...

  8. AES块加密与解密

    AES块加密与解密 解密目标 在CBC和CTR两种模式下分别给出十篇加密的样例密文,求解密一篇特定的密文 解密前提 全部密文及其加密使用的key都已给出 加密的方法遵循AES的标准 解密过程分析 实验 ...

  9. 【测试123】ISTQB AL

    近一年时间加强了金融领域基础知识,希望能顺利获得FRM认证. 接下来因为工作需要,在测试工程师的角色上有所深入发展. 仔细想了下,一是管理上的角色转换,如何协调各个不同测试级别,以及如何成为一个称职满 ...

  10. MetaMask/provider-engine-1

    https://github.com/MetaMask/provider-engine 在学习这个之前应该先看一下什么是zero-client,MetaMask/zero-client Web3 Pr ...