[抄题]:

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]:

[思维问题]:

以为同向型,其实对撞型,关键是能省空间 面试官喜欢

[一句话思路]:

对撞型,关键是能省空间 面试官喜欢

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 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 是否由两个完全平方数构成的更多相关文章

  1. 【Leetcode_easy】633. Sum of Square Numbers

    problem 633. Sum of Square Numbers 题意: solution1: 可以从c的平方根,注意即使c不是平方数,也会返回一个整型数.然后我们判断如果 i*i 等于c,说明c ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  6. 【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. ...

  7. 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...

  8. #Leetcode# 633. Sum of Square Numbers

    https://leetcode.com/problems/sum-of-square-numbers/ Given a non-negative integer c, your task is to ...

  9. 633. Sum of Square Numbers

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

随机推荐

  1. Friendly ARM linux交叉编译问题解决

    ARM-LINUX-GCC 安装参考:(笔记)Ubuntu下安装arm-linux-gcc-4.4.3.tar.gz (交叉编译环境) 然而安装完成之后运行 arm-linux-gcc -v (注意g ...

  2. 软件安全攻防--缓冲区溢出和shellcode

    缓冲区溢出漏洞实验报告 实验楼中有seed缓冲区溢出漏洞实验,实验内容与课本中要求的实验基本一致,便利用实验楼提供好的现成实验环境来完成这次的实践内容. 一.实验简介 缓冲区溢出是指程序试图向缓冲区写 ...

  3. 51nod 1012 最小公倍数LCM

    输入2个正整数A,B,求A与B的最小公倍数. 收起   输入 2个数A,B,中间用空格隔开.(1<= A,B <= 10^9) 输出 输出A与B的最小公倍数. 输入样例 30 105 输出 ...

  4. Switch能否使用String做参数

    在Java语言中Swith可以使用参数类型有:Only convertible int values, strings or enum variables are permitted 可以自动转换为整 ...

  5. 使用pinyin4j汉字转pinyin

    引入maven依赖<dependencies> <dependency> <groupId>com.belerweb</groupId> <art ...

  6. bzoj 2119 股市的预测 —— 枚举关键点+后缀数组

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2119 思路就是对于这个形如 ABA 的串,枚举 A 的长度,并按照长度分出几块,找到一些关键 ...

  7. loj 572 Misaka Network 与求和 —— min_25筛

    题目:https://loj.ac/problem/572 推式子:https://www.cnblogs.com/cjoieryl/p/10150718.html 又学习了一下杜教筛hh: 原来 u ...

  8. C#去除数组空格

    static void Main(string[] args) { "}; Console.WriteLine("输出带有空字符串的数组:"); foreach (str ...

  9. C# 判断程序是否已在运行

    方法一: Process[] processes = rocess.GetProcessesByName("ConDemo"); ) { MessageBox.Show(" ...

  10. 【转】java接口的性能测试

    这周尝试了一把性能测试,之前都是测试网站的性能测试,java接口的性能测试还是头一次,学到了很多,特此分享一下. 主要用到了两个性能测试工具,一个是jmeter,一个是LoadRunner. 使用jm ...