题目:

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. IIS 部署SSL证书

    1.导入证书 打开IIS服务管理器,点击计算机名称,双击‘服务器证书 双击打开服务器证书后,点击右则的导入 选择证书文件,点击确定 2.站点开启SSL 选择证书文件,点击确定 点击网站下的站点名称,点 ...

  2. C#中的事件(event)处理机制

    委托 语法 [访问修饰符] delegate 返回类型 委托名(); 委托的特点 类似于C++函数指针,但它是类型安全的:委托允许将方法作为参数进行传递:委托可用于定义回调方法:委托可以链接在一起:如 ...

  3. 8、Django的模型层(2)

    第3节:多表操作 3.1 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作 ...

  4. 开发jQuery插件的基本步骤

    在进行开发jQuery插件前,首先要了解一些知识: 1.闭包 1.1.闭包的作用: · 避免全局依赖 · 避免第三方破坏 · 兼容jQuery操作符'$'和jQuery 1.2.闭包的形式 (func ...

  5. spring引入properties变量报错

    通过properties配置文件配置数据源,代码如下: <bean class="org.springframework.beans.factory.config.PropertyPl ...

  6. 多线程之CAS

    在JDK 5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁 锁机制存在以下问题: (1)在多线程竞争下,加锁.释放锁会导致比较多的上下文切换和调度延时,引起性能问题. (2 ...

  7. mysql row日志格式下 查看binlog sql语句

    有时候我们需要使用row作为binlog的日志格式,即配置文件使用了binlog_format= row 参数 这样以来,我们在查看数据库binlog内容时候,就看不到增删改查的具体语句了,在数据库恢 ...

  8. liMarquee – jQuery无缝滚动插件(制作跑马灯效果)

    liMarquee 是一款基于 jQuery 的无缝滚动插件,类似于 HTML 的 marquee 标签,但比 marquee 更强大.它可以应用于任何 Web 元素,包括文字.图像.表格.表单等元素 ...

  9. eclipse中查看安装的所有插件,并选择性的将其卸载

    牢骚:前一段时间安装了一个将错误日志回传到邮箱的eclipse插件,安装此插件对捕获debug虽然很方便,但是也带了我不小的困扰,比如:eclipse加载速度慢.打开速度慢,有时还会引起一些异常,这不 ...

  10. python json 解析

    Encode过程,是把python对象转换成json对象的一个过程,常用的两个函数是dumps和dump函数. dic1 = {'type':'dic1','username':'loleina',' ...