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 that a2 + b2 = c.
Example 1:
Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: 3
Output: False
[暴力解法]:
时间分析:
空间分析:n^2
[优化后]:
时间分析:
空间分析:n
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为同向型,其实对撞型,关键是能省空间 面试官喜欢
[一句话思路]:
对撞型,关键是能省空间 面试官喜欢
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- while (i <= (int)Math.sqrt(c) && j >= 0) 有没有等号,还是需要写完了试试
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
对撞型省时间
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
class Solution {
public boolean judgeSquareSum(int c) {
//cc
if (c < 0) return false; //ini
int i = 0, j = (int)Math.sqrt(c); //while loop
while (i <= (int)Math.sqrt(c) && j >= 0) {
if (i * i + j * j < c) i++;
else if (i * i + j * j > c) j--;
else return true;
} return false;
}
}
[代码风格] :
633. Sum of Square Numbers 是否由两个完全平方数构成的更多相关文章
- 【Leetcode_easy】633. Sum of Square Numbers
problem 633. Sum of Square Numbers 题意: solution1: 可以从c的平方根,注意即使c不是平方数,也会返回一个整型数.然后我们判断如果 i*i 等于c,说明c ...
- 633. Sum of Square Numbers【Easy】【双指针-是否存在两个数的平方和等于给定目标值】
Given a non-negative integer c, your task is to decide whether there're two integers a and bsuch tha ...
- [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平方数之和 (C++)
题目: Given a non-negative integer c, your task is to decide whether there're two integers a and b suc ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【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】633. Sum of Square Numbers 解题报告(python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...
- #Leetcode# 633. Sum of Square Numbers
https://leetcode.com/problems/sum-of-square-numbers/ Given a non-negative integer c, your task is to ...
- 633. Sum of Square Numbers
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
随机推荐
- Friendly ARM linux交叉编译问题解决
ARM-LINUX-GCC 安装参考:(笔记)Ubuntu下安装arm-linux-gcc-4.4.3.tar.gz (交叉编译环境) 然而安装完成之后运行 arm-linux-gcc -v (注意g ...
- POJ 1265 Area (pick定理)
题目大意:已知机器人行走步数及每一步的坐标变化量,求机器人所走路径围成的多边形的面积.多边形边上和内部的点的数量. 思路:叉积求面积,pick定理求点. pick定理:面积=内部点数+边上点数/2-1 ...
- 在linux中使用shell来分析统计日志中的信息
在运维工作中,要经常分析后台系统的日志,通过抓取日志中的关键字信息,对抓取结果进行统计,从而为监控结果提供基础数据.下面的shell演示了如何从大量的日志中取得想要的统计结果.其中展示了各种有趣的命令 ...
- 为什么要编写轻量级的View Controller??
1.作为iOS项目中最大的文件,ViewControllers中的代码复用率几乎是最低的2.重量级的View COntroller加大了测试的复杂度.所以关注ViewController的瘦身,把业务 ...
- ubuntn 配置webpy nginx
webpy环境搭建 在开始webpy搭建之前,有必要熟悉一下什么是fastcgi,因为搭建环境时都是使用这个模式去运行webpy程序的,具体的fastcgi描述可以参考各种百科:fastcgi协议官网 ...
- 【转】C#中的线程 入门
Keywords:C# 线程 Source:http://www.albahari.com/threading/ Author: Joe Albahari Translator: Swanky Wu ...
- maxsigma
10^8 768 10^7 448 10^6 240 10^5 128
- Android蓝牙UUID简要
UUID是"Universally Unique Identifier"的简称,通用唯一识别码的意思.对于蓝牙设备,每个服务都有通用.独立.唯一的UUID与之对应.也就是说,在同一 ...
- 容器中跨主机的网络方案-Calico
容器中的网络是建立docker集群的重要内容. 本文将介绍如何用Calico实现容器的多节点互通. Calico的组件结构如下: Calico通过etcd同步Bridge的信息,各个Docker no ...
- 低成本FPGA中实现动态相位调整
在FPGA中,动态相位调整(DPA)主要是实现LVDS接口接收时对时钟和数据通道的相位补偿,以达到正确接收的目的.ALTERA的高端FPGA,如STRATIX(r) 系列中自带有DPA电路,但低端的F ...