Merge branch 'master' of https://github.com/CyC2018/CS-Notes
This commit is contained in:
commit
eeb8e2a2c5
@ -1,4 +1,5 @@
|
|||||||
<!-- GFM-TOC -->
|
<!-- GFM-TOC -->
|
||||||
|
* [0. 原理](#0-原理)
|
||||||
* [1. 统计两个数的二进制表示有多少位不同](#1-统计两个数的二进制表示有多少位不同)
|
* [1. 统计两个数的二进制表示有多少位不同](#1-统计两个数的二进制表示有多少位不同)
|
||||||
* [2. 数组中唯一一个不重复的元素](#2-数组中唯一一个不重复的元素)
|
* [2. 数组中唯一一个不重复的元素](#2-数组中唯一一个不重复的元素)
|
||||||
* [3. 找出数组中缺失的那个数](#3-找出数组中缺失的那个数)
|
* [3. 找出数组中缺失的那个数](#3-找出数组中缺失的那个数)
|
||||||
@ -15,6 +16,8 @@
|
|||||||
<!-- GFM-TOC -->
|
<!-- GFM-TOC -->
|
||||||
|
|
||||||
|
|
||||||
|
# 0. 原理
|
||||||
|
|
||||||
**基本原理**
|
**基本原理**
|
||||||
|
|
||||||
0s 表示一串 0,1s 表示一串 1。
|
0s 表示一串 0,1s 表示一串 1。
|
||||||
@ -25,21 +28,77 @@ x ^ 1s = ~x x & 1s = x x | 1s = 1s
|
|||||||
x ^ x = 0 x & x = x x | x = x
|
x ^ x = 0 x & x = x x | x = x
|
||||||
```
|
```
|
||||||
|
|
||||||
- 利用 x ^ 1s = \~x 的特点,可以将位级表示翻转;利用 x ^ x = 0 的特点,可以将三个数中重复的两个数去除,只留下另一个数。
|
利用 x ^ 1s = \~x 的特点,可以将一个数的位级表示翻转;利用 x ^ x = 0 的特点,可以将三个数中重复的两个数去除,只留下另一个数。
|
||||||
- 利用 x & 0s = 0 和 x & 1s = x 的特点,可以实现掩码操作。一个数 num 与 mask:00111100 进行位与操作,只保留 num 中与 mask 的 1 部分相对应的位。
|
|
||||||
- 利用 x | 0s = x 和 x | 1s = 1s 的特点,可以实现设值操作。一个数 num 与 mask:00111100 进行位或操作,将 num 中与 mask 的 1 部分相对应的位都设置为 1。
|
|
||||||
|
|
||||||
位与运算技巧:
|
```
|
||||||
|
1^1^2 = 2
|
||||||
|
```
|
||||||
|
|
||||||
- n&(n-1) 去除 n 的位级表示中最低的那一位。例如对于二进制表示 10110100,减去 1 得到 10110011,这两个数相与得到 10110000。
|
利用 x & 0s = 0 和 x & 1s = x 的特点,可以实现掩码操作。一个数 num 与 mask:00111100 进行位与操作,只保留 num 中与 mask 的 1 部分相对应的位。
|
||||||
- n&(-n) 得到 n 的位级表示中最低的那一位。-n 得到 n 的反码加 1,对于二进制表示 10110100,-n 得到 01001100,相与得到 00000100。
|
|
||||||
- n-n&(\~n+1) 去除 n 的位级表示中最高的那一位。
|
|
||||||
|
|
||||||
移位运算:
|
```
|
||||||
|
01011011 &
|
||||||
|
00111100
|
||||||
|
--------
|
||||||
|
00011000
|
||||||
|
```
|
||||||
|
|
||||||
- \>\> n 为算术右移,相当于除以 2<sup>n</sup>;
|
利用 x | 0s = x 和 x | 1s = 1s 的特点,可以实现设值操作。一个数 num 与 mask:00111100 进行位或操作,将 num 中与 mask 的 1 部分相对应的位都设置为 1。
|
||||||
- \>\>\> n 为无符号右移,左边会补上 0。
|
|
||||||
- << n 为算术左移,相当于乘以 2<sup>n</sup>。
|
```
|
||||||
|
01011011 |
|
||||||
|
00111100
|
||||||
|
--------
|
||||||
|
01111111
|
||||||
|
```
|
||||||
|
|
||||||
|
**位与运算技巧**
|
||||||
|
|
||||||
|
n&(n-1) 去除 n 的位级表示中最低的那一位 1。例如对于二进制表示 01011011,减去 1 得到 01011010,这两个数相与得到 01011010。
|
||||||
|
|
||||||
|
```
|
||||||
|
01011011 &
|
||||||
|
01011010
|
||||||
|
--------
|
||||||
|
01011010
|
||||||
|
```
|
||||||
|
|
||||||
|
n&(-n) 得到 n 的位级表示中最低的那一位 1。-n 得到 n 的反码加 1,也就是 -n=\~n+1。例如对于二进制表示 10110100,-n 得到 01001100,相与得到 00000100。
|
||||||
|
|
||||||
|
```
|
||||||
|
10110100 &
|
||||||
|
01001100
|
||||||
|
--------
|
||||||
|
00000100
|
||||||
|
```
|
||||||
|
|
||||||
|
n-(n&(-n)) 则可以去除 n 的位级表示中最低的那一位 1,和 n&(n-1) 效果一样。
|
||||||
|
|
||||||
|
**移位运算**
|
||||||
|
|
||||||
|
\>\> n 为算术右移,相当于除以 2<sup>n</sup>,例如 -7 >> 2 = -2。
|
||||||
|
|
||||||
|
```
|
||||||
|
11111111111111111111111111111001 >> 2
|
||||||
|
--------
|
||||||
|
11111111111111111111111111111110
|
||||||
|
```
|
||||||
|
|
||||||
|
\>\>\> n 为无符号右移,左边会补上 0。例如 -7 >>> 2 = 1073741822。
|
||||||
|
|
||||||
|
```
|
||||||
|
11111111111111111111111111111001 >>> 2
|
||||||
|
--------
|
||||||
|
00111111111111111111111111111111
|
||||||
|
```
|
||||||
|
|
||||||
|
<< n 为算术左移,相当于乘以 2<sup>n</sup>。-7 << 2 = -28。
|
||||||
|
|
||||||
|
```
|
||||||
|
11111111111111111111111111111001 << 2
|
||||||
|
--------
|
||||||
|
11111111111111111111111111100100
|
||||||
|
```
|
||||||
|
|
||||||
**mask 计算**
|
**mask 计算**
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@
|
|||||||
|
|
||||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/95903878-725b-4ed9-bded-bc4aae0792a9.jpg"/> </div><br>
|
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/95903878-725b-4ed9-bded-bc4aae0792a9.jpg"/> </div><br>
|
||||||
|
|
||||||
广度优先搜索一层一层地进行遍历,每层遍历都以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。
|
广度优先搜索一层一层地进行遍历,每层遍历都是以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。
|
||||||
|
|
||||||
第一层:
|
第一层:
|
||||||
|
|
||||||
@ -76,6 +76,9 @@
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
public int shortestPathBinaryMatrix(int[][] grids) {
|
public int shortestPathBinaryMatrix(int[][] grids) {
|
||||||
|
if (grids == null || grids.length == 0 || grids[0].length == 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
int[][] direction = {{1, -1}, {1, 0}, {1, 1}, {0, -1}, {0, 1}, {-1, -1}, {-1, 0}, {-1, 1}};
|
int[][] direction = {{1, -1}, {1, 0}, {1, 1}, {0, -1}, {0, 1}, {-1, -1}, {-1, 0}, {-1, 1}};
|
||||||
int m = grids.length, n = grids[0].length;
|
int m = grids.length, n = grids[0].length;
|
||||||
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
||||||
@ -87,15 +90,18 @@ public int shortestPathBinaryMatrix(int[][] grids) {
|
|||||||
while (size-- > 0) {
|
while (size-- > 0) {
|
||||||
Pair<Integer, Integer> cur = queue.poll();
|
Pair<Integer, Integer> cur = queue.poll();
|
||||||
int cr = cur.getKey(), cc = cur.getValue();
|
int cr = cur.getKey(), cc = cur.getValue();
|
||||||
|
if (grids[cr][cc] == 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (cr == m - 1 && cc == n - 1) {
|
||||||
|
return pathLength;
|
||||||
|
}
|
||||||
grids[cr][cc] = 1; // 标记
|
grids[cr][cc] = 1; // 标记
|
||||||
for (int[] d : direction) {
|
for (int[] d : direction) {
|
||||||
int nr = cr + d[0], nc = cc + d[1];
|
int nr = cr + d[0], nc = cc + d[1];
|
||||||
if (nr < 0 || nr >= m || nc < 0 || nc >= n || grids[nr][nc] == 1) {
|
if (nr < 0 || nr >= m || nc < 0 || nc >= n) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (nr == m - 1 && nc == n - 1) {
|
|
||||||
return pathLength + 1;
|
|
||||||
}
|
|
||||||
queue.add(new Pair<>(nr, nc));
|
queue.add(new Pair<>(nr, nc));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -827,7 +827,7 @@ https://leetcode.com/problems/rank-scores/description/
|
|||||||
| 2 | 4.2 | 2 | 2 |
|
| 2 | 4.2 | 2 | 2 |
|
||||||
| 3 | 4.3 | 1 | 1 |
|
| 3 | 4.3 | 1 | 1 |
|
||||||
|
|
||||||
使用连接操作找到某个 score 对应的大于其值的记录:
|
使用连接操作找到某个 score 对应的大于等于其值的记录:
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
SELECT
|
SELECT
|
||||||
@ -890,7 +890,7 @@ ORDER BY
|
|||||||
| score | Rank |
|
| score | Rank |
|
||||||
| :---: | :--: |
|
| :---: | :--: |
|
||||||
| 4.2 | 1 |
|
| 4.2 | 1 |
|
||||||
| 4.2 | 2 |
|
| 4.2 | 1 |
|
||||||
| 4.1 | 2 |
|
| 4.1 | 2 |
|
||||||
|
|
||||||
连接情况如下:
|
连接情况如下:
|
||||||
|
@ -1021,7 +1021,7 @@ g/re/p(globally search a regular expression and print),使用正则表示式
|
|||||||
|
|
||||||
```html
|
```html
|
||||||
$ grep [-acinv] [--color=auto] 搜寻字符串 filename
|
$ grep [-acinv] [--color=auto] 搜寻字符串 filename
|
||||||
-c : 统计个数
|
-c : 统计匹配到行的个数
|
||||||
-i : 忽略大小写
|
-i : 忽略大小写
|
||||||
-n : 输出行号
|
-n : 输出行号
|
||||||
-v : 反向选择,也就是显示出没有 搜寻字符串 内容的那一行
|
-v : 反向选择,也就是显示出没有 搜寻字符串 内容的那一行
|
||||||
@ -1039,7 +1039,7 @@ $ grep -n 'the' regular_express.txt
|
|||||||
18:google is the best tools for search keyword
|
18:google is the best tools for search keyword
|
||||||
```
|
```
|
||||||
|
|
||||||
示例:正则表达式 a{m,n} 用来匹配字符 a m\~n 次,这里需要将 { 和 } 进行转移,因为它们在 shell 是有特殊意义的。
|
示例:正则表达式 a{m,n} 用来匹配字符 a m\~n 次,这里需要将 { 和 } 进行转义,因为它们在 shell 是有特殊意义的。
|
||||||
|
|
||||||
```html
|
```html
|
||||||
$ grep -n 'a\{2,5\}' regular_express.txt
|
$ grep -n 'a\{2,5\}' regular_express.txt
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
|
# 前言
|
||||||
|
|
||||||
|
题目来自《何海涛. 剑指 Offer[M]. 电子工业出版社, 2012.》,刷题网站推荐:
|
||||||
|
|
||||||
|
- [牛客网](https://www.nowcoder.com/ta/coding-interviews?from=cyc_github)
|
||||||
|
- [Leetcode](https://leetcode-cn.com/problemset/lcof/)
|
||||||
|
|
||||||
# 目录
|
# 目录
|
||||||
|
|
||||||
|
|
||||||
@ -78,10 +85,6 @@
|
|||||||
- [67. 把字符串转换成整数](67.%20把字符串转换成整数.md)
|
- [67. 把字符串转换成整数](67.%20把字符串转换成整数.md)
|
||||||
- [68. 树中两个节点的最低公共祖先](68.%20树中两个节点的最低公共祖先.md)
|
- [68. 树中两个节点的最低公共祖先](68.%20树中两个节点的最低公共祖先.md)
|
||||||
|
|
||||||
# 参考文献
|
|
||||||
|
|
||||||
何海涛. 剑指 Offer[M]. 电子工业出版社, 2012.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -73,6 +73,7 @@
|
|||||||
```
|
```
|
||||||
|
|
||||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/eb859228-c0f2-4bce-910d-d9f76929352b.png"/> </div><br>
|
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/eb859228-c0f2-4bce-910d-d9f76929352b.png"/> </div><br>
|
||||||
|
|
||||||
## 3. 最近未使用
|
## 3. 最近未使用
|
||||||
|
|
||||||
> NRU, Not Recently Used
|
> NRU, Not Recently Used
|
||||||
|
@ -22,19 +22,52 @@ Output:
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
public int StrToInt(String str) {
|
public int StrToInt(String str) {
|
||||||
if (str == null || str.length() == 0)
|
if (str == null)
|
||||||
return 0;
|
return 0;
|
||||||
boolean isNegative = str.charAt(0) == '-';
|
int result = 0;
|
||||||
int ret = 0;
|
boolean negative = false;//是否负数
|
||||||
for (int i = 0; i < str.length(); i++) {
|
int i = 0, len = str.length();
|
||||||
char c = str.charAt(i);
|
/**
|
||||||
if (i == 0 && (c == '+' || c == '-')) /* 符号判定 */
|
* limit 默认初始化为*负的*最大正整数 ,假如字符串表示的是正数
|
||||||
continue;
|
* 由于int的范围为-2147483648~2147483647
|
||||||
if (c < '0' || c > '9') /* 非法输入 */
|
* 那么result(在返回之前一直是负数形式)就必须和这个最大正数的负数来比较来判断是否溢出,
|
||||||
|
*/
|
||||||
|
int limit = - Integer.MAX_VALUE;
|
||||||
|
int multmin;
|
||||||
|
int digit;
|
||||||
|
|
||||||
|
if (len > 0) {
|
||||||
|
char firstChar = str.charAt(0);//首先看第一位
|
||||||
|
if (firstChar < '0') { // 有可能是 "+" or "-"
|
||||||
|
if (firstChar == '-') {
|
||||||
|
negative = true;
|
||||||
|
limit = Integer.MIN_VALUE;//在负号的情况下,判断溢出的值就变成了 整数的 最小负数了
|
||||||
|
} else if (firstChar != '+')//第一位不是数字和-只能是+
|
||||||
return 0;
|
return 0;
|
||||||
ret = ret * 10 + (c - '0');
|
if (len == 1) // Cannot have lone "+" or "-"
|
||||||
|
return 0;
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
return isNegative ? -ret : ret;
|
multmin = limit / 10;
|
||||||
|
while (i < len) {
|
||||||
|
digit = str.charAt(i++)-'0';
|
||||||
|
if (digit < 0 || digit > 9)
|
||||||
|
return 0;
|
||||||
|
//判断溢出
|
||||||
|
if (result < multmin) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
result *= 10;
|
||||||
|
if (result < limit + digit) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
result -= digit;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
//如果是正数就返回-result(result一直是负数)
|
||||||
|
return negative ? result : -result;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1432,8 +1432,9 @@ Java 注解是附加在代码中的一些元信息,用于一些工具在编译
|
|||||||
|
|
||||||
## JRE or JDK
|
## JRE or JDK
|
||||||
|
|
||||||
- JRE is the JVM program, Java application need to run on JRE.
|
- JRE:Java Runtime Environment,java运行环境的简称,为java的运行提供了所需的环境。主要包括了JVM的标准实现和一些java基本类库。
|
||||||
- JDK is a superset of JRE, JRE + tools for developing java programs. e.g, it provides the compiler "javac"
|
- JDK:Java Development Kit,java开发工具包,提供了java的开发及运行环境。JDK是java开发的核心,集成了JRE以及一些其他的工具,比如编译 java 源码的编译器 javac等。
|
||||||
|
- 因此可以这样认为:JDK>JRE>JVM,JRE支持了java程序的运行,而JDK则同时支持了java程序的开发。
|
||||||
|
|
||||||
# 参考资料
|
# 参考资料
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
<!-- GFM-TOC -->
|
<!-- GFM-TOC -->
|
||||||
|
* [0. 原理](#0-原理)
|
||||||
* [1. 统计两个数的二进制表示有多少位不同](#1-统计两个数的二进制表示有多少位不同)
|
* [1. 统计两个数的二进制表示有多少位不同](#1-统计两个数的二进制表示有多少位不同)
|
||||||
* [2. 数组中唯一一个不重复的元素](#2-数组中唯一一个不重复的元素)
|
* [2. 数组中唯一一个不重复的元素](#2-数组中唯一一个不重复的元素)
|
||||||
* [3. 找出数组中缺失的那个数](#3-找出数组中缺失的那个数)
|
* [3. 找出数组中缺失的那个数](#3-找出数组中缺失的那个数)
|
||||||
@ -15,6 +16,8 @@
|
|||||||
<!-- GFM-TOC -->
|
<!-- GFM-TOC -->
|
||||||
|
|
||||||
|
|
||||||
|
# 0. 原理
|
||||||
|
|
||||||
**基本原理**
|
**基本原理**
|
||||||
|
|
||||||
0s 表示一串 0,1s 表示一串 1。
|
0s 表示一串 0,1s 表示一串 1。
|
||||||
@ -25,21 +28,77 @@ x ^ 1s = ~x x & 1s = x x | 1s = 1s
|
|||||||
x ^ x = 0 x & x = x x | x = x
|
x ^ x = 0 x & x = x x | x = x
|
||||||
```
|
```
|
||||||
|
|
||||||
- 利用 x ^ 1s = \~x 的特点,可以将位级表示翻转;利用 x ^ x = 0 的特点,可以将三个数中重复的两个数去除,只留下另一个数。
|
利用 x ^ 1s = \~x 的特点,可以将一个数的位级表示翻转;利用 x ^ x = 0 的特点,可以将三个数中重复的两个数去除,只留下另一个数。
|
||||||
- 利用 x & 0s = 0 和 x & 1s = x 的特点,可以实现掩码操作。一个数 num 与 mask:00111100 进行位与操作,只保留 num 中与 mask 的 1 部分相对应的位。
|
|
||||||
- 利用 x | 0s = x 和 x | 1s = 1s 的特点,可以实现设值操作。一个数 num 与 mask:00111100 进行位或操作,将 num 中与 mask 的 1 部分相对应的位都设置为 1。
|
|
||||||
|
|
||||||
位与运算技巧:
|
```
|
||||||
|
1^1^2 = 2
|
||||||
|
```
|
||||||
|
|
||||||
- n&(n-1) 去除 n 的位级表示中最低的那一位。例如对于二进制表示 10110100,减去 1 得到 10110011,这两个数相与得到 10110000。
|
利用 x & 0s = 0 和 x & 1s = x 的特点,可以实现掩码操作。一个数 num 与 mask:00111100 进行位与操作,只保留 num 中与 mask 的 1 部分相对应的位。
|
||||||
- n&(-n) 得到 n 的位级表示中最低的那一位。-n 得到 n 的反码加 1,对于二进制表示 10110100,-n 得到 01001100,相与得到 00000100。
|
|
||||||
- n-n&(\~n+1) 去除 n 的位级表示中最高的那一位。
|
|
||||||
|
|
||||||
移位运算:
|
```
|
||||||
|
01011011 &
|
||||||
|
00111100
|
||||||
|
--------
|
||||||
|
00011000
|
||||||
|
```
|
||||||
|
|
||||||
- \>\> n 为算术右移,相当于除以 2<sup>n</sup>;
|
利用 x | 0s = x 和 x | 1s = 1s 的特点,可以实现设值操作。一个数 num 与 mask:00111100 进行位或操作,将 num 中与 mask 的 1 部分相对应的位都设置为 1。
|
||||||
- \>\>\> n 为无符号右移,左边会补上 0。
|
|
||||||
- << n 为算术左移,相当于乘以 2<sup>n</sup>。
|
```
|
||||||
|
01011011 |
|
||||||
|
00111100
|
||||||
|
--------
|
||||||
|
01111111
|
||||||
|
```
|
||||||
|
|
||||||
|
**位与运算技巧**
|
||||||
|
|
||||||
|
n&(n-1) 去除 n 的位级表示中最低的那一位 1。例如对于二进制表示 01011011,减去 1 得到 01011010,这两个数相与得到 01011010。
|
||||||
|
|
||||||
|
```
|
||||||
|
01011011 &
|
||||||
|
01011010
|
||||||
|
--------
|
||||||
|
01011010
|
||||||
|
```
|
||||||
|
|
||||||
|
n&(-n) 得到 n 的位级表示中最低的那一位 1。-n 得到 n 的反码加 1,也就是 -n=\~n+1。例如对于二进制表示 10110100,-n 得到 01001100,相与得到 00000100。
|
||||||
|
|
||||||
|
```
|
||||||
|
10110100 &
|
||||||
|
01001100
|
||||||
|
--------
|
||||||
|
00000100
|
||||||
|
```
|
||||||
|
|
||||||
|
n-(n&(-n)) 则可以去除 n 的位级表示中最低的那一位 1,和 n&(n-1) 效果一样。
|
||||||
|
|
||||||
|
**移位运算**
|
||||||
|
|
||||||
|
\>\> n 为算术右移,相当于除以 2<sup>n</sup>,例如 -7 >> 2 = -2。
|
||||||
|
|
||||||
|
```
|
||||||
|
11111111111111111111111111111001 >> 2
|
||||||
|
--------
|
||||||
|
11111111111111111111111111111110
|
||||||
|
```
|
||||||
|
|
||||||
|
\>\>\> n 为无符号右移,左边会补上 0。例如 -7 >>> 2 = 1073741822。
|
||||||
|
|
||||||
|
```
|
||||||
|
11111111111111111111111111111001 >>> 2
|
||||||
|
--------
|
||||||
|
00111111111111111111111111111111
|
||||||
|
```
|
||||||
|
|
||||||
|
<< n 为算术左移,相当于乘以 2<sup>n</sup>。-7 << 2 = -28。
|
||||||
|
|
||||||
|
```
|
||||||
|
11111111111111111111111111111001 << 2
|
||||||
|
--------
|
||||||
|
11111111111111111111111111100100
|
||||||
|
```
|
||||||
|
|
||||||
**mask 计算**
|
**mask 计算**
|
||||||
|
|
||||||
|
@ -139,7 +139,7 @@ private int rob(int[] nums, int first, int last) {
|
|||||||
|
|
||||||
## 4. 信件错排
|
## 4. 信件错排
|
||||||
|
|
||||||
题目描述:有 N 个 信 和 信封,它们被打乱,求错误装信方式的数量。
|
题目描述:有 N 个 信 和 信封,它们被打乱,求错误装信方式的数量(所有信封都没有装各自的信)。
|
||||||
|
|
||||||
定义一个数组 dp 存储错误方式数量,dp[i] 表示前 i 个信和信封的错误方式数量。假设第 i 个信装到第 j 个信封里面,而第 j 个信装到第 k 个信封里面。根据 i 和 k 是否相等,有两种情况:
|
定义一个数组 dp 存储错误方式数量,dp[i] 表示前 i 个信和信封的错误方式数量。假设第 i 个信装到第 j 个信封里面,而第 j 个信装到第 k 个信封里面。根据 i 和 k 是否相等,有两种情况:
|
||||||
|
|
||||||
@ -1055,7 +1055,10 @@ public int combinationSum4(int[] nums, int target) {
|
|||||||
|
|
||||||
题目描述:交易之后需要有一天的冷却时间。
|
题目描述:交易之后需要有一天的冷却时间。
|
||||||
|
|
||||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/ffd96b99-8009-487c-8e98-11c9d44ef14f.png" width="300px"> </div><br>
|
|
||||||
|
该题为马尔可夫过程,分为A观望,B持股,C冷却三个状态
|
||||||
|
状态转移图:A-(观望)->A, A-(买入|-price)->B, B-(观望)->B, B-(卖出|+price)->C, C-(冷却)->A
|
||||||
|
可用维特比算法求解
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public int maxProfit(int[] prices) {
|
public int maxProfit(int[] prices) {
|
||||||
@ -1063,19 +1066,17 @@ public int maxProfit(int[] prices) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int N = prices.length;
|
int N = prices.length;
|
||||||
int[] buy = new int[N];
|
int[] A = new int[N];
|
||||||
int[] s1 = new int[N];
|
int[] B = new int[N];
|
||||||
int[] sell = new int[N];
|
int[] C = new int[N];
|
||||||
int[] s2 = new int[N];
|
A[0] = 0;
|
||||||
s1[0] = buy[0] = -prices[0];
|
B[0] = C[0] = -prices[0];
|
||||||
sell[0] = s2[0] = 0;
|
|
||||||
for (int i = 1; i < N; i++) {
|
for (int i = 1; i < N; i++) {
|
||||||
buy[i] = s2[i - 1] - prices[i];
|
A[i] = Math.max(A[i - 1], C[i - 1]);
|
||||||
s1[i] = Math.max(buy[i - 1], s1[i - 1]);
|
B[i] = Math.max(B[i - 1], A[i - 1] - prices[i]);
|
||||||
sell[i] = Math.max(buy[i - 1], s1[i - 1]) + prices[i];
|
C[i] = B[i - 1] + prices[i];
|
||||||
s2[i] = Math.max(s2[i - 1], sell[i - 1]);
|
|
||||||
}
|
}
|
||||||
return Math.max(sell[N - 1], s2[N - 1]);
|
return Math.max(A[N - 1], C[N - 1]);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -1098,24 +1099,22 @@ The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
|
|||||||
|
|
||||||
题目描述:每交易一次,都要支付一定的费用。
|
题目描述:每交易一次,都要支付一定的费用。
|
||||||
|
|
||||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/1e2c588c-72b7-445e-aacb-d55dc8a88c29.png" width="300px"> </div><br>
|
|
||||||
|
分为A观望,B持股,两个状态
|
||||||
|
状态转移图:A-(观望)->A, A-(买入|-price)->B, B-(观望)->B, B-(卖出|+price|-fee)->A
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public int maxProfit(int[] prices, int fee) {
|
public int maxProfit(int[] prices, int fee) {
|
||||||
int N = prices.length;
|
int N = prices.length;
|
||||||
int[] buy = new int[N];
|
int[] A = new int[N];
|
||||||
int[] s1 = new int[N];
|
int[] B = new int[N];
|
||||||
int[] sell = new int[N];
|
A[0] = 0;
|
||||||
int[] s2 = new int[N];
|
B[0] = -prices[0];
|
||||||
s1[0] = buy[0] = -prices[0];
|
|
||||||
sell[0] = s2[0] = 0;
|
|
||||||
for (int i = 1; i < N; i++) {
|
for (int i = 1; i < N; i++) {
|
||||||
buy[i] = Math.max(sell[i - 1], s2[i - 1]) - prices[i];
|
A[i] = Math.max(A[i - 1], B[i - 1] + prices[i] -fee);
|
||||||
s1[i] = Math.max(buy[i - 1], s1[i - 1]);
|
B[i] = Math.max(A[i - 1] - prices[i], B[i - 1]);
|
||||||
sell[i] = Math.max(buy[i - 1], s1[i - 1]) - fee + prices[i];
|
|
||||||
s2[i] = Math.max(s2[i - 1], sell[i - 1]);
|
|
||||||
}
|
}
|
||||||
return Math.max(sell[N - 1], s2[N - 1]);
|
return A[N - 1];
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@
|
|||||||
|
|
||||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/95903878-725b-4ed9-bded-bc4aae0792a9.jpg"/> </div><br>
|
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/95903878-725b-4ed9-bded-bc4aae0792a9.jpg"/> </div><br>
|
||||||
|
|
||||||
广度优先搜索一层一层地进行遍历,每层遍历都以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。
|
广度优先搜索一层一层地进行遍历,每层遍历都是以上一层遍历的结果作为起点,遍历一个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。
|
||||||
|
|
||||||
第一层:
|
第一层:
|
||||||
|
|
||||||
@ -76,6 +76,9 @@
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
public int shortestPathBinaryMatrix(int[][] grids) {
|
public int shortestPathBinaryMatrix(int[][] grids) {
|
||||||
|
if (grids == null || grids.length == 0 || grids[0].length == 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
int[][] direction = {{1, -1}, {1, 0}, {1, 1}, {0, -1}, {0, 1}, {-1, -1}, {-1, 0}, {-1, 1}};
|
int[][] direction = {{1, -1}, {1, 0}, {1, 1}, {0, -1}, {0, 1}, {-1, -1}, {-1, 0}, {-1, 1}};
|
||||||
int m = grids.length, n = grids[0].length;
|
int m = grids.length, n = grids[0].length;
|
||||||
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
||||||
@ -87,15 +90,18 @@ public int shortestPathBinaryMatrix(int[][] grids) {
|
|||||||
while (size-- > 0) {
|
while (size-- > 0) {
|
||||||
Pair<Integer, Integer> cur = queue.poll();
|
Pair<Integer, Integer> cur = queue.poll();
|
||||||
int cr = cur.getKey(), cc = cur.getValue();
|
int cr = cur.getKey(), cc = cur.getValue();
|
||||||
|
if (grids[cr][cc] == 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (cr == m - 1 && cc == n - 1) {
|
||||||
|
return pathLength;
|
||||||
|
}
|
||||||
grids[cr][cc] = 1; // 标记
|
grids[cr][cc] = 1; // 标记
|
||||||
for (int[] d : direction) {
|
for (int[] d : direction) {
|
||||||
int nr = cr + d[0], nc = cc + d[1];
|
int nr = cr + d[0], nc = cc + d[1];
|
||||||
if (nr < 0 || nr >= m || nc < 0 || nc >= n || grids[nr][nc] == 1) {
|
if (nr < 0 || nr >= m || nc < 0 || nc >= n) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (nr == m - 1 && nc == n - 1) {
|
|
||||||
return pathLength + 1;
|
|
||||||
}
|
|
||||||
queue.add(new Pair<>(nr, nc));
|
queue.add(new Pair<>(nr, nc));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -827,7 +827,7 @@ https://leetcode.com/problems/rank-scores/description/
|
|||||||
| 2 | 4.2 | 2 | 2 |
|
| 2 | 4.2 | 2 | 2 |
|
||||||
| 3 | 4.3 | 1 | 1 |
|
| 3 | 4.3 | 1 | 1 |
|
||||||
|
|
||||||
使用连接操作找到某个 score 对应的大于其值的记录:
|
使用连接操作找到某个 score 对应的大于等于其值的记录:
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
SELECT
|
SELECT
|
||||||
@ -890,7 +890,7 @@ ORDER BY
|
|||||||
| score | Rank |
|
| score | Rank |
|
||||||
| :---: | :--: |
|
| :---: | :--: |
|
||||||
| 4.2 | 1 |
|
| 4.2 | 1 |
|
||||||
| 4.2 | 2 |
|
| 4.2 | 1 |
|
||||||
| 4.1 | 2 |
|
| 4.1 | 2 |
|
||||||
|
|
||||||
连接情况如下:
|
连接情况如下:
|
||||||
|
@ -1021,7 +1021,7 @@ g/re/p(globally search a regular expression and print),使用正则表示式
|
|||||||
|
|
||||||
```html
|
```html
|
||||||
$ grep [-acinv] [--color=auto] 搜寻字符串 filename
|
$ grep [-acinv] [--color=auto] 搜寻字符串 filename
|
||||||
-c : 统计个数
|
-c : 统计匹配到行的个数
|
||||||
-i : 忽略大小写
|
-i : 忽略大小写
|
||||||
-n : 输出行号
|
-n : 输出行号
|
||||||
-v : 反向选择,也就是显示出没有 搜寻字符串 内容的那一行
|
-v : 反向选择,也就是显示出没有 搜寻字符串 内容的那一行
|
||||||
@ -1039,7 +1039,7 @@ $ grep -n 'the' regular_express.txt
|
|||||||
18:google is the best tools for search keyword
|
18:google is the best tools for search keyword
|
||||||
```
|
```
|
||||||
|
|
||||||
示例:正则表达式 a{m,n} 用来匹配字符 a m\~n 次,这里需要将 { 和 } 进行转移,因为它们在 shell 是有特殊意义的。
|
示例:正则表达式 a{m,n} 用来匹配字符 a m\~n 次,这里需要将 { 和 } 进行转义,因为它们在 shell 是有特殊意义的。
|
||||||
|
|
||||||
```html
|
```html
|
||||||
$ grep -n 'a\{2,5\}' regular_express.txt
|
$ grep -n 'a\{2,5\}' regular_express.txt
|
||||||
|
@ -1,3 +1,10 @@
|
|||||||
|
# 前言
|
||||||
|
|
||||||
|
题目来自《何海涛. 剑指 Offer[M]. 电子工业出版社, 2012.》,刷题网站推荐:
|
||||||
|
|
||||||
|
- [牛客网](https://www.nowcoder.com/ta/coding-interviews?from=cyc_github)
|
||||||
|
- [Leetcode](https://leetcode-cn.com/problemset/lcof/)
|
||||||
|
|
||||||
# 目录
|
# 目录
|
||||||
|
|
||||||
|
|
||||||
@ -78,10 +85,6 @@
|
|||||||
- [67. 把字符串转换成整数](67.%20把字符串转换成整数.md)
|
- [67. 把字符串转换成整数](67.%20把字符串转换成整数.md)
|
||||||
- [68. 树中两个节点的最低公共祖先](68.%20树中两个节点的最低公共祖先.md)
|
- [68. 树中两个节点的最低公共祖先](68.%20树中两个节点的最低公共祖先.md)
|
||||||
|
|
||||||
# 参考文献
|
|
||||||
|
|
||||||
何海涛. 剑指 Offer[M]. 电子工业出版社, 2012.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -73,6 +73,7 @@
|
|||||||
```
|
```
|
||||||
|
|
||||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/eb859228-c0f2-4bce-910d-d9f76929352b.png"/> </div><br>
|
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/eb859228-c0f2-4bce-910d-d9f76929352b.png"/> </div><br>
|
||||||
|
|
||||||
## 3. 最近未使用
|
## 3. 最近未使用
|
||||||
|
|
||||||
> NRU, Not Recently Used
|
> NRU, Not Recently Used
|
||||||
|
Loading…
x
Reference in New Issue
Block a user