1. 输入链表倒数第k个数 (双指针): 前指针和后指针, 前指针向前移动k步; 前后指针一起移动过,前指针遇到null时,停止移动。
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode forgroundPointer, backGroundPointer;
forgroundPointer = head;
backGroundPointer = head;
//1. forgroundPointer moveforward by k step;
int kStep = k;
while(kStep >0){
forgroundPointer = forgroundPointer.next;
kStep--;
}
//1. two pointer moveforward together until forgroundPoint at end.
while(forgroundPointer != null) {
forgroundPointer = forgroundPointer.next;
backGroundPointer = backGroundPointer.next;
}
return backGroundPointer;
}
2.从尾到头输出链表到数组上(递归法)
List<ListNode> tmp = new ArrayList<>();
public int[] reversePrint(ListNode head) {
recursion(head);
int result[] = new int[tmp.size()] ;
for(int i = 0; i <tmp.size(); i++) {
result[i] = tmp.get(i).val;
}
return result;
}
public void recursion(ListNode head) {
if(head == null){
return;
}
recursion(head.next);
tmp.add(head);
}
3. 反转链表
1.双指针迭代法: 考虑遍历链表,并在访问节点时,修改next引用指向。
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while(cur!=null) {
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
2.递归法:
递归当前节点,在回溯时修改next引用指向。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
return recurse(head,null);
}
public ListNode recurse(ListNode cur, ListNode pre){
if(cur == null) {
return pre;
}
ListNode res = recurse(cur.next, cur);
cur.next = pre;
return res;
}
}
4.求两个链表的最近公共节点 (双指针)
链表A节点数量为a 链表B节点数量为b, 公共节点长度为c
当链表A走完链表A上的节点,再从链表B遍历,走到公共节点时,节点数为 a+(b-c)
当链表B走完链表B上的节点,再从链表A遍历,走到公共节点时,节点数为 b+(a-c)
此时,有两种情况,
1. 链表A和B都到公共节点,返回此时链表A节点指向的指针headA
2. 链表A和B无公共尾部,最后都为null,则返回null
代码如下:
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode A = headA, B = headB;
while (A != B) {
A = A != null ? A.next : headB;
B = B != null ? B.next : headA;
}
return A;
}
}
5. 两个栈构建一个队列
设置一个辅助栈,保存倒序的栈元素。
class CQueue {
Stack<Integer> stackA = new Stack<>();
Stack<Integer> stackB = new Stack<>();
public CQueue() {
}
public void appendTail(int value) {
stackA.add(value);
}
public int deleteHead() {
if(!stackB.isEmpty()){
return stackB.pop();
}
if(stackA.isEmpty()){
return -1;
}
while(!stackA.isEmpty()){
stackB.add(stackA.pop());
}
return stackB.pop();
}
}
6.求栈的min函数
设置一个辅助栈,也是用来保存递减的栈元素,需要注意辅助栈>=x的时候要把x保存起来。
class MinStack {
Stack<Integer> A;
Stack<Integer> B;
/** initialize your data structure here. */
public MinStack() {
A = new Stack<>();
B = new Stack<>();
}
public void push(int x) {
A.add(x);
if(B.empty()|| B.peek()>=x) { //注意这里>= 防止漏过相同的大小的min x.
B.add(x);
}
}
public void pop() {
if(A.pop().equals(B.peek())){ //注意Integer元素用equals比较。
B.pop();
}
}
public int top() {
return A.peek();
}
public int min() {
return B.peek();
}
}
7.滑动窗口最大值(构造单调的双端队列)
给定一个数组 nums 和滑动窗口的大小 k,请找出所有滑动窗口里的最大值。
示例:
输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3
输出: [3,3,5,5,6,7]
解释:
滑动窗口的位置 最大值
--------------- -----
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/hua-dong-chuang-kou-de-zui-da-zhi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法:分未形成区间和形成区间两部分来构造单调非严格的递减队列。
窗口的区间[i,j] 1-k <=i <=n-k 0<=j<=n-1
class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {
if(nums.length == 0 ){
return new int[0];
}
Deque<Integer> queue = new LinkedList<>();
int n = nums.length - k + 1;
int[] result = new int[n];
for(int i = 0; i < k; i++){
while(!queue.isEmpty() && queue.peekLast() < nums[i]) queue.removeLast();
queue.addLast(nums[i]);
}
result[0] = queue.peekFirst();
for(int i = k; i < nums.length; i++){
if(!queue.isEmpty() && queue.peekFirst().equals(nums[i-k])) queue.removeFirst();
while(!queue.isEmpty() && queue.peekLast() < nums[i]) queue.removeLast();
queue.addLast(nums[i]);
result[i-k+1] = queue.peekFirst();
}
return result;
}
}
8.求最小K的数。
最小堆法:时间复杂度 O(logk);
最值问题最好用二叉堆来解决, 最小堆的特点是,根节点要都小于其左右子节点。
第一步是构造二叉堆,从i = n/2 开始向上遍历各层大小关系
第二步是把第一个节点,和最后一个节点交换,得到最值k, 然后继续把新的数组的第一个节点下沉,以重新构造最小堆。
class Solution {
public int[] getLeastNumbers(int[] arr, int k) {
//build heaf
int N = arr.length;
for(int index = N/2; index >=1; index--) {
sink(arr, index, N);
}
//sort heaf
int[] res = new int[k];
for(int i = 0 ; i < k; i++) {
res[i] = arr[0];
exch(arr, 1, N--);
sink(arr, 1 , N);
}
return res;
}
public void sink(int[]arr, int i, int N){
while(2 * i <= N){
int j = 2 * i;
if(j < N && less(arr, j + 1, j)) j++;
if(less(arr, i, j))break;
exch(arr, i, j);
i = j; //去遍历下一层,构造最小堆
}
}
public void exch(int[] arr, int i, int j){
i--;
j--;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public boolean less(int[] arr, int i, int j){
return arr[i - 1]<arr[j - 1];
}
}
快速排序法:
快速排序是选择拆分区间值j, 使得区间内[0,j-1] < j < [j+1..n], 然后递归构造左子序和右子序,最终数组就是有序的了。
最小k个数, 如果拆分点j > k, 那边最小k个数在j的左子序列,如果j = k, 那边说明j左边的数已经是最小k个数了。
class Solution {
int k;
public int[] getLeastNumbers(int[] arr, int k) {
int N = arr.length;
this.k = k;
sort(arr, 0, N - 1);
int[] res = new int[k];
for(int i = 0 ; i < k; i++) {
res[i] = arr[i];
}
return res;
}
public void sort(int[] arr, int lo, int hi){
if(lo>=hi) {
return;
}
int j = partition(arr, lo, hi);
if(j == k) {
return;
}
if(j > k) {
sort(arr, lo, j-1);
} else {
sort(arr, j+1, hi);
}
}
public int partition(int[] arr, int lo, int hi){
int i = lo;
int j = hi + 1;
int v = arr[lo];
while(true) {
while(less(arr[++i],v)){
if(i == hi) break;
}
while(less(v, arr[--j])) {
if(j == lo) break;
}
if(i >= j) break;
exch(arr, i , j);
}
exch(arr, lo , j);
return j;
}
public void exch(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public boolean less(int i, int j){
return i<j;
}
}