日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全I(xiàn)T社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > 綜合技術(shù) > LeetCode Happy Number

LeetCode Happy Number

來源:程序員人生   發(fā)布時間:2015-06-23 08:16:23 閱讀次數(shù):3325次

1. 題目

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

2.解題方案

class Solution { public: bool isHappy(int n) { if(n < 1){ return false; } if(n == 1){ return true; } unordered_set<int> showNums; showNums.insert(n); while(true){ int sum = 0; while(n != 0){ int oneDigital = n % 10; n = n / 10; sum = sum + oneDigital * oneDigital; } if(sum == 1){ return true; }else{ if(showNums.find(sum) == showNums.end()){ showNums.insert(sum); }else{ return false; } } n = sum; } } };

思路:用求余和除以10得到每位數(shù),相加進(jìn)行判斷。這里還用了1個set來存儲已出現(xiàn)的數(shù)字,如果重復(fù)了,說明就不是1個happy number。


http://www.waitingfy.com/archives/1678

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 亚色在线视频 | 欧美日韩精选 | 白浆在线 | 成人欧美一区二区三区黑人 | 国产一区二区三区的电影 | 日韩精品一区二区三区四区 | 久久久夜夜夜 | 在线欧美日韩 | 欧美激情视频一区二区三区 | 成人免费激情视频 | 日韩久久免费视频 | 精品久久成人网 | 欧洲精品久久 | 精品综合在线 | 国产高清在线精品一区二区三区 | 成人一级毛片 | 男人午夜影院 | 免费福利在线视频 | 国内精品久久久久久久影视简单 | 成人毛片视频在线观看 | 国产精品久久久久久一区二区 | 91精品国产自产91精品 | 日本国产欧美 | 国产成人在线观看免费网站 | 亚洲看片网站 | 亚洲第一天堂无码专区 | 免费av在线| 91骚视频 | 综合久久久久 | 国产成人久久精品 | 久久懂色精品99综一区合 | 亚洲三级在线播放 | 国产在线一二区 | 国产一级一级国产 | 久久五月天婷婷 | 在线精品一区 | 国产成人久久精品麻豆二区 | 婷久久| 五月综合激情 | 久久久亚洲 | 久久精品屋 |