LeetCode Sum of Square Numbers
原题链接在这里: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的更多相关文章
- [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 ...
- 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 ...
- 【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 平方数之和
Given a non-negative integer c, your task is to decide whether there're two integers a and b such th ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- LeetCode算法题-Sum of Square Numbers(Java实现)
这是悦乐书的第276次更新,第292篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第144题(顺位题号是633).给定一个非负整数c,判断是否存在两个整数a和b,使得a的 ...
- #Leetcode# 633. Sum of Square Numbers
https://leetcode.com/problems/sum-of-square-numbers/ Given a non-negative integer c, your task is to ...
- C#LeetCode刷题之#633-平方数之和( Sum of Square Numbers)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3885 访问. 给定一个非负整数 c ,你要判断是否存在两个整数 ...
- 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...
随机推荐
- 47求1+2+3+...+n
题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 用递归 public class Soluti ...
- iOS 什么是函数式编程
前言:当前只做理解性的常规背书,根据不断深入学习会不断丰富解读内容,欢迎评论提意见 函数式编程:Functional Programming 1 基本解释: 函数式编程 是一种思维模式,一种编程思想, ...
- UIPageControl修改圆点大小,根据View大小自适应
遇到了个基本的控件问题,当设置UIPageControl的frame很小时,上面的小圆点会忽视view的frame而将圆点显示到控件外面. 但是如果想要设置小一点的圆点,或改变圆点间的间距,从而实现自 ...
- 使用CoreData存储数据
- (void)viewDidLoad { [super viewDidLoad]; //获取模型文件的路径 NSString *path=[[NSBundle mainBundle]pathForR ...
- SpringBoot整合集成redis
Redis安装:https://www.cnblogs.com/zwcry/p/9505949.html 1.pom.xml <project xmlns="http://maven. ...
- centos磁盘安装与磁盘分区方案
概述 关于centos分区的相关知识 无论怎么分区并不会影响系统文件目录的布局,如果只分/和swap这两个区 没有 usr , var , etc 等分区,在安装好后文件根目录里依然会有usr , v ...
- QtGstreamer 编译
一 安装依赖项 1 安装cmake hdhuang@hdh-UBT:~/gstreamer/qt-gstreamer-0.10.2/build$ sudo apt-get install cmake ...
- RAID 工作模式
RAID 工作模式 RAID磁盘阵列 优点: 1.提高传输速率:RAID通过在多个磁盘上同时存储和读取数据来大幅提高存储系统的数据吞吐量. 2.RAID可以达到单个磁盘驱动器几倍.几十倍甚至上百倍的速 ...
- myisam表修复
数据库myisam引擎表损坏修复步骤: 1.进入到表目录文件下 # myisamchk -of comments.MYI 2. # myisamchk -r comments.MYI 3. # ...
- Linux下安装lrzsz上传下载工具
使用yum安装 为什么要使用yum安装? 答:安装十分方便,几乎不需要别的操作,只需要一个yum命令就可以完成所有的安装过程. yum -y install lrzsz 要有网络才行 输入命令:rz ...