• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode202.快乐数|HappyNumber

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9744963.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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: 

Input: 19
Output: true
Explanation: 
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

编写一个算法来判断一个数是不是“快乐数”。

一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。

示例: 

输入: 19
输出: true
解释: 
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

8ms
 1 class Solution {
 2     func isHappy(_ n: Int) -> Bool {
 3         var fast = n
 4         var slow = n
 5         fast = sumOfSquares(fast)
 6         fast = sumOfSquares(fast)
 7         slow = sumOfSquares(slow)
 8         while fast != slow {
 9             fast = sumOfSquares(fast)
10             fast = sumOfSquares(fast)
11             slow = sumOfSquares(slow) 
12         }
13         return slow == 1
14         
15     }
16     
17     func sumOfSquares(_ n: Int) -> Int {
18         var num = n
19         var sum = 0
20         while num > 0 {
21             let value = num%10
22             sum += value*value
23             num /= 10
24         }
25         return sum
26     }
27 }

12ms

 1 class Solution {
 2     func isHappy(_ n: Int) -> Bool {
 3         var hasOccurredMap = Dictionary<Int, Bool>()
 4         return isHappy(n, &hasOccurredMap)
 5     }
 6     
 7     private func isHappy(
 8         _ n: Int,
 9         _ hasOccurredMap: inout Dictionary<Int, Bool>) -> Bool {
10         guard n != 1 else { return true }
11         guard hasOccurredMap[n] == nil else { return false }
12         var m = 0
13         var temp = n
14         while temp > 0 {
15             let digit = temp % 10
16             temp /= 10
17             m += digit * digit
18         }
19         hasOccurredMap[n] = true
20         return isHappy(m, &hasOccurredMap)
21     }
22 }

12ms

 1 class Solution {
 2     func isHappy(_ n: Int) -> Bool {
 3       var nT = n
 4         
 5         while (nT != 1 && nT != 4) {
 6             var t = 0
 7             while (nT>0) {
 8                 t += (nT % 10) * (nT % 10)
 9                 nT /= 10
10             }
11             nT = t
12         }
13         return nT == 1  
14     }
15 }

16ms

 1 class Solution {
 2     private let map:[Character: Int] = ["0" : 0, "1" : 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9]
 3     private var memory = Set<Int>()
 4     
 5     func isHappy(_ n: Int) -> Bool {
 6         var digits = getDigits(n)
 7         var square = getSquare(digits)
 8         memory.insert(square)
 9         if square == 1 {
10             return true
11         } else {
12             while true {
13                 digits = getDigits(square)
14                 square = getSquare(digits)
15                 if memory.contains(square) {
16                     return false
17                 }
18                 memory.insert(square)
19                 if square == 1 {
20                     return true
21                 }
22             }
23         }
24         return true
25     }
26     
27     func getDigits(_ n: Int) -> [Int] {
28         var digits = [Int]()
29         for s in String(n) {
30             guard let val = map[s] else {
31                 continue
32             }
33             digits.append(val)
34         }
35         return digits
36     }
37     
38     func getSquare(_ digits: [Int]) -> Int {
39         var res = 0
40         for digit in digits {
41             res += digit * digit
42         }
43         return res
44     }
45 }

20ms

 1 class Solution {
 2     func isHappy(_ n: Int) -> Bool {
 3         var prevSums: Set<Int> = Set()
 4         var curr = n
 5         while curr != 1 {
 6             if prevSums.contains(curr) {
 7                 return false
 8             }
 9             prevSums.insert(curr)
10             let digits: [Int] = String(curr).compactMap { Int(String($0)) }
11             var newSum = 0
12             for digit in digits {
13                 newSum += (digit * digit)
14             }
15             curr = newSum
16         }
17         return true
18     }
19 }

24ms

 1 class Solution {
 2     func isHappy(_ n: Int) -> Bool {
 3         var preciuslySeen: [Int: Bool] = [:]
 4         
 5         var check = n
 6         while(check != 1) {
 7             var digits = String(check).characters
 8 
 9             let squares = digits.map { ( Int(String($0)) ?? 0 ) * ( Int(String($0)) ?? 0 ) }
10             let sum = squares.reduce(0) { $0 + $1 }
11 
12             if sum == 1 { 
13                 return true
14             } else if preciuslySeen[sum] == true {
15                 return false
16             }
17 
18             preciuslySeen[sum] = true
19             check = sum
20         }
21         
22         return true
23     }
24 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap