Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit44491a6

Browse files
committed
202-happy-number.md Separated Chinese and English solutions.
1 parent8253553 commit44491a6

File tree

2 files changed

+37
-180
lines changed

2 files changed

+37
-180
lines changed

‎en/1-1000/202-happy-number.md

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#202. Happy Number - Best Practices of LeetCode Solutions
2-
LeetCode link:[202. Happy Number](https://leetcode.com/problems/happy-number),
3-
[202. 快乐数](https://leetcode.cn/problems/happy-number)
2+
LeetCode link:[202. Happy Number](https://leetcode.com/problems/happy-number), difficulty:**Easy**.
43

5-
[中文题解](#中文题解)
6-
7-
##LeetCode problem description
4+
##Description of "202. Happy Number"
85
Write an algorithm to determine if a number`n` is happy.
96

107
A**happy number** is a number defined by the following process:
@@ -15,8 +12,6 @@ A **happy number** is a number defined by the following process:
1512

1613
Return`true` if`n` is_a happy number_, and`false` if not.
1714

18-
Difficulty:**Easy**
19-
2015
###[Example 1]
2116
**Input**:`n = 19`
2217

@@ -39,8 +34,6 @@ Difficulty: **Easy**
3934
-`1 <= n <= 2**31 - 1`
4035

4136
##Intuition
42-
[中文题解](#中文题解)
43-
4437
1. It is more convenient to call`isHappy(n)` recursively. You only need to generate a new`n` as a parameter each time.
4538
2. If`n` has already appeared, it means that the loop has been entered, and`return false`. You can use`Set` to save the`n` that has appeared.
4639

@@ -222,71 +215,3 @@ public class Solution
222215
```
223216
// Welcome to create a PR to complete the code of this language, thanks!
224217
```
225-
226-
##问题描述
227-
编写一个算法来判断一个数`n` 是不是快乐数。
228-
229-
**快乐数**」 定义为:
230-
231-
* 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
232-
* 然后重复这个过程直到这个数变为`1`,也可能是**无限循环** 但始终变不到`1`
233-
* 如果这个过程**结果为**`1`,那么这个数就是快乐数。
234-
* 如果`n`_快乐数_ 就返回`true` ;不是,则返回`false`
235-
236-
难度:**容易**
237-
238-
###[Example 1]
239-
**输入**:`n = 19`
240-
241-
**输出**:`true`
242-
243-
**解释**
244-
```
245-
1**2 + 9**2 = 82
246-
8**2 + 2**2 = 68
247-
6**2 + 8**2 = 100
248-
1**2 + 0**2 + 0**2 = 1
249-
```
250-
251-
#中文题解
252-
##思路
253-
1. 递归调用`isHappy(n)`比较方便,每次只需要生成新的`n`作为参数。
254-
2. 如果`n`已经出现过了,说明进入了循环,`return false`。可以用`Set`保存已经出现过的`n`
255-
256-
##步骤
257-
1. 生成新的`n`作为`isHappy()`的参数。
258-
```javascript
259-
let sum=0
260-
261-
for (constdigitofn.toString()) {
262-
sum+=Math.pow(Number(digit),2)
263-
}
264-
265-
// omitted code
266-
267-
returnisHappy(sum)
268-
```
269-
270-
2. 如果`n`已经出现过了,说明进入了循环,`return false`。可以用`Set`保存已经出现过的`n`
271-
```javascript
272-
varisHappy=function (n,appearedNums) {
273-
appearedNums||=newSet()// 1
274-
let sum=0
275-
276-
for (constdigitofn.toString()) {
277-
sum+=Math.pow(Number(digit),2)
278-
}
279-
280-
if (sum==1) {
281-
returntrue
282-
}
283-
284-
if (appearedNums.has(sum)) {// 2
285-
returnfalse
286-
}
287-
288-
appearedNums.add(sum)// 3
289-
290-
returnisHappy(sum, appearedNums)
291-
};
292-
```

‎zh/1-1000/202-happy-number.md

Lines changed: 35 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
#202. Happy Number - Best Practices of LeetCode Solutions
2-
LeetCode link:[202. Happy Number](https://leetcode.com/problems/happy-number),
3-
[202. 快乐数](https://leetcode.cn/problems/happy-number)
1+
#202. 快乐数 - 力扣题解最佳实践
2+
力扣链接:[202. 快乐数](https://leetcode.cn/problems/happy-number), 难度:**简单**
43

5-
[中文题解](#中文题解)
6-
7-
##LeetCode problem description
8-
Write an algorithm to determine if a number`n` is happy.
9-
10-
A**happy number** is a number defined by the following process:
11-
12-
* Starting with any positive integer, replace the number by the sum of the squares of its digits.
13-
* Repeat the process until the number equals 1 (where it will stay), or it**loops endlessly in a cycle** which does not include 1.
14-
* Those numbers for which this process**ends in 1** are happy.
4+
##力扣“202. 快乐数”问题描述
5+
编写一个算法来判断一个数`n` 是不是快乐数。
156

16-
Return`true` if`n` is_a happy number_, and`false` if not.
7+
**快乐数**」 定义为:
178

18-
Difficulty:**Easy**
9+
* 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
10+
* 然后重复这个过程直到这个数变为`1`,也可能是**无限循环** 但始终变不到`1`
11+
* 如果这个过程**结果为**`1`,那么这个数就是快乐数。
12+
* 如果`n`_快乐数_ 就返回`true` ;不是,则返回`false`
1913

20-
###[Example 1]
21-
**Input**:`n = 19`
14+
###[示例 1]
15+
**输入**:`n = 19`
2216

23-
**Output**:`true`
17+
**输出**:`true`
2418

25-
**Explanation**
19+
**解释**:
2620
```
2721
1**2 + 9**2 = 82
2822
8**2 + 2**2 = 68
2923
6**2 + 8**2 = 100
3024
1**2 + 0**2 + 0**2 = 1
3125
```
3226

33-
###[Example 2]
34-
**Input**:`n = 2`
27+
###[示例 2]
28+
**输入**:`k = 2, prices = [3,2,6,5,0,3]`
3529

36-
**Output**:`false`
30+
**输出**:`7`
3731

38-
###[Constraints]
39-
-`1 <= n <= 2**31 - 1`
32+
**解释**:
33+
```
34+
在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。
35+
随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。
36+
```
4037

41-
##Intuition
42-
[中文题解](#中文题解)
38+
###[约束]
39+
-`1 <= k <= 100`
40+
-`1 <= prices.length <= 1000`
41+
-`0 <= prices[i] <= 1000`
4342

44-
1. It is more convenient to call`isHappy(n)` recursively. You only need to generate a new`n` as a parameter each time.
45-
2. If`n` has already appeared, it means that the loop has been entered, and`return false`. You can use`Set` to save the`n` that has appeared.
43+
##思路
44+
1. 递归调用`isHappy(n)`比较方便,每次只需要生成新的`n`作为参数。
45+
2. 如果`n`已经出现过了,说明进入了循环,`return false`。可以用`Set`保存已经出现过的`n`
4646

47-
##Steps
48-
1.Generate a new`n` as the`isHappy(n)` parameter.
47+
##步骤
48+
1.生成新的`n`作为`isHappy()`的参数。
4949
```javascript
5050
let sum=0
5151

@@ -58,9 +58,9 @@ for (const digit of n.toString()) {
5858
returnisHappy(sum)
5959
```
6060

61-
2.If`n` has already appeared, it means that the loop has been entered, and`return false`. You can use`Set` to save the`n` that has appeared.
61+
2.如果`n`已经出现过了,说明进入了循环,`return false`。可以用`Set`保存已经出现过的`n`
6262
```javascript
63-
varisHappy=function (n,appearedNums) {// 0
63+
varisHappy=function (n,appearedNums) {
6464
appearedNums||=newSet()// 1
6565
let sum=0
6666

@@ -82,9 +82,9 @@ var isHappy = function (n, appearedNums) { // 0
8282
};
8383
```
8484

85-
##Complexity
86-
*Time:`O(log N)`.
87-
*Space:`O(log N)`.
85+
##复杂度
86+
*时间:`O(log N)`
87+
*空间:`O(log N)`
8888

8989
##Java
9090
```java
@@ -222,71 +222,3 @@ public class Solution
222222
```
223223
// Welcome to create a PR to complete the code of this language, thanks!
224224
```
225-
226-
##问题描述
227-
编写一个算法来判断一个数`n` 是不是快乐数。
228-
229-
**快乐数**」 定义为:
230-
231-
* 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
232-
* 然后重复这个过程直到这个数变为`1`,也可能是**无限循环** 但始终变不到`1`
233-
* 如果这个过程**结果为**`1`,那么这个数就是快乐数。
234-
* 如果`n`_快乐数_ 就返回`true` ;不是,则返回`false`
235-
236-
难度:**容易**
237-
238-
###[Example 1]
239-
**输入**:`n = 19`
240-
241-
**输出**:`true`
242-
243-
**解释**
244-
```
245-
1**2 + 9**2 = 82
246-
8**2 + 2**2 = 68
247-
6**2 + 8**2 = 100
248-
1**2 + 0**2 + 0**2 = 1
249-
```
250-
251-
#中文题解
252-
##思路
253-
1. 递归调用`isHappy(n)`比较方便,每次只需要生成新的`n`作为参数。
254-
2. 如果`n`已经出现过了,说明进入了循环,`return false`。可以用`Set`保存已经出现过的`n`
255-
256-
##步骤
257-
1. 生成新的`n`作为`isHappy()`的参数。
258-
```javascript
259-
let sum=0
260-
261-
for (constdigitofn.toString()) {
262-
sum+=Math.pow(Number(digit),2)
263-
}
264-
265-
// omitted code
266-
267-
returnisHappy(sum)
268-
```
269-
270-
2. 如果`n`已经出现过了,说明进入了循环,`return false`。可以用`Set`保存已经出现过的`n`
271-
```javascript
272-
varisHappy=function (n,appearedNums) {
273-
appearedNums||=newSet()// 1
274-
let sum=0
275-
276-
for (constdigitofn.toString()) {
277-
sum+=Math.pow(Number(digit),2)
278-
}
279-
280-
if (sum==1) {
281-
returntrue
282-
}
283-
284-
if (appearedNums.has(sum)) {// 2
285-
returnfalse
286-
}
287-
288-
appearedNums.add(sum)// 3
289-
290-
returnisHappy(sum, appearedNums)
291-
};
292-
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp