【LeetCode】633. Sum of Square Numbers
Difficulty: Easy
More:【目录】LeetCode Java实现
Description
https://leetcode.com/problems/sum-of-square-numbers/submissions/
Given a non-negative integer c, your task is to decide whether there're two integers aand b such that a2 + b2 = c.
Example 1:
Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: 3
Output: False
Intuition
Using two pointers: One starts from the beginning, the other starts from the end.
It's very similar to Two Sum II - Input array is sorted
Solution
public boolean judgeSquareSum(int c) {
int i=0, j=(int)Math.sqrt(c);
while(i<=j){
int ans=i*i+j*j;
if(ans<c){
i++;
}else if(ans>c){
j--;
}else{
return true;
}
}
return false;
}
Complexity
Time complexity : O(n)
Space complexity : O(1)
What I've learned
1.The use of two pointers.
More:【目录】LeetCode Java实现
【LeetCode】633. Sum of Square Numbers的更多相关文章
- 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...
- 【leetcode】633. Sum of Square Numbers(two-sum 变形)
Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c. ...
- 【Leetcode_easy】633. Sum of Square Numbers
problem 633. Sum of Square Numbers 题意: solution1: 可以从c的平方根,注意即使c不是平方数,也会返回一个整型数.然后我们判断如果 i*i 等于c,说明c ...
- 【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找规律 日期 题目地址:https://lee ...
- 【leetcode】985. Sum of Even Numbers After Queries
题目如下: We have an array A of integers, and an array queries of queries. For the i-th query val = quer ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
- 【LeetCode】1022. Sum of Root To Leaf Binary Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetco ...
随机推荐
- word2vector(含code)
Word2Vec其实就是通过学习文本来用词向量的方式表征词的语义信息,即通过一个嵌入空间使得语义上相似的单词在该空间内距离很近. Embedding其实就是一个映射,将单词从原先所属的空间映射到新的多 ...
- Removing Stones(2019年牛客多校第三场G+启发式分治)
目录 题目链接 题意 思路 代码 题目链接 传送门 题意 初始时有\(n\)堆石子,每堆石子的石子个数为\(a_i\),然后进行游戏. 游戏规则为你可以选择任意两堆石子,然后从这两堆中移除一个石子,最 ...
- django 自定义分页,网址存储缓存,CBV
1. 通过切片控制分页 自定义分页: from django.shortcuts import render # Create your views here. from app01.models i ...
- Vue中美元$符号的意思与vue2.0中的$router 和 $route的区别
vue的实例属性和方法 除了数据属性,Vue 实例还暴露了一些有用的实例属性与方法.它们都有前缀 $,以便与用户定义的属性区分开来.例如: var data = { a: 1 } var vm = n ...
- Alibaba Cloud SDK for Java,知识点
资料 网址 Alibaba Cloud SDK for Java https://help.aliyun.com/document_detail/52740.html?spm=a2c4g.111742 ...
- 自动化测试框架:jmeter + maven+ jenkins
原理:jenkins驱动maven执行,maven驱动jmeter执行 前提条件:windows安装了jmeter.maven.tomcat.jenkins 安装方法参考汇总目录中对应的博文:http ...
- 【Excel】IF函数
判断条件: 一版判断:1>2大于 1<2小于 1=2等于 1<>2不等于 1>=2 1<=2 交集:AND() 并集:OR() 多条件:以后补
- Pandas | 01 数据结构
Pandas的三种数据结构: 系列(Series) 数据帧(DataFrame) 面板(Panel) 这些数据结构,构建在Numpy数组之上,这意味着它们很快 维数和描述 考虑这些数据结构的最好方法是 ...
- 洛谷 P1965 转圈游戏
洛谷 P1965 转圈游戏 传送门 思路 每一轮第 0 号位置上的小伙伴顺时针走到第 m 号位置,第 1 号位置小伙伴走到第 m+1 号位置,--,依此类推,第n − m号位置上的小伙伴走到第 0 号 ...
- CSRF 跨站
目录 CSRF 跨站请求伪造 解决跨站伪造问题: csrf 相关的装饰器: csrf.js文件: CSRF 跨站请求伪造 CSRF全称为Cross-site request forgery,也被称为: ...