diff --git a/LeetCode/src/main/java/leetcode/editor/cn/DesignFrontMiddleBackQueue.java b/LeetCode/src/main/java/leetcode/editor/cn/DesignFrontMiddleBackQueue.java new file mode 100644 index 0000000..14b0c14 --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/DesignFrontMiddleBackQueue.java @@ -0,0 +1,126 @@ +//请你设计一个队列,支持在前,中,后三个位置的 push 和 pop 操作。 +// +// 请你完成 FrontMiddleBack 类: +// +// +// FrontMiddleBack() 初始化队列。 +// void pushFront(int val) 将 val 添加到队列的 最前面 。 +// void pushMiddle(int val) 将 val 添加到队列的 正中间 。 +// void pushBack(int val) 将 val 添加到队里的 最后面 。 +// int popFront() 将 最前面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。 +// int popMiddle() 将 正中间 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。 +// int popBack() 将 最后面 的元素从队列中删除并返回值,如果删除之前队列为空,那么返回 -1 。 +// +// +// 请注意当有 两个 中间位置的时候,选择靠前面的位置进行操作。比方说: +// +// +// 将 6 添加到 [1, 2, 3, 4, 5] 的中间位置,结果数组为 [1, 2, 6, 3, 4, 5] 。 +// 从 [1, 2, 3, 4, 5, 6] 的中间位置弹出元素,返回 3 ,数组变为 [1, 2, 4, 5, 6] 。 +// +// +// +// +// 示例 1: +// +// +//输入: +//["FrontMiddleBackQueue", "pushFront", "pushBack", "pushMiddle", "pushMiddle", +//"popFront", "popMiddle", "popMiddle", "popBack", "popFront"] +//[[], [1], [2], [3], [4], [], [], [], [], []] +//输出: +//[null, null, null, null, null, 1, 3, 4, 2, -1] +// +//解释: +//FrontMiddleBackQueue q = new FrontMiddleBackQueue(); +//q.pushFront(1); // [1] +//q.pushBack(2); // [1, 2] +//q.pushMiddle(3); // [1, 3, 2] +//q.pushMiddle(4); // [1, 4, 3, 2] +//q.popFront(); // 返回 1 -> [4, 3, 2] +//q.popMiddle(); // 返回 3 -> [4, 2] +//q.popMiddle(); // 返回 4 -> [2] +//q.popBack(); // 返回 2 -> [] +//q.popFront(); // 返回 -1 -> [] (队列为空) +// +// +// +// +// 提示: +// +// +// 1 <= val <= 109 +// 最多调用 1000 次 pushFront, pushMiddle, pushBack, popFront, popMiddle 和 popBack 。 +// +// +// Related Topics 设计 链表 +// 👍 5 👎 0 + +package leetcode.editor.cn; + +import java.util.ArrayList; +import java.util.List; + +//1670:设计前中后队列 +public class DesignFrontMiddleBackQueue { + public static void main(String[] args) { + //测试代码 +// Solution solution = new PDesignFrontMiddleBackQueue().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + class FrontMiddleBackQueue { + List list; + + public FrontMiddleBackQueue() { + list = new ArrayList<>(1000); + } + + public void pushFront(int val) { + list.add(0, val); + } + + public void pushMiddle(int val) { + list.add(list.size()/2, val); + } + + public void pushBack(int val) { + list.add(val); + } + + public int popFront() { + if (list.isEmpty()) { + return -1; + } + return list.remove(0); + } + + public int popMiddle() { + if (list.isEmpty()) { + return -1; + } + return list.remove((list.size()-1) / 2); + } + + public int popBack() { + if (list.isEmpty()) { + return -1; + } + return list.remove(list.size()-1); + } + } + +/** + * Your FrontMiddleBackQueue object will be instantiated and called as such: + * FrontMiddleBackQueue obj = new FrontMiddleBackQueue(); + * obj.pushFront(val); + * obj.pushMiddle(val); + * obj.pushBack(val); + * int param_4 = obj.popFront(); + * int param_5 = obj.popMiddle(); + * int param_6 = obj.popBack(); + */ +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/LeetCode/src/main/java/leetcode/editor/cn/FuZaLianBiaoDeFuZhiLcof.java b/LeetCode/src/main/java/leetcode/editor/cn/FuZaLianBiaoDeFuZhiLcof.java new file mode 100644 index 0000000..53b5f4b --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/FuZaLianBiaoDeFuZhiLcof.java @@ -0,0 +1,129 @@ +//请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指 +//向链表中的任意节点或者 null。 +// +// +// +// 示例 1: +// +// +// +// 输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]] +//输出:[[7,null],[13,0],[11,4],[10,2],[1,0]] +// +// +// 示例 2: +// +// +// +// 输入:head = [[1,1],[2,1]] +//输出:[[1,1],[2,1]] +// +// +// 示例 3: +// +// +// +// 输入:head = [[3,null],[3,0],[3,null]] +//输出:[[3,null],[3,0],[3,null]] +// +// +// 示例 4: +// +// 输入:head = [] +//输出:[] +//解释:给定的链表为空(空指针),因此返回 null。 +// +// +// +// +// 提示: +// +// +// -10000 <= Node.val <= 10000 +// Node.random 为空(null)或指向链表中的节点。 +// 节点数目不超过 1000 。 +// +// +// +// +// 注意:本题与主站 138 题相同:https://leetcode-cn.com/problems/copy-list-with-random-point +//er/ +// +// +// Related Topics 链表 +// 👍 168 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.Node; + +import java.util.Arrays; +import java.util.List; + +//剑指 Offer 35:复杂链表的复制 +public class FuZaLianBiaoDeFuZhiLcof { + public static void main(String[] args) { + //测试代码 + Solution solution = new FuZaLianBiaoDeFuZhiLcof().new Solution(); + List var = Arrays.asList(7, 13, 11, 10, 1); + List random = Arrays.asList(null, 0, 4, 2, 0); + Node head = new Node(0); + head = head.setHead(var, random); + solution.copyRandomList(head); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) +/* +// Definition for a Node. +class Node { + int val; + Node next; + Node random; + + public Node(int val) { + this.val = val; + this.next = null; + this.random = null; + } +} +*/ + class Solution { + public Node copyRandomList(Node head) { + if (head == null) { + return null; + } + Node clone = head; + while (clone != null) { + Node temp = new Node(clone.val); + temp.next = clone.next; + clone.next = temp; + clone = temp.next; + } + clone = head; + while (clone != null) { + if (clone.random != null) { + clone.next.random = clone.random.next; + } + clone = clone.next.next; + } + clone = head.next; + Node temp = head; + Node newHead = clone; + while (temp != null && newHead != null) { + if (temp.next.next != null) { + temp.next = temp.next.next; + newHead.next = newHead.next.next; + } else { + temp.next = null; + } + temp = temp.next; + newHead = newHead.next; + } + + return clone; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/LeetCode/src/main/java/leetcode/editor/cn/LinkedListCycleLcci.java b/LeetCode/src/main/java/leetcode/editor/cn/LinkedListCycleLcci.java new file mode 100644 index 0000000..fd4f6ab --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/LinkedListCycleLcci.java @@ -0,0 +1,91 @@ +//给定一个链表,如果它是有环链表,实现一个算法返回环路的开头节点。 +// +// 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的 +//位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。 +// +// +// +// 示例 1: +// +// +// +// +//输入:head = [3,2,0,-4], pos = 1 +//输出:tail connects to node index 1 +//解释:链表中有一个环,其尾部连接到第二个节点。 +// +// +// 示例 2: +// +// +// +// +//输入:head = [1,2], pos = 0 +//输出:tail connects to node index 0 +//解释:链表中有一个环,其尾部连接到第一个节点。 +// +// +// 示例 3: +// +// +// +// +//输入:head = [1], pos = -1 +//输出:no cycle +//解释:链表中没有环。 +// +// +// +// 进阶: +// +// +// 你是否可以不用额外空间解决此题? +// +// +// +// Related Topics 链表 +// 👍 64 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.ListNode; + +import java.util.HashSet; +import java.util.Set; + +//面试题 02.08:环路检测 +public class LinkedListCycleLcci { + public static void main(String[] args) { + //测试代码 + Solution solution = new LinkedListCycleLcci().new Solution(); + } + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + + /** + * Definition for singly-linked list. + * class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ + public class Solution { + public ListNode detectCycle(ListNode head) { + Set listNodeSet = new HashSet<>(); + while (head != null) { + if (!listNodeSet.add(head)) { + break; + } else { + head = head.next; + } + } + return head; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/LeetCode/src/main/java/leetcode/editor/cn/MergeKSortedLists.java b/LeetCode/src/main/java/leetcode/editor/cn/MergeKSortedLists.java new file mode 100644 index 0000000..9bb9df7 --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/MergeKSortedLists.java @@ -0,0 +1,96 @@ +//给你一个链表数组,每个链表都已经按升序排列。 +// +// 请你将所有链表合并到一个升序链表中,返回合并后的链表。 +// +// +// +// 示例 1: +// +// 输入:lists = [[1,4,5],[1,3,4],[2,6]] +//输出:[1,1,2,3,4,4,5,6] +//解释:链表数组如下: +//[ +// 1->4->5, +// 1->3->4, +// 2->6 +//] +//将它们合并到一个有序链表中得到。 +//1->1->2->3->4->4->5->6 +// +// +// 示例 2: +// +// 输入:lists = [] +//输出:[] +// +// +// 示例 3: +// +// 输入:lists = [[]] +//输出:[] +// +// +// +// +// 提示: +// +// +// k == lists.length +// 0 <= k <= 10^4 +// 0 <= lists[i].length <= 500 +// -10^4 <= lists[i][j] <= 10^4 +// lists[i] 按 升序 排列 +// lists[i].length 的总和不超过 10^4 +// +// Related Topics 堆 链表 分治算法 +// 👍 1215 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.ListNode; + +//23:合并K个升序链表 +public class MergeKSortedLists{ + public static void main(String[] args) { + //测试代码 + Solution solution = new MergeKSortedLists().new Solution(); + } + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public ListNode mergeKLists(ListNode[] lists) { + ListNode result = null; + for (int i = 0; i < lists.length; i++) { + result = mergeTwoLists(result,lists[i]); + } + return result; + } + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { + if (l1 == null) { + return l2; + } + if (l2 == null) { + return l1; + } + if (l1.val < l2.val) { + l1.next = mergeTwoLists(l1.next, l2); + return l1; + } else { + l2.next = mergeTwoLists(l1, l2.next); + return l2; + } + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/LeetCode/src/main/java/leetcode/editor/cn/PartitionListLcci.java b/LeetCode/src/main/java/leetcode/editor/cn/PartitionListLcci.java new file mode 100644 index 0000000..f15353f --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/PartitionListLcci.java @@ -0,0 +1,51 @@ +//编写程序以 x 为基准分割链表,使得所有小于 x 的节点排在大于或等于 x 的节点之前。如果链表中包含 x,x 只需出现在小于 x 的元素之后(如下所示)。 +//分割元素 x 只需处于“右半部分”即可,其不需要被置于左右两部分之间。 +// +// 示例: +// +// 输入: head = 3->5->8->5->10->2->1, x = 5 +//输出: 3->1->2->10->5->5->8 +// +// Related Topics 链表 双指针 +// 👍 54 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.ListNode; + +//面试题 02.04:分割链表 +public class PartitionListLcci { + public static void main(String[] args) { + //测试代码 + Solution solution = new PartitionListLcci().new Solution(); + } + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + + /** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ + class Solution { + public ListNode partition(ListNode head, int x) { + ListNode temp = head; + ListNode current; + while (temp != null) { + while (temp.next != null && temp.next.val < x) { + current = temp.next; + temp.next = current.next; + current.next = head; + head = current; + } + temp = temp.next; + } + return head; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/LeetCode/src/main/java/leetcode/editor/cn/ReverseNodesInKGroup.java b/LeetCode/src/main/java/leetcode/editor/cn/ReverseNodesInKGroup.java new file mode 100644 index 0000000..b3fd2f8 --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/ReverseNodesInKGroup.java @@ -0,0 +1,118 @@ +//给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。 +// +// k 是一个正整数,它的值小于或等于链表的长度。 +// +// 如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。 +// +// 进阶: +// +// +// 你可以设计一个只使用常数额外空间的算法来解决此问题吗? +// 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。 +// +// +// +// +// 示例 1: +// +// +//输入:head = [1,2,3,4,5], k = 2 +//输出:[2,1,4,3,5] +// +// +// 示例 2: +// +// +//输入:head = [1,2,3,4,5], k = 3 +//输出:[3,2,1,4,5] +// +// +// 示例 3: +// +// +//输入:head = [1,2,3,4,5], k = 1 +//输出:[1,2,3,4,5] +// +// +// 示例 4: +// +// +//输入:head = [1], k = 1 +//输出:[1] +// +// +// +// +// +// 提示: +// +// +// 列表中节点的数量在范围 sz 内 +// 1 <= sz <= 5000 +// 0 <= Node.val <= 1000 +// 1 <= k <= sz +// +// Related Topics 链表 +// 👍 979 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.ListNode; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +//25:K 个一组翻转链表 +public class ReverseNodesInKGroup { + public static void main(String[] args) { + //测试代码 + Solution solution = new ReverseNodesInKGroup().new Solution(); + ListNode head = new ListNode(Arrays.asList(1, 2, 3, 4, 5)); + solution.reverseKGroup(head, 2); + } + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + + /** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ + class Solution { + public ListNode reverseKGroup(ListNode head, int k) { + List list = new ArrayList<>(); + while (head != null) { + list.add(head); + head = head.next; + } + int count = list.size() / k; + if (count == 0 || k == 1) { + return list.get(0); + } + for (int i = 0; i < count; i++) { + for (int j = i * k; j < (i + 1) * k; j++) { + if (j == i * k && i == count - 1) { + if(list.size() % k==0){ + list.get(j).next = null; + }else{ + list.get(j).next = list.get((i + 1) * k); + } + } else if (j == i * k) { + list.get(j).next = list.get((i + 2) * k - 1); + } else { + list.get(j).next = list.get(j - 1); + } + } + } + return list.get(k - 1); + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/LeetCode/src/main/java/leetcode/editor/cn/SumListsLcci.java b/LeetCode/src/main/java/leetcode/editor/cn/SumListsLcci.java new file mode 100644 index 0000000..70e7620 --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/SumListsLcci.java @@ -0,0 +1,85 @@ +//给定两个用链表表示的整数,每个节点包含一个数位。 +// +// 这些数位是反向存放的,也就是个位排在链表首部。 +// +// 编写函数对这两个整数求和,并用链表形式返回结果。 +// +// +// +// 示例: +// +// 输入:(7 -> 1 -> 6) + (5 -> 9 -> 2),即617 + 295 +//输出:2 -> 1 -> 9,即912 +// +// +// 进阶:思考一下,假设这些数位是正向存放的,又该如何解决呢? +// +// 示例: +// +// 输入:(6 -> 1 -> 7) + (2 -> 9 -> 5),即617 + 295 +//输出:9 -> 1 -> 2,即912 +// +// Related Topics 链表 数学 +// 👍 57 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.ListNode; + +//面试题 02.05:链表求和 +public class SumListsLcci { + public static void main(String[] args) { + //测试代码 + Solution solution = new SumListsLcci().new Solution(); + } + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) + + /** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ + class Solution { + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + int sum = 0; + ListNode head = l1; + ListNode temp = null; + while (l1 != null && l2 != null) { + sum += l1.val + l2.val; + l1.val = sum % 10; + sum = sum / 10; + if (l1.next == null) { + temp = l1; + } + l1 = l1.next; + l2 = l2.next; + } + while (l1 != null) { + sum += l1.val; + l1.val = sum % 10; + sum = sum / 10; + if (l1.next == null) { + temp = l1; + } + l1 = l1.next; + } + while (l2 != null && temp != null) { + sum += l2.val; + temp.next = new ListNode(sum % 10); + sum = sum / 10; + temp = temp.next; + l2 = l2.next; + } + if (sum > 0) { + temp.next = new ListNode(sum); + } + return head; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/LeetCode/src/main/java/leetcode/editor/cn/SwappingNodesInALinkedList.java b/LeetCode/src/main/java/leetcode/editor/cn/SwappingNodesInALinkedList.java new file mode 100644 index 0000000..baa2f32 --- /dev/null +++ b/LeetCode/src/main/java/leetcode/editor/cn/SwappingNodesInALinkedList.java @@ -0,0 +1,99 @@ +//给你链表的头节点 head 和一个整数 k 。 +// +// 交换 链表正数第 k 个节点和倒数第 k 个节点的值后,返回链表的头节点(链表 从 1 开始索引)。 +// +// +// +// 示例 1: +// +// +//输入:head = [1,2,3,4,5], k = 2 +//输出:[1,4,3,2,5] +// +// +// 示例 2: +// +// +//输入:head = [7,9,6,6,7,8,3,0,9,5], k = 5 +//输出:[7,9,6,6,8,7,3,0,9,5] +// +// +// 示例 3: +// +// +//输入:head = [1], k = 1 +//输出:[1] +// +// +// 示例 4: +// +// +//输入:head = [1,2], k = 1 +//输出:[2,1] +// +// +// 示例 5: +// +// +//输入:head = [1,2,3], k = 2 +//输出:[1,2,3] +// +// +// +// +// 提示: +// +// +// 链表中节点的数目是 n +// 1 <= k <= n <= 105 +// 0 <= Node.val <= 100 +// +// Related Topics 链表 +// 👍 13 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.ListNode; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +//1721:交换链表中的节点 +public class SwappingNodesInALinkedList { + public static void main(String[] args) { + Solution solution = new SwappingNodesInALinkedList().new Solution(); +// List list = Arrays.asList(1, 2, 3, 4, 5); + List list = Arrays.asList(7, 9, 6, 6, 7, 8, 3, 0, 9, 5); + solution.swapNodes(new ListNode(list), 5); + } + +//leetcode submit region begin(Prohibit modification and deletion) + + /** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ + class Solution { + public ListNode swapNodes(ListNode head, int k) { + List list = new ArrayList<>(); + ListNode temp = head; + while (temp != null) { + list.add(temp); + temp = temp.next; + } + int tem = list.get(k - 1).val; + list.get(k - 1).val = list.get(list.size() - k).val; + list.get(list.size() - k).val = tem; + return head; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file