[Leetcode with python] 202. Happy Number (# Hash table)
2019-02-11 17:53
477 查看
题目
https://leetcode.com/problems/happy-number/
解题思路
用字典储存出现过的数字。若重复出现,则返回False;若等于1,则返回True。
代码
class Solution(object): def cal_square_sum(self, n): num_l = [] while n > 0: num_l.append(n%10) n = n/10 res = 0 for i in num_l: res += i**2 return res def isHappy(self, n): """ :type n: int :rtype: bool """ d = {} while n not in d: d[n] = 0 n = self.cal_square_sum(n) if n == 1: return True return False
相关文章推荐
- [leetcode: Python]202.Happy Number
- [Leetcode with python] 242. Valid Anagram (# Hash table)
- [Leetcode with python] 3. Longest Substring Without Repeating Characters (# Hash table)
- [Leetcode with python] 36. Valid Sudoku (# Hash table)
- 【LeetCode with Python】 N-Queens
- [Leetcode][python]Copy List with Random Pointer
- 【LeetCode with Python】 LRU Cache
- [和小菜鸡一起刷题(python)] LeetCode 138. 复制带随机指针的链表(Copy List with Random Pointer)
- [LeetCode]202. Happy Number
- 【LeetCode with Python】 Single Number
- 【LeetCode with Python】 Balanced Binary Tree
- 【LeetCode with Python】 Populating Next Right Pointers in Each Node
- leetcode 202. Happy Number
- 【LeetCode with Python】 Longest Common Prefix
- (easy)LeetCode 202.Happy Number
- LeetCode202 Happy Number
- LeetCode 202. Happy Number (Easy)
- LeetCode 202. Happy Number
- 【LeetCode with Python】 Interleaving String
- <LeetCode OJ> Happy Number【202】