[leetcode]633. Sum of Square Numbers
来源:互联网 发布:七局网络教育 编辑:程序博客网 时间:2024/06/04 19:24
题目链接:https://leetcode.com/problems/sum-of-square-numbers/#/description
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: 5Output: TrueExplanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: 3Output: False
class Solution {public: bool judgeSquareSum(int c) { for(long a=0;a*a<=c;a++) { int b=c-(int)(a*a); if(binary_search(0,b,b)) return true; } return false; } bool binary_search(long s,long e,int n) { if(s>e) return false; long mid=s+(e-s)/2; if(mid*mid==n) return true; else if(mid*mid>n) return binary_search(s,mid-1,n); else if(mid*mid<n) return binary_search(mid+1,e,n); return false; }};
class Solution {public: bool judgeSquareSum(int c) { for(long long a=0;a*a<=c;a++) { int b=c-(long long)(a*a); if(binary_search(0,b,b)) return true; } return false; }private: bool binary_search(long long left,long long right,long long n) { while(left<=right) { long long mid=left+(right-left)/2; if(mid*mid==n) return true; else if(mid*mid>n) right=mid-1; else left=mid+1; } return false; }};
阅读全文
0 0
- [leetcode]633. Sum of Square Numbers
- [LeetCode]633. Sum of Square Numbers
- LeetCode 633. Sum of Square Numbers
- [leetcode]633. Sum of Square Numbers
- leetcode 633. Sum of Square Numbers
- LeetCode 633. Sum of Square Numbers
- leetcode 633. Sum of Square Numbers
- leetcode 633. Sum of Square Numbers
- [LeetCode]633. Sum of Square Numbers
- leetcode[Sum of Square Numbers]
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers。
- 【leetcode】633. Sum of Square Numbers(Python & C++)
- leetcode 633. Sum of Square Numbers 二分查找
- 湖南省第九届大学生计算机程序设计竞赛 G
- Java写一个不可变类(如String)
- yolo2-vs2015-CUDA8.0-OPENCV3.2_DEMO
- “玲珑杯”ACM比赛 Round #18 C(dp)
- 第三方登录
- [leetcode]633. Sum of Square Numbers
- 为什么要将基类的析构函数声明为虚函数
- xml/schema/wsdl
- MeshLab配置和编译
- JS窗体弹出问题输入答案_prompt
- phpStudy域名配置【目录显示】
- mysql基本操作命令_武汉app开发_沃航科技
- 8. String to Integer (atoi)
- 晨魅--MySQL常用命令