【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 ...
随机推荐
- 分布式系统原理之cap理论
1.1 CAP理论的含义 Cap理论表示在分布式系统中一致性(C).可用性(A)和分区容错性(P)最多只能同时满足两个. 一致性:客户端更新数据成功后,在任意时刻,在系统任意对外提供服务的节点,读取 ...
- 【Tomcat】安装Tomcat服务器&Tomcat的目录结构
创建时间:6.14 一.安装Tomcat服务器 Tomcat下载ver8的,现在用的多 下载并解压 配置环境变量:(切记!!不然startup那步会闪退) 1.新建系统环境变量: (1)进入根目录,复 ...
- Codeforces J. Sagheer and Nubian Market(二分枚举)
题目描述: Sagheer and Nubian Market time limit per test 2 seconds memory limit per test 256 megabytes in ...
- Flutter gradle采坑
前些日子google推出Flutter1.9版本支持web果断升级 在运行flutter时发现错误,错误提示为 Launching lib/main.dart on Android SDK built ...
- Python并发编程内容回顾
Python并发编程内容回顾 并发编程小结 目录 • 一.到底什么是线程?什么是进程? • 二.Python多线程情况下: • 三.Python多进程的情况下: • 四.为什么有这把GIL锁? • 五 ...
- VIJOS-P1066 弱弱的战壕
JDOJ 1247: VIJOS-P1066 弱弱的战壕 题目传送门 Description 永恒和mx正在玩一个即时战略游戏,名字嘛~~~~~~恕本人记性不好,忘了--b. mx在他的基地附近建立了 ...
- 微信小程序 组件 全局样式
配置如下设置 /** * 组件的一些选项 */ options: { addGlobalClass: true },
- 【转】linux 下清空或删除大文件的一些方法
原文:https://linux.cn/article-8024-1.html 在 Linux 终端下处理文件时,有时我们想直接清空文件的内容但又不必使用任何 Linux 命令行编辑器 去打开这些文件 ...
- 【java异常】 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.emptech.db.demo.mapper.master.MOmQuotaTBMapper.findOmQuotaTB
<mapper namespace="com.emptech.db.demo.mapper.master.MOmQuotaTBMapper"> public inter ...
- 网络协议 4 - 交换机与 VLAN:拓扑结构
上一次,我们通过宿舍联网打魔兽的需求,认识了如何通过物理层和链路层组建一个宿舍局域网.今天,让我们切换到稍微复杂点的场景,办公室. 在这个场景里,就不像在宿舍那样,搞几根网线,拉一拉,扯一扯就 ...