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

Appearance settings

Commit b7e3f8b

Browse filesBrowse files
committed
Create Book
1 parent ea883a6 commit b7e3f8b
Copy full SHA for b7e3f8b

File tree

Expand file treeCollapse file tree

4 files changed

+430
-13
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+430
-13
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
在原项目的基础上,尽量保持了原有的结构划分,并做了一点微小的工作:
66

77
- 代码改写为`java`语言版本
8-
- 调整部分内容,新增了几种算法
8+
- 调整内容,新增了几种算法
99
- 补充部分[leetcode中国站](https://leetcode-cn.com/)的题目链接
1010

1111
## 核心内容
@@ -19,15 +19,14 @@
1919
### 基础算法
2020

2121
- [滑动窗口](./basic_algorithm/slide_window.md)
22-
- [递归算法](./basic_algorithm/recursion.md)
2322
- [回溯算法](./basic_algorithm/backtrack.md)
2423
- [二分搜索](./basic_algorithm/binary_search.md)
2524
- [排序算法](./basic_algorithm/sort.md)
2625
- [动态规划](./basic_algorithm/dp.md)
2726

2827
### 进阶算法
2928

30-
> 此处整理了一些不常见但是在特殊场景下很有效的算法
29+
> 此处整理了一些特殊情况下适用的算法
3130
3231
- [快速选择](./advanced_algorithm/quick_select.md)
3332
- [三向切分快速排序](./advanced_algorithm/three_way_quick_sort.md)

‎SUMMARY.md

Copy file name to clipboard
+10-10Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
# 算法模板
1+
# 算法模板——Java版
22

3-
## 数据结构篇
3+
## 数据结构
44

5-
- [二叉树](data_structure/binary_tree.md)
65
- [链表](data_structure/linked_list.md)
76
- [栈和队列](data_structure/stack_queue.md)
8-
- [二进制](data_structure/binary_op.md)
7+
- [二叉树](data_structure/binary_tree.md)
98

10-
## 基础算法篇
9+
## 基础算法
1110

11+
- [滑动窗口](basic_algorithm/slide_window.md)
12+
- [回溯算法](basic_algorithm/backtrack.md)
1213
- [二分搜索](basic_algorithm/binary_search.md)
1314
- [排序算法](basic_algorithm/sort.md)
1415
- [动态规划](basic_algorithm/dp.md)
1516

16-
## 算法思维
17+
## 进阶算法
1718

18-
- [递归思维](advanced_algorithm/recursion.md)
19-
- [滑动窗口思想](advanced_algorithm/slide_window.md)
20-
- [二叉搜索树](advanced_algorithm/binary_search_tree.md)
21-
- [回溯法](advanced_algorithm/backtrack.md)
19+
- [快速选择](advanced_algorithm/quick_select.md)
20+
- [三向切分快速排序](advanced_algorithm/three_way_quick_sort.md)
21+
- [二进制运算](advanced_algorithm/binary_op.md)

‎advanced_algorithm/binary_op.md

Copy file name to clipboard
+158Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# 二进制
2+
3+
## 常见二进制操作
4+
5+
### 基本操作
6+
7+
a=0^a=a^0
8+
9+
0=a^a
10+
11+
由上面两个推导出:a=a^b^b
12+
13+
### 交换两个数
14+
15+
a=a^b
16+
17+
b=a^b
18+
19+
a=a^b
20+
21+
### 移除最后一个 1
22+
23+
a=n&(n-1)
24+
25+
### 获取最后一个 1
26+
27+
diff=(n&(n-1))^n
28+
29+
## 常见例题
30+
31+
### 只出现一次的数字
32+
33+
> [136. 只出现一次的数字](https://leetcode-cn.com/problems/single-number/)
34+
>
35+
> 给定一个**非空**整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
36+
37+
```java
38+
public int singleNumber(int[] nums) {
39+
// 10 ^ 10 == 00
40+
// 两个相同的数异或变成0
41+
int result = 0;
42+
for (int n : nums) {
43+
result = result ^ n;
44+
}
45+
return result;
46+
}
47+
```
48+
49+
### 只出现一次的数字 II
50+
51+
> [137. 只出现一次的数字 II](https://leetcode-cn.com/problems/single-number-ii/)
52+
>
53+
> 给定一个**非空**整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次。找出那个只出现了一次的元素。
54+
55+
```java
56+
public int singleNumber(int[] nums) {
57+
int result = 0;
58+
// 统计每位1的个数
59+
for (int i = 0; i < 64; i++) {
60+
int sum = 0;
61+
for (int n : nums) {
62+
// 统计1的个数
63+
sum += ((n >> i) & 1);
64+
}
65+
// 还原
66+
result |= ((sum % 3) << i);
67+
}
68+
return result;
69+
}
70+
```
71+
72+
### 只出现一次的数字 III
73+
74+
> [260. 只出现一次的数字 III](https://leetcode-cn.com/problems/single-number-iii/)
75+
>
76+
> 给定一个整数数组  `nums`,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。
77+
78+
```java
79+
public int[] singleNumber(int[] nums) {
80+
// 关键点怎么把a^b分成两部分,方案:可以通过diff最后一个1区分
81+
int diff = 0;
82+
for (int n : nums) {
83+
diff ^= n;
84+
}
85+
int[] result = new int[]{diff, diff};
86+
// 去掉末尾的1后异或diff就得到最后一个1的位置
87+
diff = (diff & (diff - 1)) ^ diff;
88+
for (int n : nums) {
89+
if ((diff & n) == 0) {
90+
result[0] ^= n;
91+
} else {
92+
result[1] ^= n;
93+
}
94+
}
95+
return result;
96+
}
97+
```
98+
99+
### 位1的个数
100+
101+
> [191. 位1的个数](https://leetcode-cn.com/problems/number-of-1-bits/)
102+
>
103+
> 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’  的个数(也被称为[汉明重量](https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F))。
104+
105+
```java
106+
public int hammingWeight(int n) {
107+
int result = 0;
108+
while (n != 0) {
109+
if ((n & 1) == 1) result++;
110+
n = n >>> 1;
111+
}
112+
return result;
113+
}
114+
```
115+
116+
### 颠倒二进制位
117+
118+
> [190. 颠倒二进制位](https://leetcode-cn.com/problems/reverse-bits/)
119+
>
120+
> 颠倒给定的 32 位无符号整数的二进制位。
121+
122+
思路:依次颠倒即可
123+
124+
```java
125+
public int reverseBits(int n) {
126+
int result = 0;
127+
int p = 31;
128+
while (n != 0) {
129+
result += ((n & 1) << p);
130+
n >>>= 1;
131+
p--;
132+
}
133+
return result;
134+
}
135+
```
136+
137+
### 数字范围按位与
138+
139+
> [201. 数字范围按位与](https://leetcode-cn.com/problems/bitwise-and-of-numbers-range/)
140+
>
141+
> 给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。
142+
143+
问题转化为求两个给定数字二进制状态下的最长公共前缀,可以用移位判断的思想来做,这里使用另一种`Brian Kernighan`算法:`number``number−1`之间进行按位与运算后,`number`中最右边的1会被抹去变成0。
144+
145+
```java
146+
public int rangeBitwiseAnd(int m, int n) {
147+
while (m < n) {
148+
// 抹去最右边的 1
149+
n = n & (n - 1);
150+
}
151+
return n;
152+
}
153+
```
154+
155+
## 注意点
156+
157+
- Java中区分右移运算和无符号右移运算
158+
- 注意运算顺序,不确定时尽量加上括号

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.