Skip to content

Commit 889f664

Browse files
committed
auto commit
1 parent fc2f1a0 commit 889f664

File tree

4 files changed

+122
-81
lines changed

4 files changed

+122
-81
lines changed

notes/Leetcode 题解.md

Lines changed: 110 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ public int findContentChildren(int[] g, int[] s) {
349349
Arrays.sort(s);
350350
int gIndex = 0, sIndex = 0;
351351
while (gIndex < g.length && sIndex < s.length) {
352-
if (g[gIndex] <= s[sIndex]) gIndex++;
352+
if (g[gIndex] <= s[sIndex])
353+
gIndex++;
353354
sIndex++;
354355
}
355356
return gIndex;
@@ -367,11 +368,10 @@ public int findContentChildren(int[] g, int[] s) {
367368
```java
368369
public int maxProfit(int[] prices) {
369370
int profit = 0;
370-
for (int i = 1; i < prices.length; i++) {
371-
if (prices[i] > prices[i - 1]) {
371+
for (int i = 1; i < prices.length; i++)
372+
if (prices[i] > prices[i - 1])
372373
profit += (prices[i] - prices[i - 1]);
373-
}
374-
}
374+
375375
return profit;
376376
}
377377
```
@@ -389,11 +389,13 @@ Output: True
389389

390390
```java
391391
public boolean canPlaceFlowers(int[] flowerbed, int n) {
392+
int len = flowerbed.length;
392393
int cnt = 0;
393-
for (int i = 0; i < flowerbed.length; i++) {
394-
if (flowerbed[i] == 1) continue;
394+
for (int i = 0; i < len; i++) {
395+
if (flowerbed[i] == 1)
396+
continue;
395397
int pre = i == 0 ? 0 : flowerbed[i - 1];
396-
int next = i == flowerbed.length - 1 ? 0 : flowerbed[i + 1];
398+
int next = i == len - 1 ? 0 : flowerbed[i + 1];
397399
if (pre == 0 && next == 0) {
398400
cnt++;
399401
flowerbed[i] = 1;
@@ -415,17 +417,19 @@ Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
415417

416418
题目描述:判断一个数组能不能只修改一个数就成为非递减数组。
417419

418-
在出现 nums[i] < nums[i - 1] 时,需要考虑的是应该修改数组的哪个数,使得本次修改能使 i 之前的数组成为非递减数组,并且 **不影响后续的操作** 。优先考虑令 nums[i - 1] = nums[i],因为如果修改 nums[i] = nums[i - 1] 的话,那么 nums[i] 这个数会变大,就有可能比 nums[i + 1] 大,从而影响了后续操作。还有一个比较特别的情况就是 nums[i] < nums[i - 2],只修改 nums[i - 1] = nums[i] 不能令数组成为非递减,只能通过修改 nums[i] = nums[i - 1] 才行
420+
在出现 nums[i] < nums[i - 1] 时,需要考虑的是应该修改数组的哪个数,使得本次修改能使 i 之前的数组成为非递减数组,并且 **不影响后续的操作** 。优先考虑令 nums[i - 1] = nums[i],因为如果修改 nums[i] = nums[i - 1] 的话,那么 nums[i] 这个数会变大,就有可能比 nums[i + 1] 大,从而影响了后续操作。还有一个比较特别的情况就是 nums[i] < nums[i - 2],只修改 nums[i - 1] = nums[i] 不能使数组成为非递减数组,只能修改 nums[i] = nums[i - 1]
419421

420422
```java
421423
public boolean checkPossibility(int[] nums) {
422424
int cnt = 0;
423-
for (int i = 1; i < nums.length; i++) {
424-
if (nums[i] < nums[i - 1]) {
425-
cnt++;
426-
if (i - 2 >= 0 && nums[i - 2] > nums[i]) nums[i] = nums[i - 1];
427-
else nums[i - 1] = nums[i];
428-
}
425+
for (int i = 1; i < nums.length && cnt < 2; i++) {
426+
if (nums[i] >= nums[i - 1])
427+
continue;
428+
cnt++;
429+
if (i - 2 >= 0 && nums[i - 2] > nums[i])
430+
nums[i] = nums[i - 1];
431+
else
432+
nums[i - 1] = nums[i];
429433
}
430434
return cnt <= 1;
431435
}
@@ -442,15 +446,61 @@ Return true.
442446

443447
```java
444448
public boolean isSubsequence(String s, String t) {
445-
int pos = -1;
449+
int index = -1;
446450
for (char c : s.toCharArray()) {
447-
pos = t.indexOf(c, pos + 1);
448-
if (pos == -1) return false;
451+
index = t.indexOf(c, index + 1);
452+
if (index == -1)
453+
return false;
449454
}
450455
return true;
451456
}
452457
```
453458

459+
**不重叠的区间个数**
460+
461+
[435. Non-overlapping Intervals (Medium)](https://leetcode.com/problems/non-overlapping-intervals/description/)
462+
463+
```html
464+
Input: [ [1,2], [1,2], [1,2] ]
465+
466+
Output: 2
467+
468+
Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.
469+
```
470+
471+
```html
472+
Input: [ [1,2], [2,3] ]
473+
474+
Output: 0
475+
476+
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
477+
```
478+
479+
题目描述:计算让一组区间不重叠所需要移除的区间个数。
480+
481+
直接计算最多能组成的不重叠区间个数即可。
482+
483+
在每次选择中,区间的结尾最为重要,选择的区间结尾越小,留给后面的区间的空间越大,那么后面能够选择的区间个数也就越大。
484+
485+
按区间的结尾进行排序,每次选择结尾最小,并且和前一个区间不重叠的区间。
486+
487+
```java
488+
public int eraseOverlapIntervals(Interval[] intervals) {
489+
if (intervals.length == 0)
490+
return 0;
491+
Arrays.sort(intervals, Comparator.comparingInt(o -> o.end));
492+
int cnt = 1;
493+
int end = intervals[0].end;
494+
for (int i = 1; i < intervals.length; i++) {
495+
if (intervals[i].start < end)
496+
continue;
497+
end = intervals[i].end;
498+
cnt++;
499+
}
500+
return intervals.length - cnt;
501+
}
502+
```
503+
454504
**投飞镖刺破气球**
455505

456506
[452. Minimum Number of Arrows to Burst Balloons (Medium)](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
@@ -465,31 +515,27 @@ Output:
465515

466516
题目描述:气球在一个水平数轴上摆放,可以重叠,飞镖垂直投向坐标轴,使得路径上的气球都会刺破。求解最小的投飞镖次数使所有气球都被刺破。
467517

468-
对气球按末尾位置进行排序,得到:
469-
470-
```html
471-
[[1,6], [2,8], [7,12], [10,16]]
472-
```
473-
474-
如果让飞镖投向 6 这个位置,那么 [1,6][2,8] 这两个气球都会被刺破,这种方式下刺破这两个气球的投飞镖次数最少,并且后面两个气球依然可以使用这种方式来刺破。
518+
也是计算不重叠的区间个数,不过和 Non-overlapping Intervals 的区别在于,[1, 2][2, 3] 在本题中算是重叠区间。
475519

476520
```java
477521
public int findMinArrowShots(int[][] points) {
478-
if (points.length == 0) return 0;
479-
Arrays.sort(points, (a, b) -> (a[1] - b[1]));
480-
int curPos = points[0][1];
481-
int shots = 1;
522+
if (points.length == 0)
523+
return 0;
524+
525+
Arrays.sort(points, Comparator.comparingInt(o -> o[1]));
526+
527+
int cnt = 1, end = points[0][1];
482528
for (int i = 1; i < points.length; i++) {
483-
if (points[i][0] <= curPos) {
529+
if (points[i][0] <= end) // [1,2] 和 [2,3] 算重叠
484530
continue;
485-
}
486-
curPos = points[i][1];
487-
shots++;
531+
cnt++;
532+
end = points[i][1];
488533
}
489-
return shots;
534+
return cnt;
490535
}
491536
```
492537

538+
493539
**分隔字符串使同种字符出现在一起**
494540

495541
[763. Partition Labels (Medium)](https://leetcode.com/problems/partition-labels/description/)
@@ -504,25 +550,25 @@ A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits
504550
```
505551

506552
```java
507-
public List<Integer> partitionLabels(String S) {
508-
List<Integer> partitions = new ArrayList<>();
509-
int[] lastIndexs = new int[26];
510-
for (int i = 0; i < S.length(); i++) {
511-
lastIndexs[S.charAt(i) - 'a'] = i;
512-
}
513-
int firstIndex = 0;
514-
while (firstIndex < S.length()) {
515-
int lastIndex = firstIndex;
516-
for (int i = firstIndex; i < S.length() && i <= lastIndex; i++) {
517-
int index = lastIndexs[S.charAt(i) - 'a'];
518-
if (index == i) continue;
519-
if (index > lastIndex) lastIndex = index;
520-
}
521-
partitions.add(lastIndex - firstIndex + 1);
522-
firstIndex = lastIndex + 1;
523-
}
524-
return partitions;
525-
}
553+
public List<Integer> partitionLabels(String S) {
554+
int[] lastIndexs = new int[26];
555+
for (int i = 0; i < S.length(); i++)
556+
lastIndexs[S.charAt(i) - 'a'] = i;
557+
558+
List<Integer> ret = new ArrayList<>();
559+
int firstIndex = 0;
560+
while (firstIndex < S.length()) {
561+
int lastIndex = firstIndex;
562+
for (int i = firstIndex; i < S.length() && i <= lastIndex; i++) {
563+
int index = lastIndexs[S.charAt(i) - 'a'];
564+
if (index > lastIndex)
565+
lastIndex = index;
566+
}
567+
ret.add(lastIndex - firstIndex + 1);
568+
firstIndex = lastIndex + 1;
569+
}
570+
return ret;
571+
}
526572
```
527573

528574
**根据身高和序号重组队列**
@@ -545,25 +591,17 @@ Output:
545591

546592
```java
547593
public int[][] reconstructQueue(int[][] people) {
548-
if (people == null || people.length == 0 || people[0].length == 0) return new int[0][0];
549-
Arrays.sort(people, (a, b) -> {
550-
if (a[0] == b[0]) return a[1] - b[1];
551-
return b[0] - a[0];
552-
});
553-
int N = people.length;
554-
List<int[]> tmp = new ArrayList<>();
555-
for (int i = 0; i < N; i++) {
556-
int index = people[i][1];
557-
int[] p = new int[]{people[i][0], people[i][1]};
558-
tmp.add(index, p);
559-
}
560-
561-
int[][] ret = new int[N][2];
562-
for (int i = 0; i < N; i++) {
563-
ret[i][0] = tmp.get(i)[0];
564-
ret[i][1] = tmp.get(i)[1];
565-
}
566-
return ret;
594+
if (people == null || people.length == 0 || people[0].length == 0)
595+
return new int[0][0];
596+
597+
Arrays.sort(people, (a, b) -> (a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]));
598+
599+
List<int[]> queue = new ArrayList<>();
600+
601+
for (int[] p : people)
602+
queue.add(p[1], p);
603+
604+
return queue.toArray(new int[queue.size()][]);
567605
}
568606
```
569607

notes/Redis.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ Redis 没有关系型数据库中的表这一概念来将同类型的数据存
466466
# 参考资料
467467

468468
- Carlson J L. Redis in Action[J]. Media.johnwiley.com.au, 2013.
469-
- 黄健宏. Redis 设计与实现 [M]. 机械工业出版社, 2014.
469+
- [黄健宏. Redis 设计与实现 [M]. 机械工业出版社, 2014.](http://redisbook.com/index.html)
470470
- [REDIS IN ACTION](https://redislabs.com/ebook/foreword/)
471471
- [论述 Redis 和 Memcached 的差异](http://www.cnblogs.com/loveincode/p/7411911.html)
472472
- [Redis 3.0 中文版- 分片](http://wiki.jikexueyuan.com/project/redis-guide)

notes/剑指 offer 题解.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ position-4 : (0,1,2,3,2,5) // nums[i] == nums[nums[i]], exit
115115

116116
```java
117117
public boolean duplicate(int[] nums, int length, int[] duplication) {
118-
if (nums == null || length <= 0) return false;
118+
if (nums == null || length <= 0)
119+
return false;
119120
for (int i = 0; i < length; i++) {
120121
while (nums[i] != i) {
121122
if (nums[i] == nums[nums[i]]) {

notes/计算机操作系统.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!-- GFM-TOC -->
2-
* [一、 概述](#一-概述)
2+
* [一、概述](#一概述)
33
* [操作系统基本特征](#操作系统基本特征)
44
* [操作系统基本功能](#操作系统基本功能)
55
* [系统调用](#系统调用)
@@ -31,21 +31,21 @@
3131
<!-- GFM-TOC -->
3232

3333

34-
# 一、 概述
34+
# 一、概述
3535

3636
## 操作系统基本特征
3737

3838
### 1. 并发
3939

40-
并发性是指宏观上在一段时间内能同时运行多个程序,而并行性则指同一时刻能运行多个指令
40+
并发是指宏观上在一段时间内能同时运行多个程序,而并行则指同一时刻能运行多个指令
4141

4242
并行需要硬件支持,如多流水线或者多处理器。
4343

4444
操作系统通过引入进程和线程,使得程序能够并发运行。
4545

4646
### 2. 共享
4747

48-
共享是指系统中的资源可以供多个并发进程共同使用
48+
共享是指系统中的资源可以被多个并发进程共同使用
4949

5050
有两种共享方式:互斥共享和同时共享。
5151

@@ -69,15 +69,17 @@
6969

7070
### 2. 内存管理
7171

72-
内存分配、地址映射、内存保护与共享和内存扩充等功能
72+
内存分配、地址映射、内存保护与共享、内存扩充等
7373

7474
### 3. 文件管理
7575

76-
文件存储空间的管理、目录管理及文件读写管理和保护等
76+
文件存储空间的管理、目录管理、文件读写管理和保护等
7777

7878
### 4. 设备管理
7979

80-
完成用户的 I/O 请求,方便用户使用各种设备,并提高设备的利用率,主要包括缓冲管理、设备分配、设备处理和虛拟设备等功能。
80+
完成用户的 I/O 请求,方便用户使用各种设备,并提高设备的利用率。
81+
82+
主要包括缓冲管理、设备分配、设备处理、虛拟设备等。
8183

8284
## 系统调用
8385

0 commit comments

Comments
 (0)