原题链接在这里:https://leetcode.com/problems/sum-of-square-numbers/description/

题目:

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

题解:

在[0, (int)Math.sqrt(c)]区间内用two points夹比.

Time Complexity: O(sqrt(c)). Space: O(1).

AC Java:

 class Solution {
public boolean judgeSquareSum(int c) {
if(c < 0){
return false;
} int l = 0;
int r = (int)Math.sqrt(c);
while(l<=r){
int cur = l*l + r*r;
if(cur == c){
return true;
}else if(cur < c){
l++;
}else if(cur > c){
r--;
}
}
return false;
}
}

LeetCode Sum of Square Numbers的更多相关文章

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

  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. 【Leetcode_easy】633. Sum of Square Numbers

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

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

  5. 【LeetCode】633. Sum of Square Numbers

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

  6. LeetCode算法题-Sum of Square Numbers(Java实现)

    这是悦乐书的第276次更新,第292篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第144题(顺位题号是633).给定一个非负整数c,判断是否存在两个整数a和b,使得a的 ...

  7. #Leetcode# 633. Sum of Square Numbers

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

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

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

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

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

随机推荐

  1. Centos6.6安装mysql记录

    一.环境介绍: 系统:Cerntos6.6 Mysql版本:mysql-5.6.34 二.安装操作: 1.卸载旧版本: rpm -qa |grep mysql mysql-server-5.1.73- ...

  2. oracle数据向历史表数据迁移————procedure

    create or replace procedure remove_refund_his_pro isbegin declare cursor refund_query_cur is select ...

  3. this对象解析

    this在js中有着非常广泛的应用,但其所指的对象也常常让人摸不着头脑,简而言之: this指的就是调用函数的对象,最常见的莫过以下几种 1.直接使用函数,则为window对象 function a( ...

  4. Unity Json 之三

    今天在网上看到一个simplejson,直接调用这两个API就可以了,简单易用 string jsonstr = SimpleJson.SimpleJson.SerializeObject(json) ...

  5. Java 封装、继承、多态

    Java中使用 extends 关键字 进行父类继承 在初始化子类时,子类会自动执行父类的构造方法, 如果子类的构造方法中没有显示调用父类的构造方法, 则系统会默认调用父类无参的构造方法 super( ...

  6. codeforces 439D 思维

    题意:两个数组a,b,每次操作可将其中一个数组的一个数字加1或减1,求最小操作次数使得a数组的最小值大于等于b数组的最大值. 思路: 解法一:考虑最终状态,假设a为数组a中最小的数,b为数组b中最大的 ...

  7. hql学习记录

    ` String hql = "from SysUser o join o.set where owner_id = :newName"; Query query = this.g ...

  8. java resources 红叉 An error occurred while filtering resources

    用eclipse创建了一个Spring mvc的Maven项目,在项目上有个叉叉,通过Window -> Show View -> Markers中看到错误原因 An error occu ...

  9. HDU 2419 Boring Game(并查集+map)

    感觉做得有点复杂了,但是AC了还是...爽... 题意:给你n个点每个点有一个价值,接下来有m条边,然后是q个操作,每个操作有三种情况: F X K:寻找与X点直接或间接相连的不小于价值K的最小价值, ...

  10. C中的指针和字符串

    程序 #include<stdio.h>int main(void){ char *mesg="Don't be a fool!"; char *copy; copy= ...