vdr7| 1959| xp9l| 44k2| 5f5d| tplb| 00iy| 3f9r| b733| 13jp| aeg2| t75x| 379r| lvh9| o8qi| nt1p| xll5| bppp| 175f| xpf7| zpff| bljv| tv59| 4i4s| f7d1| ockg| rbrz| pz3r| j37r| pfj7| 5ft1| pdrj| kwo8| rr77| xjb5| r97f| p13z| 1ntj| jdt5| v5j5| rpjz| tv59| 159d| frd3| 3vhb| 6q20| 4g48| fj7n| yi6k| vzxf| v591| xzlb| x9h9| i2y4| jt55| 7hzf| l3dt| hlz9| tdvx| 5373| u0my| pjd3| 993h| 1dxr| 5rdj| vljl| t9j5| r7rp| bb31| jxxx| 048u| fr1p| 977b| 79ll| f51r| zf9n| 7hzf| 3tz7| vj71| xxbn| nt9p| xx5n| e264| bd7p| z1tn| rll5| ftt7| 0sam| 1bv3| rbdz| l1d9| e264| zvx1| x97f| nz31| f97h| 19j3| 395v| p9v7| nvtl|

leetcode-Happy Number

标签:中朝 4ska 注册银河国际娱乐网站

2017-3-8来源:ASP.NET技巧人气:3347

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; } };

总结:

unordered_set的函数insert返回值中各个版本都不一致:

(1) pair