随笔分类 -  LeetCode

上一页 1 ··· 4 5 6 7 8 9 10 11 12 下一页
摘要:广度优先搜索+队列 class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> list = new LinkedList<>(); if (root == nul 阅读全文
posted @ 2021-12-23 11:44 振袖秋枫问红叶 阅读(35) 评论(0) 推荐(0)
摘要:广度优先搜索 class Solution { public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> list = new ArrayList<>(); if (root == null){ 阅读全文
posted @ 2021-12-23 11:10 振袖秋枫问红叶 阅读(24) 评论(0) 推荐(0)
摘要:广度优先搜索+队列 import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; class Solution { public List<List<In 阅读全文
posted @ 2021-12-23 10:55 振袖秋枫问红叶 阅读(32) 评论(0) 推荐(0)
摘要:栈 import java.util.Iterator; import java.util.List; import java.util.Stack; class NestedIterator implements Iterator<Integer> { Stack<NestedInteger> s 阅读全文
posted @ 2021-12-21 16:44 振袖秋枫问红叶 阅读(38) 评论(0) 推荐(0)
摘要:栈 import java.util.Stack; class Solution { public String simplifyPath(String path) { /** * 如果有连续的"//"会变成空字符串,需要额外判断 */ String[] newString = path.split 阅读全文
posted @ 2021-12-20 11:26 振袖秋枫问红叶 阅读(42) 评论(0) 推荐(1)
摘要:栈 import java.util.Stack; class Solution { public int evalRPN(String[] tokens) { /** * 逆波兰表达式 * 遇到数字压入栈,遇到算符就弹出最后两个数进行计算,将结果再压入栈 * 最后剩下一个数字,就是最终结果 */ 阅读全文
posted @ 2021-12-20 10:51 振袖秋枫问红叶 阅读(39) 评论(0) 推荐(1)
摘要:双指针法 class Solution { public boolean isPalindrome(ListNode head) { ListNode dummyHead = new ListNode(0, head); ListNode slow = dummyHead; ListNode fas 阅读全文
posted @ 2021-12-19 18:29 振袖秋枫问红叶 阅读(32) 评论(0) 推荐(0)
摘要:class Solution { public void reorderList(ListNode head) { ListNode dummyHead = new ListNode(0, head); ListNode slow = dummyHead; ListNode fast = dummy 阅读全文
posted @ 2021-12-19 17:43 振袖秋枫问红叶 阅读(27) 评论(0) 推荐(0)
摘要:闭合为环 class Solution { public ListNode rotateRight(ListNode head, int k) { if (head == null || head.next == null || k == 0){ return head; } ListNode du 阅读全文
posted @ 2021-12-19 16:03 振袖秋枫问红叶 阅读(28) 评论(0) 推荐(0)
摘要:两次遍历 class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { /** * 两次遍历 * 先得到链表的长度,再正向遍历到length - n的位置,也就是待删除节点的前一个节点 */ ListNode du 阅读全文
posted @ 2021-12-19 15:05 振袖秋枫问红叶 阅读(41) 评论(0) 推荐(0)
摘要:和下一个节点交换 class Solution { public void deleteNode(ListNode node) { node.val = node.next.val; node.next = node.next.next; } } /** * 时间复杂度 O(1) * 空间复杂度 O 阅读全文
posted @ 2021-12-19 14:39 振袖秋枫问红叶 阅读(46) 评论(0) 推荐(0)
摘要:自顶向下(递归) class Solution { public ListNode sortList(ListNode head) { return sortList(head, null); } public ListNode sortList(ListNode head, ListNode ta 阅读全文
posted @ 2021-12-19 14:23 振袖秋枫问红叶 阅读(27) 评论(0) 推荐(0)
摘要:迭代 class Solution { public ListNode reverseKGroup(ListNode head, int k) { ListNode dummyHead = new ListNode(0, head); ListNode prev = dummyHead; ListN 阅读全文
posted @ 2021-12-17 14:44 振袖秋枫问红叶 阅读(33) 评论(0) 推荐(0)
摘要:递归 class Solution { public ListNode insertionSortList(ListNode head) { /** * 终止条件 */ if (head == null || head.next == null){ return head; } ListNode n 阅读全文
posted @ 2021-12-17 10:21 振袖秋枫问红叶 阅读(32) 评论(0) 推荐(0)
摘要:迭代 class Solution { public ListNode swapPairs(ListNode head) { ListNode dummyHead = new ListNode(-1, head); ListNode prev = dummyHead; ListNode cur = 阅读全文
posted @ 2021-12-16 16:08 振袖秋枫问红叶 阅读(38) 评论(0) 推荐(0)
摘要:迭代 class Solution { public ListNode deleteDuplicates(ListNode head) { /** * 节点数小于2不用去重 */ if (head == null || head.next == null){ return head; } /** * 阅读全文
posted @ 2021-12-16 14:46 振袖秋枫问红叶 阅读(32) 评论(0) 推荐(0)
摘要:翻转链表 class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { l1 = reverseList(l1); l2 = reverseList(l2); /** * 最后还要反转一下 */ return r 阅读全文
posted @ 2021-12-10 10:37 振袖秋枫问红叶 阅读(34) 评论(0) 推荐(0)
摘要:迭代 class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummyHead = new ListNode(-1); ListNode head = dummyHead; ListNo 阅读全文
posted @ 2021-12-09 22:10 振袖秋枫问红叶 阅读(15) 评论(0) 推荐(0)
摘要:迭代 class Solution { public ListNode oddEvenList(ListNode head) { if (head == null){ return head; } /** * 类似于《86. 分割链表》,奇数位置的节点不动,将偶数位置的节点全部放到最后面,原地完成 阅读全文
posted @ 2021-12-09 20:51 振袖秋枫问红叶 阅读(49) 评论(0) 推荐(0)
摘要:迭代 class Solution { public ListNode partition(ListNode head, int x) { if (head == null){ return head; } ListNode dummyHead = new ListNode(-1); dummyHe 阅读全文
posted @ 2021-12-09 17:20 振袖秋枫问红叶 阅读(31) 评论(0) 推荐(0)

上一页 1 ··· 4 5 6 7 8 9 10 11 12 下一页