给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2: 输入: 3 输出: False 方法一: class Solution { public: bool judgeSquareSum(int c) { for(int i = sqrt(c); i >= 0; i--) { int j = sqrt(c - i * i); if(i * i + j * j ==…
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 这道题让我们求一个数是否能由平方数之和组成,刚开始博主没仔细看题,没有看…
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 这道题让我们求一个数是否能由平方数之和组成,刚开始博主没仔细看题,没有看…
题目: 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 分析: 给定一个非负整数c ,你要判断是否存在两个整数a和b,使…
题意:给定一个非负整数c,确定是否存在a和b使得a*a+b*b=c. class Solution { typedef long long LL; public: bool judgeSquareSum(int c) { LL head = 0; LL tail = (LL)(sqrt(c)); while(head <= tail){ LL sum = head * head + tail * tail; if(sum == (LL)c) return true; else if(sum >…
上篇文章中一道数学问题 - 自除数,今天我们接着分析 LeetCode 中的另一道数学题吧~ 今天要给大家分析的面试题是 LeetCode 上第 633 号问题, Leetcode 633 - 平方数之和 https://leetcode.com/problems/sum-of-square-numbers/ 题目描述 给定一个非负整数 c ,你要判断是否存在两个整数 a和 b,使得 \(a^2 + b^2 = c\). 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2…
problem 633. Sum of Square Numbers 题意: solution1: 可以从c的平方根,注意即使c不是平方数,也会返回一个整型数.然后我们判断如果 i*i 等于c,说明c就是个平方数,只要再凑个0,就是两个平方数之和,返回 true:如果不等于的话,那么算出差值 c - i*i,如果这个差值也是平方数的话,返回 true.遍历结束后返回 false, class Solution { public: bool judgeSquareSum(int c) { ; --…
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - Leetcode 633. 平方数之和 - 题解 Leetcode 633 - Sum of square number 在线提交: https://leetcode.com/problems/sum-of-square-numbers/ 题目描述 给定一个非负整数 c ,你要判断是否存在两个整数 a…
633. 平方数之和 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2: 输入: 3 输出: False class Solution { public boolean judgeSquareSum(int c) { if(c<=1) return true; int l = 0; int r = (int)Math.pow(c,0.5); while(l<=…
[JavaScript]Leetcode每日一题-平方数之和 [题目描述] 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c . 示例1: 输入:c = 5 输出:true 解释:1 * 1 + 2 * 2 = 5 示例2: 输入:c = 3 输出:false 示例3: 输入:c = 4 输出:true 示例4: 输入:c = 2 输出:true 示例5: 输入:c = 1 输出:true 提示: 0 <= c <= 231 - 1 [分析] 暴力 暴…