多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

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

leetcode 202 Happy Number

來源:程序員人生   發(fā)布時間:2015-09-23 08:18:17 閱讀次數(shù):4143次

 

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

我的解決方案:



// happy number.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <set> using namespace std; bool isHappy(int n) { int split = 0; int sum = 0; set<int> myset; set<int>::iterator it; while(sum != 1) { do { split = n % 10; n = n / 10; sum = split* split + sum; } while(n>0); if(sum==1) { break; } else { it=myset.find(sum); if(it!=myset.end()) { return false; } myset.insert(sum); n = sum; sum = 0; } } return true; } int _tmain(int argc, _TCHAR* argv[]) { isHappy(19); return 0; }





最短的1個代碼,用了些數(shù)論的知識吧:

https://leetcode.com/discuss/33014/4ms⑸-line-c-code


bool isHappy(int n) { while(n>6){ int next = 0; while(n){next+=(n%10)*(n%10); n/=10;} n = next; } return n==1; }



兩個python代碼:




def isHappy(self, n): return self.isHappyHelper(n, {}) def isHappyHelper(self, n, prev): if n == 1: return True elif n not in prev: prev[n] = 1 else: return False new = 0 for char in str(n): new += int(char)**2 return self.isHappyHelper(new, prev)

class Solution: # @param {integer} n # @return {boolean} def isHappy(self, n): table = [] n = self.convert(n) while n != 1: if n in table: return False else: table.append(n) n = self.convert(n) return True # @param {integer} n # @return {integer} sum of digits def convert(self, n): res = 0 while n > 0: temp = n % 10 res += temp * temp n = n // 10 return res




生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 69av视频| 波多野结衣一区二区三区四区 | 欧美日本黄色 | 免费看成人毛片日本久久 | 亚洲天堂视频在线观看免费 | 久久乐国产综合亚洲精品 | 欧美精品第一页 | 欧美巨大另类极品videosbest | 日韩久久久精品首页 | 一区二区三区在线免费看 | 456成人免费高清视频 | 波多野结衣在线观看一区 | 精品一区二区三区四区乱码90 | 日本免费中文字幕在线看 | 福利视频第一区 | 欧美网色 | 高清亚洲 | 九九久久久久午夜精选 | 亚洲高清视频在线 | 日本护士xxxxx在线 | 私人午夜影院 | 久久精品国产99久久99久久久 | 国产麻豆精品在线观看 | 国产免费一级高清淫曰本片 | 欧美一级欧美一级在线播放 | 欧美人与zoxxxx视频 | 日本天堂视频 | 国产aⅴ自拍 | 亚洲欧美精品天堂久久综合一区 | 亚洲无线乱码高清在线观看一区 | 成人在线一区二区 | 国产成人一区二区三区影院免费 | 亚洲成a人片在线观看尤物 亚洲成a人片在线观看中文!!! | 国产成人精品视频在放 | 自拍欧美亚洲 | 欧美妇性猛交视频 | 精品国产欧美一区二区三区成人 | 亚洲欧美色综合一区二区在线 | 亚洲国产欧美精品一区二区三区 | 广西毛片| 亚洲日韩aⅴ在线视频 |