2017.12.25 LeetCode 633. Sum of Square Numbers【思维枚举】
来源:互联网 发布:linux如何查看文件权限 编辑:程序博客网 时间:2024/06/14 14:03
633. 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
Example 1:
Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: 3
Output: False
题意: 给你一个int范围内的数n,让你判断是否存在两个数的平方和为n
分析: 不可以暴力的枚举两个数,可以用set来单方面枚举,先预处理出来所有的平方数,然后直接set::count(),即可
参考函数
class Solution {public: set<int> s; long long MAX = 0x7fffffff; long long m = sqrt(MAX+0.5); void init() { for(long long i = 0;i <= m;i++) { s.insert(i*i); } } bool judgeSquareSum(int c) { init(); bool flg = false; for(auto it = s.begin(); it != s.end(); it++) { if((*it) > c) break; if(s.count(c-(*it))){ return true; flg = true; } } if(!flg) return false; }};
- 如有错误或遗漏,请私聊下UP,thx
阅读全文