判断是否为平方数之和-LintCode
2017-11-22 16:07
211 查看
给一个整数 c, 你需要判断是否存在两个整数 a 和 b 使得 a^2 + b^2 = c.
样例:
给出 n = 5
返回 true // 1 * 1 + 2 * 2 = 5
给出 n = -5
返回 false
思路:
构建set包含0*0,1*1,…,c√∗c√
利用指针it,遍历set,查找是否有元素等于c-*it,
若存在,返回true
样例:
给出 n = 5
返回 true // 1 * 1 + 2 * 2 = 5
给出 n = -5
返回 false
思路:
构建set包含0*0,1*1,…,c√∗c√
利用指针it,遍历set,查找是否有元素等于c-*it,
若存在,返回true
#ifndef C697_H #define C697_H #include<iostream> #include<set> #include<math.h> using namespace std; class Solution { public: /* * @param : the given number * @return: whether whether there're two integers */ bool checkSumOfSquareNumbers(int num) { // write your code here if (num < 0) return false; int n = (int)sqrt(num); set<int> set; for (int i = 0; i <= n; ++i) set.insert(i*i); for (auto it = set.begin(); it != set.end(); ++it) { if (set.find(num - *it) != set.end()) { return true; } } return false; } }; #endif
相关文章推荐
- LintCode题解之判断是否为平方数之和
- LintCode 判断一个二叉树树是否是另一个二叉树的子书
- 判断一个数是否为平方数
- 【Lintcode】判断字符串是否没有重复字符
- python_lintcode_685First Unique Number In Stream_157判断字符串是否没有重复字符
- Lintcode697-判断是否为平方数之和
- 编写函数,判断两个自然数是否是“自然对数”。所谓“自然对数”是指两个自然数的和与差都是平方数。例:17-8=9 ,17+8=25 ,则17与8就是自然对数。主函数中输出100以内的所有自然对数。
- 判断一个自然数是否是某个数的平方
- lintcode 判断一个单链表是否有环及环的链接点
- 697 - 判断是否为平方数之和
- Lintcode 157. 判断字符串是否没有重复字符
- lintcode-157-判断字符串是否没有重复字符
- 判断一个自然数是否是某个数的平方
- lintcode-389-判断数独是否合法
- LintCode-判断字符串是否没有重复字符
- python_lintcode_96. 链表划分_389. 判断数独是否合法
- lintcode 容易题:Valid Sudoku 判断数独是否合法
- Java之判断大整数是否为平方数
- 日常练习:lintcode 157. 判断字符串是否没有重复字符
- 判断一个数是否是2的平方