[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 }
相关文章
相关标签/搜索