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. hdu 5090 Game with Pearls (额,, 想法题吧 / 二分图最大匹配也可做)

    题意: 给你N个数,a1,,,,an.代表第i个管子里有ai个珍珠. 规定只能往每根管里增加k的倍数个珍珠. 如果存在一套操作,操作完毕后可以得到1~N的一个排列,则Jerry赢,否则Tom赢. 问谁 ...

  2. 面试官:熟悉JS中的new吗?能手写实现吗?

    目录 1 new 运算符简介 2 new 究竟干了什么事 3 模拟实现 new 运算符 4 补充 预备知识: 了解原型和原型链 了解this绑定 1 new 运算符简介 MDN文档:new 运算符创建 ...

  3. swagger3.0(springboot)消除basic-error-controller

    1.新建springboot项目,可以通过https://start.spring.io/快速生成springboot项目. 2.引入jar依赖: <dependency> <gro ...

  4. python环境搭建、pycharm安装

    一.      实验目标 (1)  Python环境搭建 (2)  会pycharm安装和使用 (3)  了解python程序设计流程 二.      实验内容 1.勾选Add Python 3.7 ...

  5. 一、spring 环境搭建

    一.springtoolSuite4下载 1.概述 Spring Tools 4 是适用于您最喜欢的编码环境的下一代 Spring 工具.它主要从头开始重建,为开发基于 Spring 的企业应用程序提 ...

  6. fork()和vfork()的区别,signal函数用法,exec()系列函数的用法小结

    一:fork()和vfork()的区别:    fork()函数可以创建子进程,有两个返回值,即调用一次返回两个值,一个是父进程调用fork()后的返回值,该返回值是刚刚创建的子进程的ID;另一个是子 ...

  7. 如何利用SimpleNVR建立全天候远程视频监控系统

    随着社会经济的发展,5G.AI.云计算.大数据.物联网等新兴技术迭代更新的驱动下,传统的安防监控早已无法满足我们的需求.那么我们如何建立全天候远程视频监控系统来替代传统监控呢?如何进一步优化城市管理. ...

  8. Mysql教程:(六)修改语句、、删除语句、字符查询like

    1.修改语句 update 表名 set  where 条件 mysql> update student set birth=1988,department='中文系' where id=901 ...

  9. celery kill task

    from celery.task.control import revokerevoke(task_id, terminate=True) https://stackoverflow.com/ques ...

  10. Modelsim仿真新手入门最详细教程

    2021年11月15日 00 安装包/版本 我是提前在网上下好的(但这一点也给我的实验造成了"麻烦"),用的是Modelsim SE-64 2020.4版本的,学校实验室的似乎不同 ...