Skip to content

Commit a7a9d7d

Browse files
committed
add leetcode: offer-56.1-56.2
1 parent a300d2f commit a7a9d7d

File tree

7 files changed

+78
-4
lines changed

7 files changed

+78
-4
lines changed

Algorithm/src/acwing/basic/chapter2/_154SlidingWindow.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,17 @@ public static void main(String[] args) throws IOException {
7575
// System.out.println( str.length );
7676
for (int i = 0; i < n; i++) a[i] = toInt(str[i]);
7777

78-
78+
// 递增的队列,输出最小值
7979
for (int i = 0; i < n; i++) {
8080
/*首先判断当前队头元素还是不是在窗口的内部,如果不在的话就要删掉
8181
i此时是窗口右侧的端点,i - k + 1得到的是窗口左侧的端点,这样就可以判断
8282
此时队列的头部时候还在窗口内
8383
*/
8484
if (hh <= tt && q[hh] < i - k + 1) hh++;
8585
//当队列不空,并且队尾元素比当前要加入进来的元素大,那就把他删掉
86+
// (对于递增队列来说,其实是把较小的值插到队列的前面。所以队尾大于当前元素,队尾指针减减)
8687
while (hh <= tt && a[q[tt]] >= a[i]) tt--;
87-
// a的下标存储进队列
88+
// a的下标存储进队列(到这一步就会把最小的入队)-----这里不用判断hh<= tt因为tt最多减到-1
8889
q[++tt] = i;
8990

9091
//当窗口满足k个数时,才输出最小值
@@ -95,12 +96,15 @@ public static void main(String[] args) throws IOException {
9596
// 这里要记得重新初始化队列长度
9697
hh = 0;
9798
tt = -1;
99+
//递减队列,输出最大值
98100
for (int i = 0; i < n; i++) {
99101
if (hh <= tt && q[hh] < i - k + 1) hh++;
100102
//当队列不空,并且队尾元素比当前要加入进来的元素小,那就把他删掉
103+
// (对于递减队列来说,其实是把较大的值插到队列的前面)
101104
while (hh <= tt && a[q[tt]] <= a[i]) tt--;
102105
q[++tt] = i;
103106

107+
// 满足有k个窗口的时候,直接输出队头即可
104108
if (i >= k - 1) System.out.print(a[q[hh]] + " ");
105109
}
106110

Algorithm/src/acwing/basic/chapter2/_830MonotoneStack.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class _830MonotoneStack {
3535
public static int tt = 0;
3636
public static int n;
3737

38+
//==========================可以覆盖最近比当前数较大的,所以是单调栈============================
3839
public static void main(String[] args) throws IOException {
3940
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
4041
String[] str = br.readLine().split(" ");
@@ -53,7 +54,7 @@ public static void main(String[] args) throws IOException {
5354
// 否则输出栈顶元素
5455
System.out.print(stk[tt] + " ");
5556
}
56-
// 然后把当前元素记录到找中
57+
// 然后把当前元素记录到栈中(这一步其实就已经覆盖栈中最小大于当前数的了)
5758
stk[++tt] = x;
5859
// 遍历的变量加加
5960
i++;

Algorithm/src/offer/_55_1TheDepthOfTheBinaryTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Solution {
2323
public int maxDepth(TreeNode root) {
2424
if( root == null ) return 0;
2525
// queue本层结点, temp下一层结点
26-
List<TreeNode> queue = new LinkedList<>(){{ add(root); }} , temp;
26+
List<TreeNode> queue = new LinkedList<TreeNode>(){{ add(root); }} , temp;
2727
int res = 0;
2828
while( queue.size() != 0 ){
2929
temp = new LinkedList<>();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package offer;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-14-14:31
6+
* @Function Description :56.1数组中数字出现的次数
7+
*/
8+
public class _56_1_NumberOfNumbersInTheArray {
9+
class Solution {
10+
public int[] singleNumbers(int[] nums) {
11+
// xy记录只出现一次的数字
12+
int x = 0 , y = 0 , n = 0 , m = 1;
13+
// 异或一遍之后,n就是两个只出现一次的异或值,其余出现两次的抵消了
14+
for( int num : nums ){
15+
n ^= num;
16+
}
17+
// 通过与运算从右向左循环判断,可 获取整数x⊕y 首位 1 ,记录于 m 中
18+
while( (n & m) == 0 )
19+
m <<= 1;
20+
//把xy分离出来
21+
for( int num : nums ){
22+
// 跟m相与等于1为一组,说明是其中一个
23+
if( (num & m) != 0 ) x ^= num;
24+
// 等于0另外一组,是另外一个。 其余出现两次的会在异或过程中抵消
25+
else y ^= num;
26+
}
27+
28+
return new int[]{x , y};
29+
}
30+
}
31+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package offer;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @author : CodeWater
8+
* @create :2022-07-14-14:51
9+
* @Function Description :56.2 数组中数字出现的次数
10+
*/
11+
public class _56_2_NumberOfNumbersInTheArray {
12+
//==========================位运算============================
13+
class Solution {
14+
public int singleNumber(int[] nums) {
15+
int ones = 0 , twos = 0;
16+
for( int num : nums ){
17+
ones = ones ^ num & ~twos;
18+
twos = twos ^ num & ~ones;
19+
}
20+
return ones;
21+
}
22+
}
23+
24+
//==========================hashmap============================
25+
class Solution2 {
26+
public int singleNumber(int[] nums) {
27+
Map<Integer , Integer> map = new HashMap<>();
28+
for( int i = 0 ; i < nums.length ; i++ ){
29+
if( map.containsKey(nums[i]) ) map.put( nums[i] , map.get(nums[i]) + 1 );
30+
else map.put(nums[i] , 1 );
31+
}
32+
for( Map.Entry<Integer , Integer> entry : map.entrySet() ){
33+
if( entry.getValue() == 1 ) return entry.getKey();
34+
}
35+
return -1;
36+
}
37+
}
38+
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)