Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6479b87

Browse files
committed
docs: 新增No38和No326题解
1 parent 8385ada commit 6479b87

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

leetcode刷题/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [No.21 合并两个有序链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No21_merge-two-lists.md)
99
- [No.26 从排序数组中删除重复项](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No26_remove-duplicates.md)
1010
- [No.36 有效的数独](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No36_isvalid-sudoku.md)
11+
- [No.38 报数](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No38_count-and-say.md)
1112
- [No.48 旋转图像](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No48_rotate.md)
1213
- [No.66 加一](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No66_plus-one.md)
1314
- [No.122 买卖股票的最佳时机 II](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No122_max-profit.md)
@@ -51,6 +52,7 @@
5152
- [No.8 字符串转换整数 (atoi)](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No8_my-atoi.md)
5253
- [No.14 最长公共前缀](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No14_longest-common-prefix.md)
5354
- [No.28 实现 strStr()](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No28_str-str.md)
55+
- [No.38 报数](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No38_count-and-say.md)
5456
- [No.125 验证回文串](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No125_is-palindrome.md)
5557
- [No.242 有效的字母异位词](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No242_is-anagram.md)
5658
- [No.387 字符串中的第一个唯一字符](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No387_first-uniq-char.md)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# No.326 3的幂
2+
3+
4+
给定一个整数,写一个函数来判断它是否是 3 的幂次方。
5+
6+
## 示例
7+
8+
示例 1:
9+
10+
```
11+
输入: 27
12+
输出: true
13+
```
14+
15+
示例 2:
16+
```
17+
输入: 0
18+
输出: false
19+
```
20+
21+
示例 3:
22+
23+
```
24+
输入: 9
25+
输出: true
26+
```
27+
28+
示例 4:
29+
30+
```
31+
输入: 45
32+
输出: false
33+
```
34+
35+
## 解题思路
36+
37+
将输入的数n一直除以3,如果是 3 第次幂,则最后的结果一定为1。
38+
39+
代码如下:
40+
41+
```javascript
42+
/**
43+
* @param {number} n
44+
* @return {boolean}
45+
*/
46+
var isPowerOfThree = function(n) {
47+
if (n == 0) {
48+
return false;
49+
}
50+
while (n != 1) {
51+
n = n / 3;
52+
if (n == 0) {
53+
return false;
54+
}
55+
}
56+
return true;
57+
};
58+
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# No.38 报数
2+
3+
难度: `easy`
4+
5+
6+
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:
7+
8+
```
9+
1. 1
10+
2. 11
11+
3. 21
12+
4. 1211
13+
5. 111221
14+
15+
```
16+
17+
1 被读作  "one 1"  ("一个一") , 即 11。
18+
11 被读作 "two 1s" ("两个一"), 即 21。
19+
21 被读作 "one 2",  "one 1" ("一个二" ,  "一个一") , 即 1211。
20+
21+
给定一个正整数 n(1 ≤ n ≤ 30),输出报数序列的第 n 项。
22+
23+
注意:整数顺序将表示为一个字符串。
24+
25+
 
26+
## 示例
27+
28+
示例 1:
29+
```
30+
输入: 1
31+
输出: "1"
32+
```
33+
34+
示例 2:
35+
```
36+
输入: 4
37+
输出: "1211"
38+
```
39+
40+
## 解题思路
41+
42+
一次此看题目一脸懵逼,需要多读几遍题目。
43+
44+
还是用正则表达式来做,全局匹配数字,且数字连续相同。
45+
46+
代码如下:
47+
48+
```javascript
49+
/**
50+
* @param {number} n
51+
* @return {string}
52+
*/
53+
var countAndSay = function(n) {
54+
let prev = '1'
55+
for(let i = 1; i < n; i++){
56+
prev = prev.replace(/(\d)\1*/g, item =>`${item.length}${item[0]}`)
57+
}
58+
return prev
59+
};
60+
61+
```

0 commit comments

Comments
 (0)