网站被降权,韩国还有出线的可能,福建有没有网站做鞋子一件代发,专业画册设计力扣爆刷第107天之CodeTop100五连刷21-25 文章目录 力扣爆刷第107天之CodeTop100五连刷21-25一、103. 二叉树的锯齿形层序遍历二、92. 反转链表 II三、54. 螺旋矩阵四、160. 相交链表五、23. 合并 K 个升序链表 一、103. 二叉树的锯齿形层序遍历
题目链接#xff1a;https://…力扣爆刷第107天之CodeTop100五连刷21-25 文章目录 力扣爆刷第107天之CodeTop100五连刷21-25一、103. 二叉树的锯齿形层序遍历二、92. 反转链表 II三、54. 螺旋矩阵四、160. 相交链表五、23. 合并 K 个升序链表 一、103. 二叉树的锯齿形层序遍历
题目链接https://leetcode.cn/problems/binary-tree-zigzag-level-order-traversal/description/ 思路本题要求每层先从左往右遍历下一层从右往左再往下一层又变成了从左往右遍历就是这种交替遍历其实层序遍历的方式我们不需要改变只需要改变记录的方式对于每层出队节点收集时如果该层是要求正序那么把元素从尾部添加进新队列如果该层要求逆序那就从头部添加进队列因为尾插法是正序头插法是逆序遍历后然后再把收集到的数据添加进总集合中即可。 class Solution {ListListInteger arrayList new ArrayList();LinkedListTreeNode queue new LinkedList();public ListListInteger zigzagLevelOrder(TreeNode root) {if(root null) return arrayList;boolean flag true;queue.add(root);while(!queue.isEmpty()) {int size queue.size();LinkedListInteger list new LinkedList();for(int i 0; i size; i) {TreeNode node queue.poll();if(flag) {list.addLast(node.val);}else{list.addFirst(node.val);}if(node.left ! null) {queue.add(node.left);}if(node.right ! null) {queue.add(node.right);}}flag !flag;arrayList.add(new ArrayList(list));}return arrayList;}
}
二、92. 反转链表 II
题目链接https://leetcode.cn/problems/reverse-linked-list-ii/description/ 思路翻转链表中的一个片段没啥技术含量定位到为止然后头插法然后拼接尾部。 class Solution {public ListNode reverseBetween(ListNode head, int left, int right) {ListNode root new ListNode(-1, head);ListNode p root, pro root, pre null, end null;for(int i 0; i left; i) {pro p;p p.next;}pro.next null;end p;for(int i left; i right; i) {pre p.next;p.next pro.next;pro.next p;p pre;}end.next p;return root.next;}
}三、54. 螺旋矩阵
题目链接https://leetcode.cn/problems/spiral-matrix/description/ 思路螺旋矩阵控制上下左右边界例如只要上边界小于等于下边界就可以读取一行如果左边界小于等于右边就可以读取一列。
class Solution {ListInteger spiralOrder(int[][] matrix) {ListInteger list new ArrayList();int m matrix.length, n matrix[0].length;int left 0, right n-1, up 0, down m-1;while(list.size() m * n) {if(up down) {for(int i left; i right; i) {list.add(matrix[up][i]);}up;}if(left right) {for(int i up; i down; i) {list.add(matrix[i][right]);}right--;}if(up down) {for(int i right; i left; i--) {list.add(matrix[down][i]);}down--;}if(left right) {for(int i down; i up; i--) {list.add(matrix[i][left]);}left;}} return list;}
}四、160. 相交链表
题目链接https://leetcode.cn/problems/intersection-of-two-linked-lists/description/ 思路经典题目两个链表都先求长度然后遍历统一长度然后同步遍历即可。 public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode pa headA, pb headB;int lenA 0, lenB 0;while(pa ! null) {pa pa.next;lenA;}while(pb ! null) {pb pb.next;lenB;}pa headA;pb headB;while(lenA lenB) {pb pb.next;lenB--;}while(lenA lenB) {pa pa.next;lenA--;}while(pa ! null) {if(pa pb) return pa;pa pa.next;pb pb.next;}return null;}
}五、23. 合并 K 个升序链表
题目链接https://leetcode.cn/problems/merge-k-sorted-lists/description/ 思路采用优先级队列链表按照头部节点进行优先级排序出队后然后再入队然后一直出队统计即可。 class Solution {
public ListNode mergeKLists(ListNode[] lists) {PriorityQueueListNode queue new PriorityQueue((a, b) - a.val-b.val);for(ListNode node : lists) {if(node ! null) {queue.add(node);}}ListNode root new ListNode();ListNode p root;while(!queue.isEmpty()) {ListNode node queue.poll();p.next node;p p.next;if(node.next ! null) {queue.add(node.next);}}return root.next;}
}