mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
更新 0202.快乐数 排版格式修复
This commit is contained in:
@ -28,7 +28,7 @@
|
||||
6^2 + 8^2 = 100
|
||||
1^2 + 0^2 + 0^2 = 1
|
||||
|
||||
# 思路
|
||||
## 思路
|
||||
|
||||
这道题目看上去貌似一道数学问题,其实并不是!
|
||||
|
||||
@ -80,10 +80,10 @@ public:
|
||||
|
||||
|
||||
|
||||
# 其他语言版本
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```java
|
||||
class Solution {
|
||||
public boolean isHappy(int n) {
|
||||
@ -107,8 +107,9 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
(版本一)使用集合
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def isHappy(self, n: int) -> bool:
|
||||
@ -131,7 +132,7 @@ class Solution:
|
||||
n, r = divmod(n, 10)
|
||||
new_num += r ** 2
|
||||
return new_num
|
||||
```
|
||||
```
|
||||
(版本二)使用集合
|
||||
```python
|
||||
class Solution:
|
||||
@ -146,7 +147,7 @@ class Solution:
|
||||
if new_num==1: return True
|
||||
else: n = new_num
|
||||
return False
|
||||
```
|
||||
```
|
||||
(版本三)使用数组
|
||||
```python
|
||||
class Solution:
|
||||
@ -161,7 +162,7 @@ class Solution:
|
||||
if new_num==1: return True
|
||||
else: n = new_num
|
||||
return False
|
||||
```
|
||||
```
|
||||
(版本四)使用快慢指针
|
||||
```python
|
||||
class Solution:
|
||||
@ -180,7 +181,7 @@ class Solution:
|
||||
n, r = divmod(n, 10)
|
||||
new_num += r ** 2
|
||||
return new_num
|
||||
```
|
||||
```
|
||||
(版本五)使用集合+精简
|
||||
```python
|
||||
class Solution:
|
||||
@ -192,7 +193,7 @@ class Solution:
|
||||
return False
|
||||
seen.add(n)
|
||||
return True
|
||||
```
|
||||
```
|
||||
(版本六)使用数组+精简
|
||||
```python
|
||||
class Solution:
|
||||
@ -204,8 +205,9 @@ class Solution:
|
||||
return False
|
||||
seen.append(n)
|
||||
return True
|
||||
```
|
||||
Go:
|
||||
```
|
||||
### Go:
|
||||
|
||||
```go
|
||||
func isHappy(n int) bool {
|
||||
m := make(map[int]bool)
|
||||
@ -225,7 +227,7 @@ func getSum(n int) int {
|
||||
}
|
||||
```
|
||||
|
||||
javaScript:
|
||||
### JavaScript:
|
||||
|
||||
```js
|
||||
var isHappy = function (n) {
|
||||
@ -303,7 +305,7 @@ var isHappy = function(n) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function isHappy(n: number): boolean {
|
||||
@ -322,7 +324,7 @@ function isHappy(n: number): boolean {
|
||||
};
|
||||
```
|
||||
|
||||
Swift:
|
||||
### Swift:
|
||||
|
||||
```swift
|
||||
// number 每个位置上的数字的平方和
|
||||
@ -355,7 +357,8 @@ func isHappy(_ n: Int) -> Bool {
|
||||
}
|
||||
```
|
||||
|
||||
PHP:
|
||||
### PHP:
|
||||
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
@ -386,7 +389,8 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Rust:
|
||||
### Rust:
|
||||
|
||||
```Rust
|
||||
use std::collections::HashSet;
|
||||
impl Solution {
|
||||
@ -416,7 +420,8 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
|
||||
C:
|
||||
### C:
|
||||
|
||||
```C
|
||||
typedef struct HashNodeTag {
|
||||
int key; /* num */
|
||||
@ -473,8 +478,8 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
### C#:
|
||||
|
||||
C#:
|
||||
```csharp
|
||||
public class Solution {
|
||||
private int getSum(int n) {
|
||||
@ -500,3 +505,4 @@ public class Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user