国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

leetcode-Happy Number

2019-11-06 05:26:42
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Question:

Write an algorithm to determine if a number is “happy”.

A happy number is a number defined by the following PRocess: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number

12 + 92 = 82 82 + 22 = 68 62 + 82 = 100 12 + 02 + 02 = 1

Solution:

class Solution { public: bool isHappy(int n) { if(n == 0){ return false; } n = getNum(n); unordered_set<int> mset; while(mset.insert(n).second){ if(n == 1){ return true; } n = getNum(n); } return false; } int getNum(int n){ int res = 0; while(n){ int tmp = n % 10; res += tmp * tmp; n /= 10; } return res; }};

總結(jié):

unordered_set的函數(shù)insert返回值中各個(gè)版本都不一致:

(1) pair


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄄城县| 延长县| 庐江县| 东海县| 云南省| 巧家县| 四川省| 宁都县| 陵川县| 广宗县| 和平区| 四平市| 呼伦贝尔市| 穆棱市| 来安县| 方城县| 邛崃市| 仙桃市| 芜湖市| 民勤县| 上思县| 金门县| 台南市| 盖州市| 井冈山市| 永善县| 调兵山市| 霍林郭勒市| 商水县| 平昌县| 金川县| 漯河市| 阿拉善左旗| 杭州市| 黑河市| 磐石市| 隆德县| 抚顺市| 偃师市| 嫩江县| 抚州市|