lintcode: Check Sum of Square Numbers
Check Sum of Square Numbers
Given a integer c
, your task is to decide whether there're two integers a and b such that a^2 + b^2 = c
.
Given n = 5
Return true
// 1 * 1 + 2 * 2 = 5
Given n = -5
Return false
代码
1 class Solution { 2 public: 3 /* 4 * @param : the given number 5 * @return: whether whether there're two integers 6 */ 7 bool checkSumOfSquareNumbers(int num) { 8 // write your code here 9 if (num < 0) { 10 return false; 11 } 12 int c = floor(sqrt(num)); 13 int i = c; 14 if (i * i == num) return true; 15 int j = 1; 16 while (j <= i) { 17 if (i * i + j * j == num) { 18 return true; 19 } else if (i * i + j * j < num) { 20 j++; 21 } else { 22 i--; 23 } 24 } 25 return false; 26 } 27 };
转载于:https://www.cnblogs.com/gousheng/p/7629933.html
lintcode: Check Sum of Square Numbers相关推荐
- LeetCode633 | Sum of Square Numbers (Easy)
题目地址(633. 平方数之和) https://leetcode-cn.com/problems/sum-of-square-numbers/ 题目描述 给定一个非负整数 c ,你要判断是否存在两个 ...
- C#LeetCode刷题之#633-平方数之和( Sum of Square Numbers)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3885 访问. 给定一个非负整数 c ,你要判断是否存在两个整数 ...
- 633. Sum of Square Numbers
题目就是非负正数 c,是否可以写成c**2 = a**2+b**2 1.缩写范围,因为是平方和,所以a,b两个数一个在c的平方根内 2.循环得出另一个数的平方 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 ...
- 633.平方数之和(Sum of Square Numbers)
题目描述 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2: 输 ...
- lintcode: k Sum 解题报告
K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...
- [LintCode]k Sum
题目 Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers whe ...
- LintCode: 3 Sum
C++ 把3个数求和,转变为2个数求和 1. 把数组排序 2. 注意过滤重复值 3. 从前到后遍历,游标i 4. 从后边数中找start + end = -arr[i]的2 sum 5. start ...
- Lintcode: k Sum II
Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...
最新文章
- 本地应用 v-on补充
- [AaronYang]C#人爱学不学8[事件和.net4.5的弱事件深入浅出]
- Java 设计模式之《观察者模式》
- oracle ORA-01113的解决方法(file 1 needs media recovery)
- FAT AP和FIT AP 区别 (无线篇)
- Integer 值判断相等
- Feature Engineering 特征工程 1. Baseline Model
- Python从序列中选择k个不重复元素
- echarts地图地名显示_echarts地图 省份名称自定义位置和居中解决办法
- 如何用Camtasia进行内容补充?
- python怎么理解函数的参数_Python中函数参数理解
- “声波识别”可用于购物
- 解决webSocket不兼容IE浏览器问题
- excel查找窗口被拉边上_excel
- uboot如何驱动SD卡
- 企业邮箱登录入口和界面如何设置
- 命名----做程序员的基本要求
- 指标体系:指标的设计方法
- 单片机C语言应用100例(第二版)光盘资料 作者王东峰 陈圆圆 郭向阳
- 活动安排问题 动态规划、贪心算法C语言实现