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

  只要是two_sum 变形 都可以考虑用hash_set来做。

  

class Solution {
public:
bool judgeSquareSum(int c) {
//这个和两数之和很想 感觉是两数之和的变形
//先求一组两平方和之数 如果这两平方和数都存在的话 就返回true
//用hash_set 存平方和数
unordered_set<long> res;
long i=0;
bool flag=false;
while(i*i<=c)
{
res.insert(i*i);
i++;
}
for(auto a:res)
{
if(res.count(c-a))
{
flag=true;
break;
}
}
return flag;
}
};
  solution2:
class Solution {
public:
bool judgeSquareSum(int c) {
//之间判断 用sqrt以及int 数据类型的特性
for(int i=0;i<=sqrt(c);i++)
{
int t=sqrt(c-i*i);
if(t*t==c-i*i) return true;
}
return false;
}
};
  

【leetcode】633. Sum of Square Numbers(two-sum 变形)的更多相关文章

  1. 【Leetcode_easy】633. Sum of Square Numbers

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

  2. lintcode: Check Sum of Square Numbers

    Check Sum of Square Numbers Given a integer c, your task is to decide whether there're two integers ...

  3. C#LeetCode刷题之#633-平方数之和( Sum of Square Numbers)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3885 访问. 给定一个非负整数 c ,你要判断是否存在两个整数 ...

  4. 【LeetCode】633. Sum of Square Numbers

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

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

  6. #Leetcode# 633. Sum of Square Numbers

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

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

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

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

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

随机推荐

  1. ACM 数据读写/对拍

    freopen()函数在ACM中的使用 - cfzjxz的专栏 - 博客频道 - CSDN.NET 在做acm题目的过程中,我们需要在本地机器上调试.调试过程中,如果输入数据少还可以接受,但如果输入数 ...

  2. SpringBoot 中发布ApplicationEventPublisher,监听ApplicationEvent 异步操作

    有这么一个业务场景:当用户注册后,发送邮件到其邮箱提示用户进行账号激活,且注册成功的同时需要赠送新人用户体验卡券. 业务有了,那么问题也就来了. What? 问题....问题?我听说你有问题? 来拔刀 ...

  3. ant生成jmeter测试报告没有数据【已解决】

    1.如下图,在配置build时,到网上找配置文件,配置样式表文件名经常是jmeter-results-detail-report_21.xsl, 3.但是在其他版本可能被不是这个文件名,在jmeter ...

  4. 基于ABP开发框架的技术点分析和项目快速开发实现

    在我们开发各种项目应用的时候,往往都是基于一定框架进行,同时配合专用的代码生成工具,都是为了快速按照固定模式开发项目,事半功倍,本篇随笔对基于ABP开发框架的技术点进行分析和ABP框架项目快速开发实现 ...

  5. 【JAVA】笔记(3)---封装;如何选择声明静态变量还是实例变量;如何选择声明静态方法还是实例方法;静态代码块与实例代码块的执行顺序与用途;

    封装: 1.目的:保证对象中的实例变量无法随意修改/访问,只能通过我们自己设定的入口,出口(set / get)来间接操作:屏蔽类中复杂的结构,使我们程序员在主方法中关联对象写代码时,思路/代码格式更 ...

  6. [bzoj1005]明明的烦恼

    根据purfer序列的原理,每一个purfer序列都一一对应了一棵树,每一个点在purfer序列中出现的次数就是它的度数,那么直接用组合数去计算即可,注意要加高精度 1 #include<cst ...

  7. [noi1994]海盗

    令$a_{i,j}(j\le i)$表示第i个人的方案中给第j个人$a_{i,j}$的钱,有以下性质: 1.如果第j个人一定同意(否则就会死)第i个人的方案,那么$a_{i,j}=0$(容易发现一定同 ...

  8. JAVA基础----面向对象复习和IDEA的安装和使用

    1.使用集成开发工具eclipse 1.1.java的集成开发工具很多,包括:eclipse.Intellij IDEA.netbeans..... eclipse: IBM开发的.eclipse翻译 ...

  9. 『学了就忘』Linux用户管理 — 51、用户管理相关命令

    目录 1.添加用户(useradd命令) 2.设定密码(passwd命令) 3.用户信息修改(usermod命令) 4.删除用户(userdel命令) 5.切换用户身份(su命令) 1.添加用户(us ...

  10. 获取客户端Mac地址

    近期有个需求,需要获取客户端Mac地址作为白名单验证的依据.使用.net,B/S架构.先百度找了一些获取mac地址的方法, using System; using System.Collections ...