From 764b3e9e51608baf17183a5e36cab0888a6e46f9 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 19 Jul 2023 14:34:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=200202.=E5=BF=AB=E4=B9=90?= =?UTF-8?q?=E6=95=B0=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0202.快乐数.md | 42 ++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index 7fe8cd8d..4a77e2b6 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -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 { +