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

Commit 3b6c65d

Browse files
committed
docs: add No204、No581题解
1 parent 71a9081 commit 3b6c65d

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

‎leetcode刷题/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- [No.160 相交链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No160_get-intersection-node.md)
2626
- [No.169 多数元素](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No169_majority-element.md)
2727
- [No.189 旋转数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No189_rotate-arr.md)
28+
- [No.204 计数质数](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No204_count-primes.md)
2829
- [No.206 反转链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No206_reverse-list.md)
2930
- [No.234 回文链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No234_is-palindrome.md)
3031
- [No.237 删除链表节点](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No237_delete-node.md)
@@ -36,6 +37,7 @@
3637
- [No.412 Fizz Buzz](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No412_fizz-buzz.md)
3738
- [No.448 找到所有数组中消失的数字](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No448_find-disappeared-numbers.md)
3839
- [No.492 构造矩形](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No492_construct-rectangle.md)
40+
- [No.581 最短无序连续子数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No581_find-unsorted-subarray.md)
3941

4042
## 分类
4143

@@ -54,7 +56,8 @@
5456
- [No.189 旋转数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No189_rotate-arr.md)
5557
- [No.283 移动零](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No283_move-zeroes.md)
5658
- [No.350 两个数组的交集 II](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No350_intersect.md)
57-
59+
- [No.448 找到所有数组中消失的数字](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No448_find-disappeared-numbers.md)
60+
- [No.581 最短无序连续子数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No581_find-unsorted-subarray.md)
5861

5962
### 字符串
6063

@@ -85,4 +88,6 @@
8588

8689
#### 数学初级
8790

88-
- [No.326 3的幂](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No326_is-power-of-three.md)
91+
- [No.326 3的幂](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No326_is-power-of-three.md)
92+
- [No.204 计数质数](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No204_count-primes.md)
93+
- [No.492 构造矩形](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No492_construct-rectangle.md)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# No.204 计数质数
2+
3+
难度:`easy`
4+
5+
6+
统计所有小于非负整数 n 的质数的数量。
7+
8+
## 示例
9+
10+
示例:
11+
12+
```
13+
输入: 10
14+
输出: 4
15+
解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。
16+
```
17+
18+
## 解题思路
19+
20+
使用暴力法,一次判断2-n中质数的个数。
21+
22+
这里使用排除法,例如循环到 2 的时候,将所有2的倍数的数排除,以此类推。
23+
24+
代码如下:
25+
26+
```javascript
27+
var countPrimes = function(n) {
28+
let count = 0;
29+
let signs = new Uint8Array(n);
30+
for (let i = 2; i < n; i++) {
31+
if (!signs[i - 1]) {
32+
count++;
33+
for (let j = i * i; j <= n; j += i) {
34+
signs[j - 1] = true;
35+
}
36+
}
37+
}
38+
return count;
39+
};
40+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# No.581 最短无序连续子数组
2+
3+
难度:`easy`
4+
5+
给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序。
6+
7+
你找到的子数组应是最短的,请输出它的长度。
8+
9+
10+
## 示例
11+
12+
示例 1:
13+
14+
输入: [2, 6, 4, 8, 10, 9, 15]
15+
输出: 5
16+
解释: 你只需要对 [6, 4, 8, 10, 9] 进行升序排序,那么整个表都会变为升序排序。
17+
说明 :
18+
19+
输入的数组长度范围在 [1, 10,000]
20+
输入的数组可能包含重复元素 ,所以升序的意思是<=。
21+
22+
## 解题思路
23+
24+
复制一份数组,将数组排序,然后比较两个数组,记录不同值的 index。
25+
26+
代码如下:
27+
28+
```javascript
29+
/**
30+
* @param {number[]} nums
31+
* @return {number}
32+
*/
33+
var findUnsortedSubarray = function(nums) {
34+
let arr = [...nums];
35+
let firstIdx = lastIdx = 0;
36+
nums.sort(function(a,b){return a-b});
37+
for (let i = 0, n = nums.length; i < n; i++) {
38+
if (arr[i] != nums[i]) {
39+
firstIdx = i;
40+
break;
41+
}
42+
}
43+
for (let i = nums.length-1; i > 0; i--) {
44+
if (arr[i] != nums[i]) {
45+
lastIdx = i;
46+
break;
47+
}
48+
}
49+
return lastIdx == firstIdx ? 0 : lastIdx-firstIdx +1;
50+
};
51+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /