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.

您在真实的面试中是否遇到过这个题?

Yes

样例

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相关推荐

  1. LeetCode633 | Sum of Square Numbers (Easy)

    题目地址(633. 平方数之和) https://leetcode-cn.com/problems/sum-of-square-numbers/ 题目描述 给定一个非负整数 c ,你要判断是否存在两个 ...

  2. C#LeetCode刷题之#633-平方数之和( Sum of Square Numbers)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3885 访问. 给定一个非负整数 c ,你要判断是否存在两个整数 ...

  3. 633. Sum of Square Numbers

    题目就是非负正数 c,是否可以写成c**2 = a**2+b**2 1.缩写范围,因为是平方和,所以a,b两个数一个在c的平方根内 2.循环得出另一个数的平方 3.这里用到一个小技巧,一个数的平方开方 ...

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

  5. 633.平方数之和(Sum of Square Numbers)

    题目描述 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2: 输 ...

  6. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

  7. [LintCode]k Sum

    题目 Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers whe ...

  8. LintCode: 3 Sum

    C++ 把3个数求和,转变为2个数求和 1. 把数组排序 2. 注意过滤重复值 3. 从前到后遍历,游标i 4. 从后边数中找start + end = -arr[i]的2 sum 5. start ...

  9. Lintcode: k Sum II

    Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...

最新文章

  1. 本地应用 v-on补充
  2. [AaronYang]C#人爱学不学8[事件和.net4.5的弱事件深入浅出]
  3. Java 设计模式之《观察者模式》
  4. oracle ORA-01113的解决方法(file 1 needs media recovery)
  5. FAT AP和FIT AP 区别 (无线篇)
  6. Integer 值判断相等
  7. Feature Engineering 特征工程 1. Baseline Model
  8. Python从序列中选择k个不重复元素
  9. echarts地图地名显示_echarts地图 省份名称自定义位置和居中解决办法
  10. 如何用Camtasia进行内容补充?
  11. python怎么理解函数的参数_Python中函数参数理解
  12. “声波识别”可用于购物
  13. 解决webSocket不兼容IE浏览器问题
  14. excel查找窗口被拉边上_excel
  15. uboot如何驱动SD卡
  16. 企业邮箱登录入口和界面如何设置
  17. 命名----做程序员的基本要求
  18. 指标体系:指标的设计方法
  19. 单片机C语言应用100例(第二版)光盘资料 作者王东峰 陈圆圆 郭向阳
  20. 活动安排问题 动态规划、贪心算法C语言实现

热门文章

  1. Upload LABS Pass-10
  2. [java] byte不能直接相加
  3. CUDA学习(一)之使用GPU输出HelloWorld
  4. 从Nest到Nesk -- 模块化Node框架的实践
  5. 如何在树莓派上安装Fedora 25
  6. LVS+Keepalive+Nginx实现负载均衡
  7. 学员参观IDC机房及实操实践活动
  8. 在AX4.0中使用C#脚本的实现
  9. 手把手教你部署Nagios
  10. Python天天美味(4) - isinstance判断对象类型