数据结构(一)
本篇代码主要以Java为例子,leetcode刷题建议至少过5遍。
一维数据结构
数组:在大多数静态语言中是连续的数据地址,在n个位置(1开始计算)插入数据,前面的数需要偏移(n-1),后面的数需要偏移(len-n+1)
链表:通过上一个节点指向下一个节点,相关联。循环链表,双端链表。LinkedList双向列表,同时继承了Duque
跳表:通过建立索引,加速查询效率。通常取该数组的一半建立上一级索引,查找速度log(n)
1->5
1->3->5
1->2->3->4->5
队列:排队的概念,先进先出。特殊的有双端队列,优先队列。Deque。PriorityQuque(大顶堆实现)
重点的api:offer,peek,poll(不存在返回null)。push,element,pop(不存在抛出异常)
栈:先进后出。Stack。通常个人使用双端队列比较方便。
leetcode刷题指南(简单题不提供代码):
1.283移动零[简单]:交换0的位置,向后移动即可
2.15三数之和[中等]:略微复杂,但是有套路。适用于四数之和等等的。
1.排序(这步是为了后面去重方便),从小到大
2.循环嵌套(四数就3个循环,3数就2个循环)
3.双指针法,计算(sum,target(0)和sum比较),如果target>sum,说明右边太大了,要减。
4.去重集合,如果左边值/右边值和下一个值一样,不用加进去了结果都一样。如果当前值i与前一个值一样,也不用再计算了
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i - 1])
continue;
int l = i + 1;
int r = nums.length - 1;
while (l < r) {
int sum = nums[i] + nums[l] + nums[r];
if (sum == 0) {
List<Integer> row = new ArrayList<>();
row.add(nums[l]);
row.add(nums[r]);
row.add(nums[i]);
res.add(row);
while (l < r && nums[l] == nums[l + 1]) l++;
while (l < r && nums[r] == nums[r - 1]) r--;
l++;
r--;
} else if (sum > 0)
r--;
else l++;
}
}
return res;
}
3.11.盛最多水的容器[中等]:双指针法,如果左边和右边指针,小的高度,移动即可
public int maxArea(int[] height) {
int res = 0;
int l = 0;
int r = height.length - 1;
while (l < r) {
if(height[l] > height[r]){
res = Math.max(res,height[r]*(r-l));
r--;
}else {
res = Math.max(res,height[l]*(r-l));
l++;
}
}
return res;
}
4.641.设计循环双端队列[中等]:使用节点引用
class MyCircularDeque {
Node head = null;
Node end = null;
int size = 0;
int cap = 0;
public MyCircularDeque(int k) {
cap = k;
}
public boolean insertFront(int value) {
if (size == cap) return false;
Node cur = new Node(value);
size++;
if (head == null) {
end = cur;
head = cur;
return true;
}
head.pre = cur;
cur.next = head;
head = cur;
return true;
}
public boolean insertLast(int value) {
if (size == cap) return false;
Node cur = new Node(value);
size++;
if (end == null) {
end = cur;
head = cur;
return true;
}
end.next = cur;
cur.pre = end;
end = cur;
return true;
}
public boolean deleteFront() {
if (size == 0) return false;
size--;
if (end == head) {
head = null;
end = null;
return true;
}
head.next.pre = null;
head = head.next;
return true;
}
public boolean deleteLast() {
if (size == 0) return false;
size--;
if (end == head) {
head = null;
end = null;
return true;
}
end.pre.next = null;
end = end.pre;
return true;
}
public int getFront() {
if (size == 0) return -1;
return head.val;
}
public int getRear() {
if (size == 0) return -1;
return end.val;
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == cap;
}
public static class Node {
int val;
Node pre;
Node next;
public Node(int val) {
this.val = val;
}
}
}
/**
* Your MyCircularDeque object will be instantiated and called as such:
* MyCircularDeque obj = new MyCircularDeque(k);
* boolean param_1 = obj.insertFront(value);
* boolean param_2 = obj.insertLast(value);
* boolean param_3 = obj.deleteFront();
* boolean param_4 = obj.deleteLast();
* int param_5 = obj.getFront();
* int param_6 = obj.getRear();
* boolean param_7 = obj.isEmpty();
* boolean param_8 = obj.isFull();
*/
5.42.接雨水[困难]:双指针法,单调递增栈
1.双指针法,计算每个距离为1的能接雨水的高度。如果左边比右边大,那么计算右边的,否则计算左边的。计算方法:该边最大高度值-当前高度值
2.单调递增栈。如果数组当前元素>Deque中最右边的元素(最大的),加入最右。否则结算,从左往右结算,记录左边历史最大值,leftMax-当前值进行增加。
public int trap(int[] height) {
int l = 0, r = height.length - 1;
int lMax = height[l], rMax = height[r];
int res = 0;
while (l < r) {
lMax = Math.max(lMax, height[l]);
rMax = Math.max(rMax, height[r]);
if (height[l] > height[r]) {
res += (rMax - height[r]);
r--;
} else {
res += (lMax - height[l]);
l++;
}
}
return res;
}
单调递增,单调递减,优先队列,双指针。
下期预告
二维数据结构上集(哈希表,树,图)