Skip to content

Commit

Permalink
auto commit
Browse files Browse the repository at this point in the history
  • Loading branch information
CyC2018 committed May 26, 2020
1 parent 15324cf commit afcfca0
Show file tree
Hide file tree
Showing 19 changed files with 150 additions and 321 deletions.
2 changes: 1 addition & 1 deletion docs/notes/Java 基础.md
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ System.out.println(InterfaceExample.x);

使用接口:

- 需要让不相关的类都实现个方法,例如不相关的类都可以实现 Compareable 接口中的 compareTo() 方法;
- 需要让不相关的类都实现个方法,例如不相关的类都可以实现 Comparable 接口中的 compareTo() 方法;
- 需要使用多重继承。

使用抽象类:
Expand Down
2 changes: 1 addition & 1 deletion docs/notes/Java 虚拟机.md
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ public static final int value = 123;
其中解析过程在某些情况下可以在初始化阶段之后再开始,这是为了支持 Java 的动态绑定。

<div data="补充为什么可以支持动态绑定 --> <--"></div>
### 5. 初始化
### 5. 初始化

<div data="modify -->"></div>
初始化阶段才真正开始执行类中定义的 Java 程序代码。初始化阶段是虚拟机执行类构造器 &lt;clinit>() 方法的过程。在准备阶段,类变量已经赋过次系统要求的初始值,而在初始化阶段,根据程序员通过程序制定的主观计划去初始化类变量和其它资源。
Expand Down
85 changes: 13 additions & 72 deletions docs/notes/Leetcode 题解 - 位运算.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<!-- GFM-TOC -->
* [0. 原理](#0-原理)
* [1. 统计两个数的二进制表示有多少位不同](#1-统计两个数的二进制表示有多少位不同)
* [2. 数组中唯一一个不重复的元素](#2-数组中唯一一个不重复的元素)
* [3. 找出数组中缺失的那个数](#3-找出数组中缺失的那个数)
Expand All @@ -16,9 +15,7 @@
<!-- GFM-TOC -->


# 0. 原理

**基本原理**
**基本原理**

0s 表示01s 表示1

Expand All @@ -28,79 +25,23 @@ x ^ 1s = ~x x & 1s = x x | 1s = 1s
x ^ x = 0 x & x = x x | x = x
```

利用 x ^ 1s = \~x 的特点,可以将个数的位级表示翻转;利用 x ^ x = 0 的特点,可以将三个数中重复的两个数去除,只留下另个数。

```
1^1^2 = 2
```

利用 x & 0s = 0x & 1s = x 的特点,可以实现掩码操作。个数 nummask00111100 进行位与操作,只保留 num 中与 mask1 部分相对应的位。

```
01011011 &
00111100
--------
00011000
```

利用 x | 0s = xx | 1s = 1s 的特点,可以实现设值操作。个数 nummask00111100 进行位或操作,将 num 中与 mask1 部分相对应的位都设置为 1

```
01011011 |
00111100
--------
01111111
```

**位与运算技巧**
- 利用 x ^ 1s = \~x 的特点,可以将位级表示翻转;利用 x ^ x = 0 的特点,可以将三个数中重复的两个数去除,只留下另个数。
- 利用 x & 0s = 0x & 1s = x 的特点,可以实现掩码操作。个数 nummask00111100 进行位与操作,只保留 num 中与 mask1 部分相对应的位。
- 利用 x | 0s = xx | 1s = 1s 的特点,可以实现设值操作。个数 nummask00111100 进行位或操作,将 num 中与 mask1 部分相对应的位都设置为 1

n&(n-1) 去除 n 的位级表示中最低的那1。例如对于二进制表示 01011011,减去 1 得到 01011010,这两个数相与得到 01011010
位与运算技巧:

```
01011011 &
01011010
--------
01011010
```
- n&(n-1) 去除 n 的位级表示中最低的那位。例如对于二进制表示 10110100,减去 1 得到 10110011,这两个数相与得到 10110000
- n&(-n) 得到 n 的位级表示中最低的那位。-n 得到 n 的反码加 1,对于二进制表示 10110100,-n 得到 01001100,相与得到 00000100
- n-n&(\~n+1) 去除 n 的位级表示中最高的那位。

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
```

&lt;&lt; n 为算术左移,相当于乘以 2<sup>n</sup>。-7 << 2 = -28

```
11111111111111111111111111111001 << 2
--------
11111111111111111111111111100100
```
- \>\> n 为算术右移,相当于除以 2<sup>n</sup>;
- \>\>\> n 为无符号右移,左边会补上 0
- &lt;&lt; n 为算术左移,相当于乘以 2<sup>n</sup>。

**mask 计算**
** mask 计算**

要获取 111111111,将 0 取反即可,\~0

Expand Down
50 changes: 22 additions & 28 deletions docs/notes/Leetcode 题解 - 搜索.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>

广度优先搜索层地进行遍历,每层遍历都是以上层遍历的结果作为起点,遍历个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。
广度优先搜索层地进行遍历,每层遍历都以上层遍历的结果作为起点,遍历个距离能访问到的所有节点。需要注意的是,遍历过的节点不能再次被遍历。

层:

Expand Down Expand Up @@ -75,39 +75,33 @@
题目描述:0 表示可以经过某个位置,求解从左上角到右下角的最短路径长度。

```java
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 m = grids.length, n = grids[0].length;
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
queue.add(new Pair<>(0, 0));
int pathLength = 0;
while (!queue.isEmpty()) {
int size = queue.size();
pathLength++;
while (size-- > 0) {
Pair<Integer, Integer> cur = queue.poll();
int cr = cur.getKey(), cc = cur.getValue();
if (grids[cr][cc] == 1) {
public int shortestPathBinaryMatrix(int[][] grids) {
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;
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
queue.add(new Pair<>(0, 0));
int pathLength = 0;
while (!queue.isEmpty()) {
int size = queue.size();
pathLength++;
while (size-- > 0) {
Pair<Integer, Integer> cur = queue.poll();
int cr = cur.getKey(), cc = cur.getValue();
grids[cr][cc] = 1; // 标记
for (int[] d : direction) {
int nr = cr + d[0], nc = cc + d[1];
if (nr < 0 || nr >= m || nc < 0 || nc >= n || grids[nr][nc] == 1) {
continue;
}
if (cr == m - 1 && cc == n - 1) {
return pathLength;
}
grids[cr][cc] = 1; // 标记
for (int[] d : direction) {
int nr = cr + d[0], nc = cc + d[1];
if (nr < 0 || nr >= m || nc < 0 || nc >= n) {
continue;
}
queue.add(new Pair<>(nr, nc));
if (nr == m - 1 && nc == n - 1) {
return pathLength + 1;
}
queue.add(new Pair<>(nr, nc));
}
}
return -1;
}
return -1;
}
```

## 2. 组成整数的最小平方数数量
Expand Down
4 changes: 2 additions & 2 deletions docs/notes/Leetcode-Database 题解.md
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ https://leetcode.com/problems/rank-scores/description/
| 2 | 4.2 | 2 | 2 |
| 3 | 4.3 | 1 | 1 |

使用连接操作找到某个 score 对应的大于等于其值的记录
使用连接操作找到某个 score 对应的大于其值的记录

```sql
SELECT
Expand Down Expand Up @@ -890,7 +890,7 @@ ORDER BY
| score | Rank |
| :---: | :--: |
| 4.2 | 1 |
| 4.2 | 1 |
| 4.2 | 2 |
| 4.1 | 2 |

连接情况如下:
Expand Down
4 changes: 2 additions & 2 deletions docs/notes/Linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ g/re/p(globally search a regular expression and print),使用正则表示式

```html
$ grep [-acinv] [--color=auto] 搜寻字符串 filename
-c统计匹配到行的个数
-c统计个数
-i : 忽略大小写
-n : 输出行号
-v : 反向选择,也就是显示出没有 搜寻字符串 内容的那
Expand All @@ -1039,7 +1039,7 @@ $ grep -n 'the' regular_express.txt
18:google is the best tools for search keyword
```

示例:正则表达式 a{m,n} 用来匹配字符 a m\~n 次,这里需要将 { 和 } 进行转义,因为它们在 shell 是有特殊意义的。
示例:正则表达式 a{m,n} 用来匹配字符 a m\~n 次,这里需要将 { 和 } 进行转移,因为它们在 shell 是有特殊意义的。

```html
$ grep -n 'a\{2,5\}' regular_express.txt
Expand Down
11 changes: 4 additions & 7 deletions docs/notes/剑指 Offer 题解 - 目录.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# 前言

题目来自《何海涛. 剑指 Offer[M]. 电子工业出版社, 2012.》,刷题网站推荐:

- [牛客网](https://www.nowcoder.com/ta/coding-interviews?from=cyc_github)
- [Leetcode](https://leetcode-cn.com/problemset/lcof/)

# 目录


Expand Down Expand Up @@ -85,6 +78,10 @@
- [67. 把字符串转换成整数](67.%20把字符串转换成整数.md)
- [68. 树中两个节点的最低公共祖先](68.%20树中两个节点的最低公共祖先.md)

# 参考文献

何海涛. 剑指 Offer[M]. 电子工业出版社, 2012.




Expand Down
3 changes: 1 addition & 2 deletions docs/notes/计算机操作系统 - 内存管理.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
```

<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/eb859228-c0f2-4bce-910d-d9f76929352b.png"/> </div><br>

## 3. 最近未使用

> NRU, Not Recently Used
Expand All @@ -89,7 +88,7 @@

NRU 优先换出已经被修改的脏页面(R=0M=1),而不是被频繁使用的干净页面(R=1M=0)。

## 4. 先进先出
## 4. 先进先出

> FIFO, First In First Out

Expand Down
57 changes: 12 additions & 45 deletions notes/67. 把字符串转换成整数.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,20 @@ Output:

```java
public int StrToInt(String str) {
if (str == null)
if (str == null || str.length() == 0)
return 0;
boolean isNegative = str.charAt(0) == '-';
int ret = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (i == 0 && (c == '+' || c == '-')) /* 符号判定 */
continue;
if (c < '0' || c > '9') /* 非法输入 */
return 0;
int result = 0;
boolean negative = false;//是否负数
int i = 0, len = str.length();
/**
* limit 默认初始化为*负的*最大正整数 ,假如字符串表示的是正数
* 由于int的范围为-2147483648~2147483647
* 那么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;
if (len == 1) // Cannot have lone "+" or "-"
return 0;
i++;
}
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;
ret = ret * 10 + (c - '0');
}
return isNegative ? -ret : ret;
}
```


Expand Down
7 changes: 3 additions & 4 deletions notes/Java 基础.md
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ System.out.println(InterfaceExample.x);

使用接口:

- 需要让不相关的类都实现个方法,例如不相关的类都可以实现 Compareable 接口中的 compareTo() 方法;
- 需要让不相关的类都实现个方法,例如不相关的类都可以实现 Comparable 接口中的 compareTo() 方法;
- 需要使用多重继承。

使用抽象类:
Expand Down Expand Up @@ -1432,9 +1432,8 @@ Java 注解是附加在代码中的一些元信息,用于一些工具在编译

## JRE or JDK

- JREJava Runtime Environmentjava运行环境的简称,为java的运行提供了所需的环境。主要包括了JVM的标准实现和java基本类库。
- JDKJava Development Kitjava开发工具包,提供了java的开发及运行环境。JDKjava开发的核心,集成了JRE以及些其他的工具,比如编译 java 源码的编译器 javac等。
- 因此可以这样认为:JDK>JRE>JVMJRE支持了java程序的运行,而JDK则同时支持了java程序的开发。
- JRE is the JVM program, Java application need to run on JRE.
- JDK is a superset of JRE, JRE + tools for developing java programs. e.g, it provides the compiler "javac"
# 参考资料
Expand Down
2 changes: 1 addition & 1 deletion notes/Java 虚拟机.md
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ public static final int value = 123;
其中解析过程在某些情况下可以在初始化阶段之后再开始,这是为了支持 Java 的动态绑定。

<div data="补充为什么可以支持动态绑定 --> <--"></div>
### 5. 初始化
### 5. 初始化

<div data="modify -->"></div>
初始化阶段才真正开始执行类中定义的 Java 程序代码。初始化阶段是虚拟机执行类构造器 &lt;clinit>() 方法的过程。在准备阶段,类变量已经赋过次系统要求的初始值,而在初始化阶段,根据程序员通过程序制定的主观计划去初始化类变量和其它资源。
Expand Down
Loading

0 comments on commit afcfca0

Please sign in to comment.