Skip to content

Commit 296602c

Browse files
committed
feat: add solutions to leetcode problem: No.1134. Armstrong Number
1 parent 54932a3 commit 296602c

File tree

5 files changed

+109
-4
lines changed

5 files changed

+109
-4
lines changed

solution/1100-1199/1134.Armstrong Number/README.md

+39-2
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,64 @@
3636
<li><code>1 &lt;= N &lt;= 10^8</code></li>
3737
</ol>
3838

39-
4039
## 解法
4140

4241
<!-- 这里可写通用的实现逻辑 -->
4342

43+
先求数字 n 的长度 k,然后累加 n 上每一位的数字的 k 次幂。最后判断累加的结果是否与 n 相等即可。
44+
4445
<!-- tabs:start -->
4546

4647
### **Python3**
4748

4849
<!-- 这里可写当前语言的特殊实现逻辑 -->
4950

5051
```python
51-
52+
class Solution:
53+
def isArmstrong(self, n: int) -> bool:
54+
k = len(str(n))
55+
s, t = 0, n
56+
while t:
57+
t, v = divmod(t, 10)
58+
s += pow(v, k)
59+
return n == s
5260
```
5361

5462
### **Java**
5563

5664
<!-- 这里可写当前语言的特殊实现逻辑 -->
5765

5866
```java
67+
class Solution {
68+
public boolean isArmstrong(int n) {
69+
int k = String.valueOf(n).length();
70+
int s = 0, t = n;
71+
while (t != 0) {
72+
s += Math.pow(t % 10, k);
73+
t /= 10;
74+
}
75+
return n == s;
76+
}
77+
}
78+
```
5979

80+
### **JavaScript**
81+
82+
```js
83+
/**
84+
* @param {number} n
85+
* @return {boolean}
86+
*/
87+
var isArmstrong = function (n) {
88+
const k = String(n).length;
89+
let s = 0;
90+
let t = n;
91+
while (t) {
92+
s += Math.pow(t % 10, k);
93+
t = Math.floor(t / 10);
94+
}
95+
return n == s;
96+
};
6097
```
6198

6299
### **...**

solution/1100-1199/1134.Armstrong Number/README_EN.md

+37-2
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,56 @@
3232
<li><code>1 &lt;= n &lt;= 10<sup>8</sup></code></li>
3333
</ul>
3434

35-
3635
## Solutions
3736

3837
<!-- tabs:start -->
3938

4039
### **Python3**
4140

4241
```python
43-
42+
class Solution:
43+
def isArmstrong(self, n: int) -> bool:
44+
k = len(str(n))
45+
s, t = 0, n
46+
while t:
47+
t, v = divmod(t, 10)
48+
s += pow(v, k)
49+
return n == s
4450
```
4551

4652
### **Java**
4753

4854
```java
55+
class Solution {
56+
public boolean isArmstrong(int n) {
57+
int k = String.valueOf(n).length();
58+
int s = 0, t = n;
59+
while (t != 0) {
60+
s += Math.pow(t % 10, k);
61+
t /= 10;
62+
}
63+
return n == s;
64+
}
65+
}
66+
```
4967

68+
### **JavaScript**
69+
70+
```js
71+
/**
72+
* @param {number} n
73+
* @return {boolean}
74+
*/
75+
var isArmstrong = function (n) {
76+
const k = String(n).length;
77+
let s = 0;
78+
let t = n;
79+
while (t) {
80+
s += Math.pow(t % 10, k);
81+
t = Math.floor(t / 10);
82+
}
83+
return n == s;
84+
};
5085
```
5186

5287
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public boolean isArmstrong(int n) {
3+
int k = String.valueOf(n).length();
4+
int s = 0, t = n;
5+
while (t != 0) {
6+
s += Math.pow(t % 10, k);
7+
t /= 10;
8+
}
9+
return n == s;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number} n
3+
* @return {boolean}
4+
*/
5+
var isArmstrong = function (n) {
6+
const k = String(n).length;
7+
let s = 0;
8+
let t = n;
9+
while (t) {
10+
s += Math.pow(t % 10, k);
11+
t = Math.floor(t / 10);
12+
}
13+
return n == s;
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def isArmstrong(self, n: int) -> bool:
3+
k = len(str(n))
4+
s, t = 0, n
5+
while t:
6+
t, v = divmod(t, 10)
7+
s += pow(v, k)
8+
return n == s

0 commit comments

Comments
 (0)