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 thata2+b2=c.

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
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 冰桶报价 红酒 冰桶 坐冰桶内打麻将 重庆人坐冰桶内打麻将 冰桶挑战是什么 冰桶挑战图片 冰桶挑战的意义何在 冰桶挑战的利弊 类似冰桶挑战的活动 小智冰桶挑战 明星冰桶挑战 冰桶挑战点名 冰桶挑战筹款 冰桶挑战时间 冰桶挑战类似 冰桶挑战是什么意思 酒缸 酒杯架 红酒杯架 高脚杯 杯架 橡木桶 漏斗 红酒塞 酒壶 玻璃酒杯 葡萄酒瓶 小酒杯 葡萄酒杯 酒盒 白酒杯 酒坛子 洋酒杯 酒杯 酒架 分酒器 酒塞 啤酒杯 鸡尾酒杯 葡萄酒醒酒是什么意思 葡萄酒醒酒时间