[Swift]LeetCode1134. 阿姆斯特朗數 | Armstrong Number

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-filulupc-ku.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

The k-digit number N is an Armstrong number if and only if the k-th power of each digit sums to N.git

Given a positive integer N, return true if and only if it is an Armstrong number.github

Example 1:微信

Input: 153
Output: true Explanation: 153 is a 3-digit number, and 153 = 1^3 + 5^3 + 3^3. 

Example 2:spa

Input: 123
Output: false Explanation: 123 is a 3-digit number, and 123 != 1^3 + 2^3 + 3^3 = 36.

Note:code

  1. 1 <= N <= 10^8

假設存在一個 k 位數 N,其每一位上的數字的 k 次冪的總和也是 N,那麼這個數是阿姆斯特朗數。htm

給你一個正整數 N,讓你來斷定他是不是阿姆斯特朗數,是則返回 true,不是則返回 falseblog

示例 1:get

輸入:153
輸出:true
示例: 
153 是一個 3 位數,且 153 = 1^3 + 5^3 + 3^3。

示例 2:input

輸入:123
輸出:false
解釋: 
123 是一個 3 位數,且 123 != 1^3 + 2^3 + 3^3 = 36。

提示:

  1. 1 <= N <= 10^8

4 ms

 1 class Solution {
 2     func isArmstrong(_ N: Int) -> Bool {
 3         var N = N
 4         var t:[Int] = [Int](repeating:0,count:11)
 5         var cnt:Int = 0
 6         var i:Int = 0
 7         var j:Int = 0
 8         var orz:Int = N
 9         var ans:Int = 0
10         while(N != 0)
11         {
12             cnt += 1
13             t[cnt] = N % 10
14             N /= 10
15         }
16         for i in 1...cnt
17         {
18             var res:Int = 1
19             for j in 1...cnt
20             {
21                 res *= t[i]
22             }
23             ans+=res
24         }
25         return ans == orz
26     }
27 }
相關文章
相關標籤/搜索