原题链接在这里: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.7(64bit)---安装mysql5.5、jdk、tomcat

    ********mysql******** ------------------------------------------------------------------------------ ...

  2. arm64的适配问题,这次真醉了

    写过tableView的童鞋都知道,有必须的两个代理方法要实现,还有几个选择实现的. 必须实现的代理方法: ~设置行数 - (NSInteger)tableView:(UITableView *)ta ...

  3. four application:geocoder widget

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. BEM(一种 CSS 命名规则)

    一. 什么是 BEM BEM的意思就是块(block).元素(element).修饰符(modifier),是由 Yandex 团队提出的一种前端命名方法论. 这种巧妙的命名方法让你的 CSS 类对其 ...

  5. CentOS 5下freeswitch中集成使用ekho实现TTS功能二

    三:以上Festival安装完成以后回到ekho安装目录: 执行./configure --enable-festival 前 更改configure 1:替换 #AC_DEFINE(ENABLE_F ...

  6. AngularJs 的一则错误 [$INJECTOR:MODULERR]

    Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to: Error: [$injector:modu ...

  7. Linux服务器注意事项

    1.在Linux新建一个tomcat目录,执行里面的文件运行的时候 会出现权限不足的提示?解决办法:这是因为新建的文件夹,对于可执行脚本,必须先授权,进入bin目录后,执行命令  chmod 764 ...

  8. centos7安装MPlyaer

    最近更换了centos7系统,对新系统的操作不是太熟悉.大神轻喷.昨晚突然想要下个电影看看,结果发现系统自带的播放器支持的视频格式有限,google查了一下,他们推荐使用MPlayer.于是经过一通g ...

  9. Java执行过程

    Java的运行原理 在Java中引入了虚拟机的概念,即在机器和编译程序之间加入了一层抽象的虚拟的机器.这台虚拟的机器在任何平台上都提供给编译程序一个的共同的接口.编译程序只需要面向虚拟机,生成虚拟机能 ...

  10. 二叉查找树--java

    package com.test.tree; public class BinarySearchTree<T extends Comparable<? super T>> { ...