Skip to content

Commit 6051a97

Browse files
committed
feat: add solutions to lc problem: No.1134
No.1134.Armstrong Number
1 parent c2e0c1d commit 6051a97

File tree

8 files changed

+164
-49
lines changed

8 files changed

+164
-49
lines changed

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

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44-
先求数字 n 的长度 k,然后累加 n 上每一位的数字的 k 次幂。最后判断累加的结果是否与 n 相等即可。
44+
**方法一:模拟**
45+
46+
我们可以先计算出数字的位数 $k$,然后计算每一位上的数字的 $k$ 次幂的总和 $s$,最后判断 $s$ 是否等于 $n$ 即可。
47+
48+
时间复杂度 $O(\log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为给定的数字。
4549

4650
<!-- tabs:start -->
4751

@@ -53,11 +57,11 @@
5357
class Solution:
5458
def isArmstrong(self, n: int) -> bool:
5559
k = len(str(n))
56-
s, t = 0, n
57-
while t:
58-
t, v = divmod(t, 10)
59-
s += pow(v, k)
60-
return n == s
60+
s, x = 0, n
61+
while x:
62+
s += (x % 10)**k
63+
x //= 10
64+
return s == n
6165
```
6266

6367
### **Java**
@@ -67,17 +71,48 @@ class Solution:
6771
```java
6872
class Solution {
6973
public boolean isArmstrong(int n) {
70-
int k = String.valueOf(n).length();
71-
int s = 0, t = n;
72-
while (t != 0) {
73-
s += Math.pow(t % 10, k);
74-
t /= 10;
74+
int k = (n + "").length();
75+
int s = 0;
76+
for (int x = n; x > 0; x /= 10) {
77+
s += Math.pow(x % 10, k);
7578
}
76-
return n == s;
79+
return s == n;
7780
}
7881
}
7982
```
8083

84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
bool isArmstrong(int n) {
90+
int k = to_string(n).size();
91+
int s = 0;
92+
for (int x = n; x; x /= 10) {
93+
s += pow(x % 10, k);
94+
}
95+
return s == n;
96+
}
97+
};
98+
```
99+
100+
### **Go**
101+
102+
```go
103+
func isArmstrong(n int) bool {
104+
k := 0
105+
for x := n; x > 0; x /= 10 {
106+
k++
107+
}
108+
s := 0
109+
for x := n; x > 0; x /= 10 {
110+
s += int(math.Pow(float64(x%10), float64(k)))
111+
}
112+
return s == n
113+
}
114+
```
115+
81116
### **JavaScript**
82117

83118
```js
@@ -88,15 +123,26 @@ class Solution {
88123
var isArmstrong = function (n) {
89124
const k = String(n).length;
90125
let s = 0;
91-
let t = n;
92-
while (t) {
93-
s += Math.pow(t % 10, k);
94-
t = Math.floor(t / 10);
126+
for (let x = n; x; x = Math.floor(x / 10)) {
127+
s += Math.pow(x % 10, k);
95128
}
96-
return n == s;
129+
return s == n;
97130
};
98131
```
99132

133+
### **TypeScript**
134+
135+
```ts
136+
function isArmstrong(n: number): boolean {
137+
const k = String(n).length;
138+
let s = 0;
139+
for (let x = n; x; x = Math.floor(x / 10)) {
140+
s += Math.pow(x % 10, k);
141+
}
142+
return s == n;
143+
}
144+
```
145+
100146
### **...**
101147

102148
```

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

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,60 @@
4242
class Solution:
4343
def isArmstrong(self, n: int) -> bool:
4444
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
45+
s, x = 0, n
46+
while x:
47+
s += (x % 10)**k
48+
x //= 10
49+
return s == n
5050
```
5151

5252
### **Java**
5353

5454
```java
5555
class Solution {
5656
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;
57+
int k = (n + "").length();
58+
int s = 0;
59+
for (int x = n; x > 0; x /= 10) {
60+
s += Math.pow(x % 10, k);
6261
}
63-
return n == s;
62+
return s == n;
6463
}
6564
}
6665
```
6766

67+
### **C++**
68+
69+
```cpp
70+
class Solution {
71+
public:
72+
bool isArmstrong(int n) {
73+
int k = to_string(n).size();
74+
int s = 0;
75+
for (int x = n; x; x /= 10) {
76+
s += pow(x % 10, k);
77+
}
78+
return s == n;
79+
}
80+
};
81+
```
82+
83+
### **Go**
84+
85+
```go
86+
func isArmstrong(n int) bool {
87+
k := 0
88+
for x := n; x > 0; x /= 10 {
89+
k++
90+
}
91+
s := 0
92+
for x := n; x > 0; x /= 10 {
93+
s += int(math.Pow(float64(x%10), float64(k)))
94+
}
95+
return s == n
96+
}
97+
```
98+
6899
### **JavaScript**
69100

70101
```js
@@ -75,15 +106,26 @@ class Solution {
75106
var isArmstrong = function (n) {
76107
const k = String(n).length;
77108
let s = 0;
78-
let t = n;
79-
while (t) {
80-
s += Math.pow(t % 10, k);
81-
t = Math.floor(t / 10);
109+
for (let x = n; x; x = Math.floor(x / 10)) {
110+
s += Math.pow(x % 10, k);
82111
}
83-
return n == s;
112+
return s == n;
84113
};
85114
```
86115

116+
### **TypeScript**
117+
118+
```ts
119+
function isArmstrong(n: number): boolean {
120+
const k = String(n).length;
121+
let s = 0;
122+
for (let x = n; x; x = Math.floor(x / 10)) {
123+
s += Math.pow(x % 10, k);
124+
}
125+
return s == n;
126+
}
127+
```
128+
87129
### **...**
88130

89131
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
bool isArmstrong(int n) {
4+
int k = to_string(n).size();
5+
int s = 0;
6+
for (int x = n; x; x /= 10) {
7+
s += pow(x % 10, k);
8+
}
9+
return s == n;
10+
}
11+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func isArmstrong(n int) bool {
2+
k := 0
3+
for x := n; x > 0; x /= 10 {
4+
k++
5+
}
6+
s := 0
7+
for x := n; x > 0; x /= 10 {
8+
s += int(math.Pow(float64(x%10), float64(k)))
9+
}
10+
return s == n
11+
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
class Solution {
22
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;
3+
int k = (n + "").length();
4+
int s = 0;
5+
for (int x = n; x > 0; x /= 10) {
6+
s += Math.pow(x % 10, k);
87
}
9-
return n == s;
8+
return s == n;
109
}
1110
}

solution/1100-1199/1134.Armstrong Number/Solution.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
var isArmstrong = function (n) {
66
const k = String(n).length;
77
let s = 0;
8-
let t = n;
9-
while (t) {
10-
s += Math.pow(t % 10, k);
11-
t = Math.floor(t / 10);
8+
for (let x = n; x; x = Math.floor(x / 10)) {
9+
s += Math.pow(x % 10, k);
1210
}
13-
return n == s;
11+
return s == n;
1412
};
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Solution:
22
def isArmstrong(self, n: int) -> bool:
33
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
4+
s, x = 0, n
5+
while x:
6+
s += (x % 10)**k
7+
x //= 10
8+
return s == n
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function isArmstrong(n: number): boolean {
2+
const k = String(n).length;
3+
let s = 0;
4+
for (let x = n; x; x = Math.floor(x / 10)) {
5+
s += Math.pow(x % 10, k);
6+
}
7+
return s == n;
8+
}

0 commit comments

Comments
 (0)