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 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++)的更多相关文章
- [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 ...
- [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 ...
- Leetcode633.Sum of Square Numbers平方数之和
给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2: 输入: 3 ...
- #Leetcode# 633. Sum of Square Numbers
https://leetcode.com/problems/sum-of-square-numbers/ Given a non-negative integer c, your task is to ...
- 【Leetcode_easy】633. Sum of Square Numbers
problem 633. Sum of Square Numbers 题意: solution1: 可以从c的平方根,注意即使c不是平方数,也会返回一个整型数.然后我们判断如果 i*i 等于c,说明c ...
- 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...
- 【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. ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 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 ...
随机推荐
- October 15th 2017 Week 42nd Sunday
Excellence is a continuous process and not an accident. 卓越是一个持续的过程而不是一个偶然事件. It is said that ten tho ...
- css常见的概念
padding-top:10px;是指容器内的内容距离容器的顶部有10个像素,是包含在容器内的: margin-top:10px;是指容器本身的顶部距离其他容器有10个像素,不包含在容器内: top: ...
- 给SVN设置代理
XP系统在C:\Documents and Settings\Administrator\Application Data\Subversion目录下 win7及以上系统在C:\Users\admin ...
- Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)
在 linux服务器上运行代码报错: Python3中遇到UnicodeEncodeError: ‘ascii’ codec can’t encode characters in ordinal no ...
- Jmeter遇到线程链接被重置(Connection reset by peer: socket write error)的解决方法
做性能测试的时候遇到一个很奇怪的问题,多线程的计划,有一个线程第一次能跑过,第二次确跑不过,单独跑这个线程跑多少次都没有问题,把思考时间改短也没有问题,唯独出现在特定的状态下,特定状态是啥,也不得而知 ...
- Spark2.3文档翻阅的一点简略笔记(WaterMarking)
写本文原因是之前已经将官网文档阅读过几遍,但是后来工作接触spark机会较少所以没有跟进新特性,利用周末一点闲暇时间粗略阅读一篇,将自己之前遇见过的问题解决过的问题印象不深刻的问题做一下记录. 1关于 ...
- 【JavaScript】read_line()、print()实现输入输出
/*输入 输入的第一行为一个正整数T,表示有T组测试数据.随后的T行中,每行为一组测试数据. 每组测试数据包含由3个正整数构成,分别为N.M和a,其中1<=N, M, a <=10^9. ...
- python之生成随机测验试卷
自己又开始懒散的态度生活,所以几乎有两个月没有更博了. 项目:美国各州首府地理考试,为防止作弊,35份试卷,50道题随机次序,生成独一无二的试卷. 基本想法: 1.将各州首府的地方和首府写入列表,以K ...
- 2、Pyspider使用入门
1.接上一篇,在webui页面,点击右侧[Create]按钮,创建爬虫任务 2.输入[Project Name],[Start Urls]为爬取的起始地址,可以先不输入,点击[Create]进入: 3 ...
- 客户端本地存储的比较及使用window.name数据传输
一:cookie: 1. 什么是cookie? Cookie是在客户端用于存储会话信息的,用户请求页面在web服务器与浏览器之间传递.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cooki ...