File tree Expand file tree Collapse file tree 7 files changed +99
-13
lines changed Expand file tree Collapse file tree 7 files changed +99
-13
lines changed Original file line number Diff line number Diff line change 30
30
1)方法功能:入参是一个节点,返回该节点的左右子树是否平衡
31
31
2)终止条件:节点为空时返回true
32
32
3)递归逻辑:调用计算深度的方法得到左右节点的深度,计算高度差是否不超过1,是则平衡返回true,否则不平衡返回false。左右节点同样需要判断是否平衡,因此调用同样的方法
33
- 4)返回值:
33
+ 4)返回值:节点为根的树是否平衡 且 左节点为根的树是否平衡 且 右节点为根的树是否平衡
34
34
*/
35
35
class Solution {
36
36
public boolean isBalanced (TreeNode root ) {
Original file line number Diff line number Diff line change 8
8
七、排序
9
9
八、位运算
10
10
九、模拟
11
- 十、其他算法
11
+ 十、数组
12
+ 十一、字符串
13
+ 十二、数学
12
14
13
15
14
16
一、链表
82
84
83
85
84
86
七、排序
85
- 3. 数组中重复的数字
86
87
40. 最小的K个数
87
88
41. 数据流中的中位数
88
89
51. 数组中的逆序对
103
104
67. 把字符串转换成整数(atoi)
104
105
105
106
106
- 十、其他算法
107
- 5. 替换空格(增删、构造、替换)
108
- 14. 剪绳子
107
+ 十、数组
108
+ 3. 数组中重复的数字(哈希,集合,排序)
109
109
17. 打印从1到最大的n位数
110
110
21. 调整数组顺序使奇数位于偶数前面
111
111
39. 数组中出现次数超过一半的数字
112
- 43. 整数中1出现的次数(从1到n整数中1出现的次数)
113
112
45. 把数组排成最小的数
114
113
49. 丑数
115
- 50. 第一个只出现一次的字符
116
114
57. 和为S的两个数字
117
- 58. 左旋转字符串
118
115
62. 孩子们的游戏(圆圈中最后剩下的数)
119
116
66. 构建乘积数组
120
117
74. 和为S的连续正数序列
121
- 75. 字符流中第一个不重复的字符
122
118
81. 调整数组顺序使奇数位于偶数前面(二)
119
+
120
+
121
+ 十一、字符串
122
+ 5. 替换空格(增删、构造、替换)
123
+ 43. 整数中1出现的次数(从1到n整数中1出现的次数)
124
+ 50. 第一个只出现一次的字符
125
+ 58. 左旋转字符串
126
+ 75. 字符流中第一个不重复的字符
127
+
128
+
129
+ 十二、数学
130
+ 14. 剪绳子
123
131
83. 剪绳子(进阶版)
Original file line number Diff line number Diff line change
1
+ // 3. 数组中重复的数字
2
+
3
+
4
+ /*
5
+ 原地哈希:把数字交换到正确的位置上
6
+ 1、由于数字都在0到n-1的范围内,所以数字和索引可以一一对应,不对应时说明重复了
7
+ 2、遍历数组,如果索引与数字相同,说明该数字已经排序好了,继续下一个数字判断
8
+ 3、如果索引与数字不同,则将数字交换到正确的位置上
9
+ 4、当第二次碰到重复数字时,由于在正确的位置上已经有该值了,则返回该重复数字
10
+
11
+ 元素:3 1 2 3 4
12
+ 索引:0 1 2 3 4
13
+ */
14
+ public class Solution {
15
+ public int duplicate (int [] numbers ) {
16
+ for (int i = 0 ; i < numbers .length ; i ++) {
17
+ if (i == numbers [i ]) {
18
+ continue ;
19
+ }
20
+ if (numbers [i ] == numbers [numbers [i ]]) {
21
+ return numbers [i ];
22
+ }
23
+ swap (numbers , i , numbers [i ]);
24
+ }
25
+ return -1 ;
26
+ }
27
+
28
+ private void swap (int [] numbers , int x , int y ) {
29
+ int temp = numbers [x ];
30
+ numbers [x ] = numbers [y ];
31
+ numbers [y ] = temp ;
32
+ }
33
+ }
34
+
35
+
36
+ /*
37
+ 集合:用HashSet存放元素,存在则返回,不存在则添加,遍历完没找到则返回-1
38
+ */
39
+ public class Solution {
40
+ public int duplicate (int [] numbers ) {
41
+ Set <Integer > set = new HashSet <>();
42
+ for (int num : numbers ) {
43
+ if (set .contains (num )) {
44
+ return num ;
45
+ } else {
46
+ set .add (num );
47
+ }
48
+ }
49
+ return -1 ;
50
+ }
51
+ }
52
+
53
+
54
+ /*
55
+ 排序:排序后判断当前数字与前一数字是否相同,相同则返回
56
+ */
57
+ public class Solution {
58
+ public int duplicate (int [] numbers ) {
59
+ Arrays .sort (numbers );
60
+ for (int i = 1 ; i < numbers .length ; i ++) {
61
+ if (numbers [i ] == numbers [i - 1 ]) {
62
+ return numbers [i ];
63
+ }
64
+ }
65
+ return -1 ;
66
+ }
67
+ }
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ class ListNode {
13
13
14
14
15
15
/*
16
- 递归
16
+ 递归:递归到尾部后,逐层将节点值添加到列表
17
17
*/
18
18
public class Solution {
19
19
private ArrayList <Integer > list = new ArrayList <Integer >();
Original file line number Diff line number Diff line change @@ -13,6 +13,17 @@ class TreeNode {
13
13
*/
14
14
15
15
16
+ /*
17
+ 递归:
18
+ 1、方法功能:入参是前序和中序数组,获取前序数组首元素,构造节点,然后返回该节点
19
+ 2、终止条件:两个数组为空,结束
20
+ 3、递归逻辑:
21
+ 1)由于需要构造左右节点,所以要将数组拆分成左右子树的两部分数组
22
+ 2)先找到头节点在中序数组的位置,将中序数组拆分成左右两部分,根据中序数组左半部分的长度 将前序数组也拆分成左右两部分
23
+ 3)需要构造左右节点,调用同样的方法,把两个数组传进去,得到左右节点
24
+ 4)根节点连接左右节点,返回根节点
25
+ 4、返回值:两个数组构造出来的头节点
26
+ */
16
27
public class Solution {
17
28
public TreeNode reConstructBinaryTree (int [] pre , int [] in ) {
18
29
if (pre .length == 0 || in .length == 0 ) {
Original file line number Diff line number Diff line change 1
- // 50.第一个只出现一次的字符
1
+ // 50. 第一个只出现一次的字符
2
2
3
3
4
4
public class Solution {
Original file line number Diff line number Diff line change 7
7
public class Solution {
8
8
public int [] FindNumsAppearOnce (int [] array ) {
9
9
int [] res = new int [2 ];
10
- Set <Integer > set = new HashSet ();
10
+ Set <Integer > set = new HashSet <> ();
11
11
for (int i = 0 ; i < array .length ; i ++) {
12
12
if (set .contains (array [i ])) {
13
13
set .remove (array [i ]);
You can’t perform that action at this time.
0 commit comments