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 ...
随机推荐
- python学习之面向对象(上)
定义了一个Animal类,该类包括了构造函数,私有方法,公有方法,静态方法,属性的方问等 双下划线"__"组成了私有成员的定义约束,其它情况则为公有成员 #_metaclass_= ...
- WPF XMAL获取元素的父元素,子元素
/// 获得指定元素的父元素 /// </summary> /// <typeparam name="T">指定页面元素</typeparam> ...
- asp.net core microservices 架构之 分布式自动计算(二)
一 简介 上一篇介绍了zookeeper如何进行分布式协调,这次主要讲解quartz使用zookeeper进行分布式计算,因为上一篇只是讲解原理,而这次实际使用, ...
- sapconnector.dll download
Sapnoc30Demo_Yan.rar Sap30Server.rar SAPNCO_Sample_Code.zip sapcon3.0_X64.rar DbEntry.Net.4.1.Setup. ...
- DbEntry 默认 主键ID为long
DbEntry 默认 主键ID为long,如果自己表中的主键ID为int,可以通过以下方式修改: public class Company :DbObjectModel<Company,int& ...
- 【模板】NOIP模板汇总
图论 数据结构 数学 其他: 洛谷模板:a,b两个字符串,求b串在a串中出现的位置 #include<iostream> #include<cstdio> #include&l ...
- 转:django中session的实现机制
转:www.jianshu.com 要理解session,首先要搞清楚cookie的概念.由于http是无状态的,服务器不能记住用户的信息状态,因此若由同一个客户端发起的多条请求,服务器不能辨别这些请 ...
- bzoj 4881 [Lydsy1705月赛]线段游戏
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4881 1.当一块相互交织的线段中有3个或以上两两相交的那种线段时,无解. 这就是最长下降子序 ...
- 实现对sqlite数据库增删改查
package com.example.db.dao;import java.util.ArrayList;import java.util.List;import android.content.C ...
- linux下PS1命令提示符设置
linux下PS1命令提示符设置 在此文件最后一行添加:vim /etc/profileexport PS1='[\u@\h \W]\$ ' #这里必须用单引号. \d :代表日期,格式为 ...