Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 116 additions & 1 deletion Week_02/wangjiahe/NOTE.md
Original file line number Diff line number Diff line change
@@ -1 +1,116 @@
# 学习笔记
# 学习笔记
## Deque的push与addFirst与addLast
```java
/**
* openjdk11 中 push方法调用的就是addFirst
*
*/
public void push(E e) {
addFirst(e);
}
```

## PriorityQueue 优先队列
优先队列是可以对插入元素进行排序的有序队列,可以根据自然顺序或者自定义比较器对插入元素进行排序,内部也是由数组实现的。

### 1.add offer 插入元素
add方法添加元素的核心排序是调用下面两个方法进行

```java
if (comparator != null)
siftUpUsingComparator(k, x, queue, comparator);
else
siftUpComparable(k, x, queue);
```

其中siftUpUsingComparator顾名思义是调用自定义的比较器进行的。

```java
private static <T> void siftUpComparable(int k, T x, Object[] es) {
Comparable<? super T> key = (Comparable<? super T>) x;
while (k > 0) {
int parent = (k - 1) >>> 1;
Object e = es[parent];
if (key.compareTo((T) e) >= 0)
break;
es[k] = e;
k = parent;
}
es[k] = key;
}

private static <T> void siftUpUsingComparator(
int k, T x, Object[] es, Comparator<? super T> cmp) {
while (k > 0) {
int parent = (k - 1) >>> 1;
Object e = es[parent];
if (cmp.compare(x, (T) e) >= 0)
break;
es[k] = e;
k = parent;
}
es[k] = x;
}
```
大概意思就是,当插入一个新元素时,跟前面元素进行比较,如果比之前的元素大直接break插入,如果小则互相交换值并用交换后的值继续与之前的元素比较,直到最小不用交换。

### 2.remove poll 删除元素

删除,先遍历所有元素,发现元素所在的下标,删除后重新排序。

```java
private void siftDown(int k, E x) {
if (comparator != null)
siftDownUsingComparator(k, x, queue, size, comparator);
else
siftDownComparable(k, x, queue, size);
}

private static <T> void siftDownComparable(int k, T x, Object[] es, int n) {
// assert n > 0;
Comparable<? super T> key = (Comparable<? super T>)x;
int half = n >>> 1; // loop while a non-leaf
while (k < half) {
int child = (k << 1) + 1; // assume left child is least
Object c = es[child];
int right = child + 1;
if (right < n &&
((Comparable<? super T>) c).compareTo((T) es[right]) > 0)
c = es[child = right];
if (key.compareTo((T) c) <= 0)
break;
es[k] = c;
k = child;
}
es[k] = key;
}

private static <T> void siftDownUsingComparator(
int k, T x, Object[] es, int n, Comparator<? super T> cmp) {
// assert n > 0;
int half = n >>> 1;
while (k < half) {
int child = (k << 1) + 1;
Object c = es[child];
int right = child + 1;
if (right < n && cmp.compare((T) c, (T) es[right]) > 0)
c = es[child = right];
if (cmp.compare(x, (T) c) <= 0)
break;
es[k] = c;
k = child;
}
es[k] = x;
}
```

### 3.peek

比较简单,直接取的

```java
public E peek() {    return (E) queue[0];}
```

## 笔记
基础真的很重要啊,有的写法想不到竟然是因为有些基础的东西忘了。学习难度、时间稳步提升,感觉再加几波班就要完不成了 -_-!
42 changes: 42 additions & 0 deletions Week_02/wangjiahe/num_242.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
//
// 示例 1:
//
// 输入: s = "anagram", t = "nagaram"
//输出: true
//
//
// 示例 2:
//
// 输入: s = "rat", t = "car"
//输出: false
//
// 说明:
//你可以假设字符串只包含小写字母。
//
// 进阶:
//如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?
// Related Topics 排序 哈希表


import java.util.HashMap;
import java.util.Map;

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean isAnagram(String s, String t) {
if (s == null || t == null || s.length() != t.length()) {
return false;
}
int[] array1 = new int[26]; // 字符串s的数组
int[] array2 = new int[26]; // 字符串t的数组
for (char char1 : s.toCharArray()) {
array1[char1 - 'a']++;
}
for (char char2 : t.toCharArray()) {
array2[char2 - 'a']++;
}
return Arrays.equals(array1, array2);
}
}
//leetcode submit region end(Prohibit modification and deletion)
55 changes: 55 additions & 0 deletions Week_02/wangjiahe/num_42.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
//
//
//
// 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 感谢 Mar
//cos 贡献此图。
//
// 示例:
//
// 输入: [0,1,0,2,1,0,1,3,2,1,2,1]
//输出: 6
// Related Topics 栈 数组 双指针


//leetcode submit region begin(Prohibit modification and deletion)
class Solution {

public int trap(int[] height) {
/**
* 按列计算每列能装多少水
*/
// 前一列与最后一列是装不下水的
if (height.length <= 1) {
return 0;
}
// 遍历每一列的左边与右边,如果有比当前列更高的列,则可以判断当前列是否可以装水,装多少水
int sum = 0;
for (int i = 1; i < height.length - 1; i++) {
int now = height[i];
// 遍历左边,找出最高的一段
int leftMax = now;
for (int j = i - 1; j >= 0; j--) {
int left = height[j];
if (left > leftMax) {
leftMax = left;
}
}
// 遍历右边,找出最高的一段
int rightMax = now;
for (int b = i + 1; b < height.length; b++) {
int right = height[b];
if (right > rightMax) {
rightMax = right;
}
}
// 当左右最长的部分都大于当前长度,才能存下水,存下的水的量为较短那根与当前的差
if (rightMax > now && leftMax > now) {
int min = Math.min(rightMax, leftMax);
sum = sum + min - now;
}
}
return sum;
}
}
//leetcode submit region end(Prohibit modification and deletion)