repo
stringclasses
1k values
file_url
stringlengths
96
373
file_path
stringlengths
11
294
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
6 values
commit_sha
stringclasses
1k values
retrieved_at
stringdate
2026-01-04 14:45:56
2026-01-04 18:30:23
truncated
bool
2 classes
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution142.java
data_struct_study/src/LinkedList_problem/Solution142.java
package LinkedList_problem; /** * 环形链表 II:快慢指针+Floyd算法。 */ public class Solution142 { // 1、fast走的步数是slow步数的2倍:f=2s、fast比slow多走了n个环的长度:f=s+nb => s = nb // 2、走a+nb步一定是在环的入口 // 3、slow再走a = 入口 = head走到入口 = a // 4、由3得出,起始距离入口 = 第一次相遇位置 + a,所以,此时再次相遇时的节点即为环的入口节点 public ListNode detectCycle(ListNode head) { // 1、创建快慢指针 ListNode fast = head; ListNode slow = head; // 2、当fast不为null且fast.next不为null时,则移动fast两步、slow一步, // 当fast与slow相遇时,即得到第一次相遇的节点位置 while (fast != null && fast.next != null) { fast = fast.next.next; slow = slow.next; if (fast == slow) { break; } } // 3、异常处理:如果fast为null或者fast.next为null,说明没有环,直接返回null if (fast == null || fast.next == null) { return null; } // 4、将fast定位到head,再同时移动fast和slow,当第二次相遇时,相遇的节点即为环的入口节点 fast = head; while (fast != slow) { slow = slow.next; fast = fast.next; } return fast; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution141.java
data_struct_study/src/LinkedList_problem/Solution141.java
package LinkedList_problem; /** * 判断链表是否有环 */ public class Solution141 { // 双指针:时间复杂度O(n), 空间复杂度O(1) public boolean hasCycle(ListNode head) { // 1、异常处理:如果头结点为null,则返回false // 为什么不判断head.next为null?因为1个节点也可以构成环形链表,即自己指向自己 if (head == null) { return false; } // 2、创建两个指针节点,l1为head,l2为head的下一个节点 ListNode l1 = head, l2 = head.next; // 3、当l1、l2、l2.next都不为空,则移动两个链表来判断是否有环 while (l1 != null && l2 != null && l2.next != null) { // 1)、两个指针相遇,则说明有环 if (l1 == l2) { return true; } // 2)、l1指针移动1步,l2指针移动2步 l1 = l1.next; l2 = l2.next.next; } // 4、如果遍历到链表尾部,则说明没环 return false; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution328.java
data_struct_study/src/LinkedList_problem/Solution328.java
package LinkedList_problem; public class Solution328 { public ListNode oddEvenList(ListNode head) { if (head == null) { return null; } ListNode odd = head, even = head.next, evenHead = even; while (even != null && even.next != null) { odd.next = odd.next.next; odd = odd.next; even.next = even.next.next; even = even.next; } odd.next = evenHead; return head; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution2.java
data_struct_study/src/LinkedList_problem/Solution2.java
package LinkedList_problem; /** * 2: * 1、数字之外是否有前置的0。(除0以外,没有前置0) * 2、负数(不是)。 * * prev ListNode */ public class Solution2 { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode prev=new ListNode(0), l=prev; int carry=0; while(l1!=null||l2!=null){ int x=l1==null?0:l1.val; int y=l2==null?0:l2.val; int sum=x+y+carry; carry=sum/10; prev.next=new ListNode(sum%10); prev=prev.next; if(l1!=null) l1=l1.next; if(l2!=null) l2=l2.next; } if (carry>0) prev.next=new ListNode(carry); return l.next; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution143.java
data_struct_study/src/LinkedList_problem/Solution143.java
package LinkedList_problem; public class Solution143 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution160.java
data_struct_study/src/LinkedList_problem/Solution160.java
package LinkedList_problem; public class Solution160 { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode l1 = headA, l2 = headB; while (l1 != l2) { l1 = (l1 == null) ? headB : l1.next; l2 = (l2 == null) ? headA : l2.next; } return l1; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution_4_4.java
data_struct_study/src/LinkedList_problem/Solution_4_4.java
package LinkedList_problem; /** * 快慢指针: * 时间复杂度:O(n), 空间复杂度:O(1) */ public class Solution_4_4 { // 4、快慢指针解法:先使用快慢指针找到前半个链表的尾节点,然后翻转后半个部分链表, // 最后再使用双指针依次比较对应元素,如果不同则返回false,记得返回前需要还原链表 // 时间复杂度O(n), 空间复杂度O(1) public boolean isPail(ListNode head) { // 1、如果只有0或1个节点,则说明是回文结构 if (head == null || head.next == null) { return true; } // 2、通过快慢指针找到前半部分链表的尾节点:若链表有奇数个节点,则中间的节点应该看作是前半部分; // 然后反转后半部分链表 ListNode firstHalfEnd = endOfFirstHalf(head); ListNode secondHalfStart = reverseList(firstHalfEnd.next); // 3、判断是否回文:当后半部分的指针P2到达末尾时则比较完成 ListNode p1 = head; ListNode p2 = secondHalfStart; boolean result = true; while (result && p2 != null) { if (p1.val != p2.val) { result = false; } p1 = p1.next; p2 = p2.next; } // 4、还原链表并返回结果 firstHalfEnd.next = reverseList(secondHalfStart); return result; } private ListNode reverseList(ListNode head) { ListNode prev = null; ListNode curr = head; while (curr != null) { ListNode nextTemp = curr.next; curr.next = prev; prev = curr; curr = nextTemp; } return prev; } private ListNode endOfFirstHalf(ListNode head) { ListNode fast = head; ListNode slow = head; while (fast.next != null && fast.next.next != null) { fast = fast.next.next; slow = slow.next; } return slow; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution237.java
data_struct_study/src/LinkedList_problem/Solution237.java
package LinkedList_problem; /** * O(1) * O(1) */ public class Solution237 { public void deleteNode(ListNode node) { if (node == null || node.next == null) { throw new IllegalArgumentException("node must not null and node must not tail node!"); } node.val = node.next.val; node.next = node.next.next; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution_4_3.java
data_struct_study/src/LinkedList_problem/Solution_4_3.java
package LinkedList_problem; /** * 判断链表是否是一个回文结构 * * 时间复杂度:O(n) * 空间复杂度:O(n) */ public class Solution_4_3 { private ListNode frontPointer; // 3、递归:currentNode 指针是先到尾节点,由于递归的特性再从后往前进行比较。 // frontPointer 是递归函数外的指针。若 currentNode.val != frontPointer.val // 则返回 false。反之,frontPointer 向前移动并返回 true。 public boolean isPail(ListNode head) { // 初始化frontPointer frontPointer = head; return recursivelyCheck(head); } private boolean recursivelyCheck(ListNode currentNode) { if (currentNode != null) { // 1、递归到底时执行前后指针的判断 if (!recursivelyCheck(currentNode.next)) { return false; } if (currentNode.val != frontPointer.val) { return false; } // 2、更新frontPointer frontPointer = frontPointer.next; } return true; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution24.java
data_struct_study/src/LinkedList_problem/Solution24.java
package LinkedList_problem; /** * (p、node1、node2、(next) => 复杂的穿针引线) * O(n) * O(1) */ public class Solution24 { public ListNode swapPairs(ListNode head) { ListNode dummyHead = new ListNode(0); dummyHead.next = head; ListNode p = dummyHead; while (p.next != null && p.next.next != null) { ListNode node1 = p.next; ListNode node2 = node1.next; ListNode next = node2.next; node2.next = node1; node1.next = next; p.next = node2; p = node1; } return dummyHead.next; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution_4.java
data_struct_study/src/LinkedList_problem/Solution_4.java
package LinkedList_problem; import java.util.ArrayDeque; import java.util.Deque; /** * 判断链表是否是一个回文结构 * * 时间复杂度:O(n) * 空间复杂度:O(n) */ public class Solution_4 { // 1、Java双端队列解法:利用Deque的pollFirst和pollLast方法来比较, // 时间复杂度:O(n), 空间复杂度:O(n) public boolean isPail(ListNode head) { // 1、如果链表节点数为0或1,则返回true if (head == null || head.next == null) { return true; } // 2、将链表的节点都放入双端队列中 Deque<Integer> deque = new ArrayDeque<>(); ListNode cur = head; while (cur != null) { deque.add(cur.val); cur = cur.next; } // 3、当双端队列大于1时:利用Java双端队列的pollFirst和pollLast方法来判断 while (deque.size() > 1) { if (!deque.pollFirst().equals(deque.pollLast())) { return false; } } return true; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution_1.java
data_struct_study/src/LinkedList_problem/Solution_1.java
package LinkedList_problem; /** * 两个链表的公共节点 * * 两个链表长度分别为L1+C、L2+C, C为公共部分的长度,第一个人走了L1+C步后, * 回到第二个人起点走L2步;第2个人走了L2+C步后,回到第一个人起点走L1步。 * 当两个人走的步数都为L1+L2+C时这两个家伙就相爱了。 * * 时间复杂度:O(L1+L2+C) * 空间复杂度:O(1) */ public class Solution_1 { public ListNode findFirstCommonNode(ListNode pHead1, ListNode pHead2) { // 1、定义双指针 ListNode l1 = pHead1, l2 = pHead2; // 2、当l1和l2没有相遇时:同时移动l1和l2,当有一方到达尾部时, // 就会从另一端开始,当两者相遇时,此时的位置就是相交的位置了 while (l1 != l2) { l1 = (l1 == null) ? pHead2 : l1.next; l2 = (l2 == null) ? pHead1 : l2.next; } return l1; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution83.java
data_struct_study/src/LinkedList_problem/Solution83.java
package LinkedList_problem; /** * 删除链表中重复的节点 */ public class Solution83 { public ListNode deleteDuplicates(ListNode head) { // 1、如果当前节点为null或下一个节点为null时直接返回 if (head == null || head.next == null) { return head; } // 2、递归删除重复节点:如果当前节点与下一个节点值相等,则直接链接下一个节点 head.next = deleteDuplicates(head.next); return head.val == head.next.val ? head.next : head; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution92.java
data_struct_study/src/LinkedList_problem/Solution92.java
package LinkedList_problem; /** * (1、m 和 n 超过链表范围怎么办?2、m > n 怎么办?) */ public class Solution92 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution19.java
data_struct_study/src/LinkedList_problem/Solution19.java
package LinkedList_problem; /** * 19: * 1、n从0计还是从1计。 * 2、n不合法,负数或者大于链表长度如何处理(保证n合法)。 * * O(n) * O(1) */ public class Solution19 { // 1、先遍历一遍计算链表长度;再遍历一遍得到待删除节点的前一个节点并删除待删除节点 public ListNode removeNthFromEnd(ListNode head, int n) { // 1、使用虚拟头结点 ListNode dummyHead = new ListNode(0); dummyHead.next = head; // 2、先遍历一遍计算链表的长度 int len = 0; for (ListNode cur = dummyHead.next; cur != null; cur = cur.next) { len++; } // 3、获取待删除节点的前一个节点cur int k = len - n; assert k >= 0; ListNode cur = dummyHead; for (int i = 0; i < k; i++) { cur = cur.next; } // 4、删除指定节点:前一个节点指向待删除节点的后一个节点 cur.next = cur.next.next; return dummyHead.next; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution23.java
data_struct_study/src/LinkedList_problem/Solution23.java
package LinkedList_problem; import java.util.PriorityQueue; /** * 题目描述:给你一个链表数组,每个链表都已经按升序排列。 * 请你将所有链表合并到一个升序链表中,返回合并后的链表。 */ public class Solution23 { // 1、优先队列:用容量为K的最小堆优先队列,把链表的头结点都放进去,然后出队当前 // 优先队列中最小的,挂上链表,然后让 出队的那个节点指向的下一个节点 入队, // 如此反复,再出队当前优先队列中最小的,直到优先队列为空。 // 时间复杂度:O(n*log(k)),n是所有链表中元素的总和,k是链表个数。 public ListNode mergeKLists(ListNode[] lists) { // 1、异常处理:如果链表数组为null或长度为0,则直接返回null if (lists == null || lists.length == 0) { return null; } // 2、创建一个虚拟头结点与最小堆优先队列(o1.val - o2.val 为升序排序) ListNode dummyHead = new ListNode(0); ListNode cur = dummyHead; PriorityQueue<ListNode> pq = new PriorityQueue<>((o1, o2) -> o1.val - o2.val); // 3、遍历链表数组:如果链表不为null,则添加到优先队列中 for (ListNode list : lists) { if (list == null) { continue; } pq.add(list); } // 4、当优先队列不为空时 while (!pq.isEmpty()) { // 1)、取出队首的最小节点,并将cur节点指向它,然后移动cur节点到下一个位置 ListNode nextNode = pq.poll(); cur.next = nextNode; cur = cur.next; // 2)、如果队首的最小节点的下一个节点不为null,则添加其下一节点到优先队列中 if (nextNode.next != null) { pq.add(nextNode.next); } } return dummyHead.next; } // 2、分支法:分而治之,链表两两合并。时间复杂度:O(n*log(k)) public ListNode mergeKLists2(ListNode[] lists) { // 1、异常处理:如果链表数组为null或长度为0,则直接返回null if (lists == null || lists.length == 0) { return null; } return merge(lists, 0, lists.length - 1); } private ListNode merge(ListNode[] lists, int left, int right) { // 1、如果合并到左右指针都相等,则直接返回左指针处的链表 if (left == right) { return lists[left]; } // 2、分而治之:最后会递归得到2个单个链表,不断返回两两合并后的链表即可 int mid = left + (right - left) / 2; ListNode l1 = merge(lists, left, mid); ListNode l2 = merge(lists, mid + 1, right); return mergeTwoLists(l1, l2); } private 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; } } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution61.java
data_struct_study/src/LinkedList_problem/Solution61.java
package LinkedList_problem; public class Solution61 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution25.java
data_struct_study/src/LinkedList_problem/Solution25.java
package LinkedList_problem; /** * K 个一组翻转链表:不仅仅是穿针引线 * * 题目描述:给你一个链表,每 k 个节点一组进行翻转,请你返回翻转后的链表。 * k 是一个正整数,它的值小于或等于链表的长度。 * 如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序 * * 1、链表分为已翻转部分+待翻转部分+未翻转部分 * 2、每次翻转前,通过 k 次循环来确定翻转链表的范围 * 3、需记录翻转链表前驱和后继,方便翻转完成后把已翻转部分和未翻转部分连接起来 * 4、初始需要两个变量 pre 和 end,pre 代表待翻转链表的前驱,end 代表待翻转链表的末尾 * 5、经过k次循环,end 到达末尾,记录待翻转链表的后继 next = end.next * 6、翻转链表,然后将三部分链表连接起来,然后重置 pre 和 end 指针,然后进入下一次循环 * 7、特殊情况,当翻转部分长度不足 k 时,在定位 end 完成后,end==null,已经到达末尾,说明题目已完成,直接返回即可 * * 时间复杂度:O(n*k),最好的情况为O(n),最差的情况为 O(n^2) * 空间复杂度:O(1),除了几个必须的节点指针外,我们并没有占用其他空间 */ public class Solution25 { // 时间复杂度:O(n*k),最好情况为O(n), 最差情况为O(n^2) // 空间复杂度:O(1) public ListNode reverseKGroup(ListNode head, int k) { // 1、异常处理:如果头结点为null或下一个节点为null,则直接返回头结点 if (head == null || head.next == null){ return head; } // 2、创建一个虚拟头结点,并将其指向头结点 ListNode dummyHead = new ListNode(0); dummyHead.next = head; // 3、初始化pre和end都指向虚拟头结点, // pre表示前驱节点:每次待翻转链表的头结点的上一个节点, // end指每次待翻转的链表的尾节点 ListNode pre = dummyHead; ListNode end = dummyHead; // 4、当待翻转链表的尾节点的下一个节点不为null时 while (end.next != null) { // 1)、必须要循环k次循环找到待翻转链表的结尾,这里每次循环要判断end是否等于空, // 因为如果为空,end.next会报空指针异常。 // 例子:dummyHead->1->2->3->4->5 若k为2,循环2次,end指向2 for (int i = 0; i < k && end != null; i++){ end = end.next; } // 2)、如果end等于null,即表示待翻转链表的节点数小于k,不进行翻转。 if (end == null){ break; } // 3)、创建后继节点next(方便后面链接链表),然后断开链表, // 再创建待翻转链表头部节点 ListNode next = end.next; end.next = null; ListNode start = pre.next; // 4)、翻转链表,pre.next指向翻转后的链表 // 例子:1->2 变成2->1。 dummyHead->2->1 pre.next = reverse(start); // 5)、翻转后头节点变到最后,通过头结点的next指针把断开的链表重新链接。 start.next = next; // 6)、更新pre和end为下次待翻转链表头结点的上一个节点,即start pre = start; end = start; } return dummyHead.next; } // 链表翻转 public ListNode reverse(ListNode head) { // 1、异常处理:如果头结点为null或下一个节点为null,则直接返回头结点 if (head == null || head.next == null){ return head; } // 2、初始化前一个节点与当前节点,并定义下一个节点 ListNode preNode = null; ListNode curNode = head; ListNode nextNode; // 3、当当前节点不为null时进行链表翻转 while (curNode != null){ // 1)、初始化nextNode nextNode = curNode.next; // 2)、将当前节点的next指针指向前一个节点 curNode.next = preNode; // 3)、将preNode和curNode指针都向后移动 preNode = curNode; curNode = nextNode; } // 4、否则,当前节点为null,直接返回preNode即可 return preNode; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution234.java
data_struct_study/src/LinkedList_problem/Solution234.java
package LinkedList_problem; /** * (1、遍历存到数组、判断数组。2、双指针) * 回文链表: 快慢指针(整除器),把剩下的一半变成逆序,再进行比较。注意奇偶情况讨论。 */ class Solution234 { public boolean isPalindrome(ListNode head) { if(head==null) return true; ListNode prev=null; ListNode slow=head, fast=head; while(fast!=null&&fast.next!=null){ slow=slow.next; fast=fast.next.next; } if (fast!=null) slow=slow.next; while(slow!=null){ ListNode tmp=slow; slow=slow.next; tmp.next=prev; prev=tmp; } slow=prev;fast=head; while(slow!=null){ if(slow.val==fast.val){slow=slow.next; fast=fast.next;} else return false; } return true; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution206_2.java
data_struct_study/src/LinkedList_problem/Solution206_2.java
package LinkedList_problem; /** * 2、pre、cur、next + 递归 * 时间复杂度:O(n) * 空间复杂度:O(n) */ public class Solution206_2 { // Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } // 2、pre、cur、next + 递归 public ListNode reverseList(ListNode head) { // 1、递归终止条件:如果头结点和其下一个节点为null,则直接返回 if (head == null || head.next == null) { return head; } ListNode p = reverseList(head.next); // 2、递归到底,从后向前反转链表:5的next指向4,4的next指向null,即断开4的next,便于后续操作 // 5->4->null => 5->4->3->null => 5->4->3->2->1->null head.next.next = head; head.next = null; return p; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution21.java
data_struct_study/src/LinkedList_problem/Solution21.java
package LinkedList_problem; /** * 合并两个有序链表为一个有序链表:设立链表的虚拟头结点 */ public class Solution21 { // 时间复杂度:O(n+m),函数 mergeTwoList 至多只会递归调用每个节点一次。 // 因此,时间复杂度取决于合并后的链表长度。 // 空间复杂度:O(n+m),递归调用 mergeTwoLists 函数时需要消耗栈空间,栈空间的大小 // 取决于递归调用的深度。结束递归调用时 mergeTwoLists 函数最多调用 n+m 次。 public ListNode mergeTwoLists(ListNode l1, ListNode l2) { // 1、当l1或l2为空时的直接返回另一个链表 if (l1 == null) { return l2; } if (l2 == null) { return l1; } // 2、判断 l1 和 l2 哪一个链表的头节点的值更小,然后递归地决定 // 下一个添加到结果里的节点。如果两个链表有一个为空,递归结束 if (l1.val < l2.val) { l1.next = mergeTwoLists(l1.next, l2); return l1; } else { l2.next = mergeTwoLists(l1, l2.next); return l2; } } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution206.java
data_struct_study/src/LinkedList_problem/Solution206.java
package LinkedList_problem; /** * 1、pre、cur、next + 循环: * * 在遍历列表时,将当前节点的 next 指针改为指向前一个元素。 * 由于节点没有引用其上一个节点,因此必须事先存储其前一个元素。在更改引用之前,还 * 需要另一个指针来存储下一个节点。不要忘记在最后返回新的头引用! * * 时间复杂度:O(n) * 空间复杂度:O(1) */ public class Solution206 { // Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } // 1、pre、cur、next + 循环 public ListNode reverseList(ListNode head) { ListNode pre = null; ListNode cur = head; while (cur != null) { ListNode next = cur.next; cur.next = pre; pre = cur; cur = next; } return pre; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution86.java
data_struct_study/src/LinkedList_problem/Solution86.java
package LinkedList_problem; public class Solution86 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution445.java
data_struct_study/src/LinkedList_problem/Solution445.java
package LinkedList_problem; import java.util.Stack; /** * 题目描述:给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。 * 它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。 * 你可以假设除了数字 0 之外,这两个数字都不会以零开头。 * * 进阶:如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。 * * 示例: * 输入:(7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) * 输出:7 -> 8 -> 0 -> 7 * * 1、如果不允许修改输入的链表呢?数字之外是否有前置的0?除0以外,没有前置0、负数?不是。 * 2、使用辅助的数据结构。 */ public class Solution445 { // 本题的主要难点在于链表中数位的顺序与我们做加法的顺序是相反的, // 为了逆序处理所有数位,我们可以使用栈:把所有数字压入栈中, // 再依次取出相加。计算过程中需要注意进位的情况。 // 时间复杂度:O(max(m, n)), 其中 m 与 n 分别为两个链表的长度。我们需要遍历每个链表。 // 空间复杂度:O(m+n), 我们把链表内容放入栈中所用的空间。 public ListNode addTwoNumbers(ListNode l1, ListNode l2) { // 1、使用 stack 存储链表数据,以便从后往前进行计算 Stack<Integer> stack1 = buildStack(l1); Stack<Integer> stack2 = buildStack(l2); // 2、创建虚拟头结点便于后面计算和进位值变量 ListNode dummyHead = new ListNode(-1); int c = 0; // 3、当stack1或stack2或c不为空时 // 例子:dummyHead->null => dummyHead->7->null => dummyHead->0->7->null => dummyHead->8->0->7->null => dummyHead->7->8->0->7->null while (!stack1.isEmpty() || !stack2.isEmpty() || c != 0) { // 1、计算当前链表值 int x = stack1.isEmpty() ? 0 : stack1.pop(); int y = stack2.isEmpty() ? 0 : stack2.pop(); int sum = x + y + c; // 2、根据当前值创建对应的链表 ListNode node = new ListNode(sum % 10); // 3、将当前节点插入dummyHead后面:当前节点指向dummyHead的下一个节点, // 然后重置dummyHead到链表头部位置 node.next = dummyHead.next; dummyHead.next = node; // 4、计算进位c c = sum / 10; } return dummyHead.next; } private Stack<Integer> buildStack(ListNode head) { Stack<Integer> stack = new Stack<>(); while (head != null) { stack.push(head.val); head = head.next; } return stack; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution19_2.java
data_struct_study/src/LinkedList_problem/Solution19_2.java
package LinkedList_problem; /** * 19: * 1、n从0计还是从1计。 * 2、n不合法,负数或者大于链表长度如何处理(保证n合法)。 * * O(n) * O(1) */ public class Solution19_2 { // 2、双指针:设立两个指针 p、q,他们的距离为n,当 q 遍历到 null 时,p.next 即为 delNode public ListNode removeNthFromEnd(ListNode head, int n) { // 1、设立虚拟头结点,便于后续循环的计算 ListNode dummyHead = new ListNode(0); dummyHead.next = head; // 2、创建双指针p、q ListNode p = dummyHead; ListNode q = dummyHead; // 3、先移动q:为后续获取待删除的前一个节点,需执行 n + 1 次遍历,而不是 n 次 for (int i = 0; i < n + 1; i++) { assert q != null; q = q.next; } // 4、再同时移动q、p:当 q == null 时,p 即为待删除的前一个节点 while (q != null) { q = q.next; p = p.next; } // 5、将待删除的前一个节点的指针指向后一个节点 p.next = p.next.next; return dummyHead.next; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution203.java
data_struct_study/src/LinkedList_problem/Solution203.java
package LinkedList_problem; /** * 使用虚拟头结点 * O(n) * O(1) */ public class Solution203 { // Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public ListNode removeElements(ListNode head, int val) { ListNode dummyHead = new ListNode(0); dummyHead.next = head; ListNode cur = dummyHead; while (cur.next != null) { if (cur.next.val == val) { ListNode delNode = cur.next; cur.next = delNode.next; } else { cur = cur.next; } } return dummyHead.next; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution147.java
data_struct_study/src/LinkedList_problem/Solution147.java
package LinkedList_problem; public class Solution147 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution_3.java
data_struct_study/src/LinkedList_problem/Solution_3.java
package LinkedList_problem; /** * 题目描述:输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯, * 本题从1开始计数,即链表的尾节点是倒数第1个节点。例如,一个链表有6个节点, * 从头节点开始,它们的值依次是1、2、3、4、5、6。 * 这个链表的倒数第3个节点是值为4的节点。 * * 设链表的长度为 N。设置两个指针 P1 和 P2,先让 P1 移动 K 个节点, * 则还有 N - K 个节点可以移动。此时让 P1 和 P2 同时移动, * 可以知道当 P1 移动到链表结尾时,P2 移动到第 N - K 个节点处, * 该位置就是倒数第 K 个节点。 * * 时间复杂度:O(n) * 空间复杂度:O(1) */ public class Solution_3 { public ListNode findKthToTail(ListNode head, int k) { // 1、异常处理:如果head==null,直接返回 if (head == null) { return null; } // 2、初始化第一个指针P1,然后移动P1 k步 ListNode P1 = head; while (P1 != null && k-- > 0) { P1 = P1.next; } // 3、异常处理:如果还没走完k步就到链表尾部了,直接返回null if (k > 0) { return null; } // 4、初始化第二个指针P2,当P1不等于null时,则同时移动P1、P2, // 当P1等于null时,P2就是倒数第k个节点 ListNode P2 = head; while (P1 != null) { P1 = P1.next; P2 = P2.next; } return P2; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Main.java
data_struct_study/src/LinkedList_problem/Main.java
package LinkedList_problem; /** * 链表,在节点间穿针引线 * * JsonChao的链表核心题库:22题 */ public class Main { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution82.java
data_struct_study/src/LinkedList_problem/Solution82.java
package LinkedList_problem; /** * 设立链表的虚拟头结点 */ public class Solution82 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/ListNode.java
data_struct_study/src/LinkedList_problem/ListNode.java
package LinkedList_problem; // Definition for singly-linked list. // 在Java版本中,我们将LinkedList相关的测试辅助函数写在ListNode里 public class ListNode { public int val; public ListNode next = null; public ListNode(int x) { val = x; } // 根据n个元素的数组arr创建一个链表 // 使用arr为参数,创建另外一个ListNode的构造函数 public ListNode (int[] arr){ if(arr == null || arr.length == 0) throw new IllegalArgumentException("arr can not be empty"); this.val = arr[0]; ListNode curNode = this; for(int i = 1 ; i < arr.length ; i ++){ curNode.next = new ListNode(arr[i]); curNode = curNode.next; } } // 返回以当前ListNode为头结点的链表信息字符串 @Override public String toString(){ StringBuilder s = new StringBuilder(""); ListNode curNode = this; while(curNode != null){ s.append(Integer.toString(curNode.val)); s.append(" -> "); curNode = curNode.next; } s.append("NULL"); return s.toString(); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList_problem/Solution_2.java
data_struct_study/src/LinkedList_problem/Solution_2.java
package LinkedList_problem; /** * 单链表的选择排序: * * 时间复杂度:O(n^2) * 空间复杂度:O(1) */ public class Solution_2 { // 根据选择排序的思想寻找最小的元素,并利用头插法插入到节点 public ListNode sortInList (ListNode head) { // 1、创建一个虚拟头结点并将它指向真正的头结点,再赋值给已排序的链表 ListNode dummyHead = new ListNode(Integer.MAX_VALUE); dummyHead.next = head; ListNode sorted = dummyHead; // 2、当已排序链表还有下一个元素时,即移动到链表尾部时已经排好序 while (sorted.next != null) { // 1)、创建pre、cur、pre_min、min便于后续的插入操作 ListNode pre = sorted; ListNode cur = sorted.next; ListNode pre_min = null; ListNode min = null; // 2)、当cur不等于null时:寻找最小的数min与pre_min while (cur != null) { if (min == null || cur.val < min.val) { min = cur; pre_min = pre; } // 1)、更新cur和pre:继续向后移动指针 cur = cur.next; pre = pre.next; } // 3)、将当前min从排序链表中移除,并利用头插法插入 // 例子:dummyHead->head => dummyHead->min->head => dummyHead->新min->min->head pre_min.next = min.next; min.next = sorted.next; sorted.next = min; // 4)、哨兵节点往后移动 sorted = min; } return dummyHead.next; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/array/Student.java
data_struct_study/src/array/Student.java
package array; public class Student { private String name; private int score; public Student(String name, int score) { this.name = name; this.score = score; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", score=" + score + '}'; } public static void main(String[] args) { Array<Student> array = new Array<>(); array.addLast(new Student("JsonChao", 99)); array.addLast(new Student("Awesome-Android", 100)); array.addLast(new Student("Awesome-Android2", 100)); System.out.println(array); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/array/Array.java
data_struct_study/src/array/Array.java
package array; /** * 数组最大的优点:快速查询 * 数组最好应用于"索引有语义"的情况(但并非所有有语义的索引都适用于数组,例如身份证号码) * 我们需要额外处理索引有语义的情况 * 数组的容量:capacity,数组的大小:size,初始为0 * * 1、实现基本功能:增删改查、各种判断方法等等 * 2、使用 泛型 让我们的数据结构可以放置 "任何"(不可以是基本 * 数据类型,只能是类对象,好在每一个基本类型都有一个对应的 * 包装类,它们之间可以自动进行装箱和拆箱的操作) 的数据类型 * 3、数组的扩容与缩容 * * 手写时的易错点: * 1、注意数组的下标 * 2、注意 size 与 capacity 的区别 * * 简单的时间复杂度分析: * 为什么要用大O,叫做大O(n)? * 忽略了常数,实际时间 T = c1 * n + c2 * 为甚不加上其中每一个常数项,而是要忽略它? * 比如说把一个数组中的元素取出来这个操作,很有可能不同的语音基于不同的实现,它实际运行的时间是不等的。 * 就算转换成机器码,它对应的那个机器码的指令数也有可能是不同的。就算指令数是相同的,同样一个指令在 CPU * 的底层,你使用的 CPU 不同,很有可能执行的操作也是不同的。所以,在实际上我们大概能说出来这个 c1 * 是多少,但是很难准确判断其具体的值。 * 大O的表示的是渐进时间复杂度,当 n 趋近于无穷大的时候,其算法谁快谁慢。 * 增:O(n) * 删:O(n) * 改:已知索引 O(1);未知索引 O(n) * 查:已知索引 O(1);未知索引 O(n) * * 均摊时间复杂度分析: * 假设 capacity = n,n + 1 次 addLast/removeLast,触发 resize,总共进行 2n + 1 次基本操作 * 平均来看,每次 addLast/removeLast 操作,进行 2 次基本操作 * 均摊计算,时间复杂度为 O(1) * * 复杂度震荡: * 当反复先后调用 addLast/removeLast 进行操作时,会不断地进行 扩容/缩容,此时时间复杂度为 O(n) * 出现问题的原因:removeLast 时 resize 过于着急 * 解决方案:Lazy (在这里是 Lazy 缩容) * 等到 size == capacity / 4 时,才将 capacity 减半 * * Array 泛型中盛放的是 E 类型的数据 */ public class Array<E> { /** * capacity 即为 data.length */ private E[] data; /** * size 即为 当前数组的大小 */ private int size; /** * 构造函数:创建一个具有 capacity 容量的数组 * @param capacity 容量大小 */ public Array(int capacity) { // Object 这里导入了别的 Object 接口(org.omg.CORBA.Object), // 导致浪费了半小时查找错误。。 data = (E[]) new Object[capacity]; size = 0; } /** * 构造函数:将静态数组 arr 转换成动态数据 * * @param arr 静态数组 */ public Array(E[] arr) { data = (E[])new Object[arr.length]; for (int i = 0; i < arr.length; i++) { data[i] = arr[i]; } size = arr.length; } /** * 默认构造函数:数组容量大小为 10 */ public Array() { this(10); } /** * 获取数组的容量 * @return 容量 */ public int getCapacity() { return data.length; } /** * 获取数组的大小 */ public int getSize() { return size; } /** * 判断数组是否为空 */ public boolean isEmpty() { return size == 0; } /** * 向数组最后添加一个元素 * 时间复杂度:O(1) */ public void addLast(E e) { add(size, e); } /** * 向数组开始添加一个元素 * 时间复杂度:O(n) */ public void addFirst(E e) { add(0, e); } /** * 向数组指定位置添加一个元素 * 时间复杂度:O(n) * 数组的添加操作的总体时间复杂度为 O(n),需要考虑最坏的情况 */ public void add(int position, E e) { /** * 插入的形式只有如下三种: * 1、从开始插入元素:data[0] * 2、从最后插入元素:data[size] * 3、从中间插入元素:data[0~size] */ if ((position < 0 || position > size)) { throw new IllegalArgumentException("add failed, position is illegal"); } if (size == data.length) { // 数组扩容,Java 中的扩容机制是 1.5 倍 resize(data.length * 2); } for (int i = size - 1; i >= position; i--) { data[i + 1] = data[i]; } data[position] = e; size++; } /** * 扩容/缩容 * 时间复杂度:O(n) * @param capacity */ private void resize(int capacity) { E[] newData = (E[]) new Object[capacity]; for (int i = 0; i < size; i++) { newData[i] = data[i]; } data = newData; } /** * 查询数组中最后一个位置的元素 * * @return 数组中的元素 */ public E queryLast() { return query(getSize() - 1); } /** * 查询数组中第一个位置的元素 * * @return 数组中的元素 */ public E queryFirst() { return query(0); } /** * 查询数组中指定位置的元素 * * @return 数组中的元素 */ public E query(int position) { if (position < 0 || position >= size) { throw new IllegalArgumentException("position is illegal~"); } return data[position]; } /** * 修改数组中指定位置的元素 */ public void set(int position, E e) { if (position < 0 || position >= size) { throw new IllegalArgumentException("position is illegal~"); } data[position] = e; } /** * 数组中是否包含某元素 * * @return 是否包含某元素 */ public boolean contains(E e) { for (int i = 0; i < size; i++) { if (data[i].equals(e)) { return true; } } return false; } /** * 查找数组中对应元素的下标 * * @return 对应元素的下标 */ public int find(E e) { for (int i = 0; i < size; i++) { if (data[i].equals(e)) { return i; } } return -1; } /** * 删除数组中最开始的元素 * * @return 被删除的元素 */ public E deleteFirst() { return delete(0); } /** * 删除数组中最后的元素 * * @return 被删除的元素 */ public E deleteLast() { return delete(size - 1); } /** * 删除数组中指定的元素 * * @param e 被删除的元素 * @return 是否删除成功 */ public boolean deleteElement(E e) { int index = find(e); if (index != -1) { delete(index); return true; } return false; } /** * 删除数组中指定下标的元素 * * @return 被删除的元素 */ public E delete(int position) { if ((position < 0 || position >= size)) { throw new IllegalArgumentException("position is illegal!"); } E deleteE = data[position]; for (int i = position + 1; i < size; i++) { data[i - 1] = data[i]; } size--; // 节省空间 data[size] = null; // 防止复杂度的震荡,同时注意处理 data.length / 2 = 0(即 data.length < 2) 的情况 if (size == data.length / 4 && data.length / 2 != 0) { // 数组的缩容 resize(data.length / 2); } return deleteE; } public void swap(int i, int j) { if (i < 0 || i >= size || j < 0 || j >= size) { throw new IllegalArgumentException("i or j is illegal access~"); } E e = data[i]; data[i] = data[j]; data[j] = e; } @Override public String toString() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(String.format("Array size is %d, length is %d\n", size, data.length)); stringBuilder.append("["); for (int i = 0; i < size; i++) { stringBuilder.append(data[i]); if (i != size - 1) { stringBuilder.append(","); } } stringBuilder.append("]"); return stringBuilder.toString(); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/array/Main.java
data_struct_study/src/array/Main.java
package array; public class Main { public static void main(String[] args) { Array<Integer> array = new Array<>(10); for (int i = 0; i < 10; i++) { array.addLast(i); } System.out.println(array); array.addLast(66); System.out.println(array); array.add(1, -100); System.out.println(array); boolean contains = array.contains(30); System.out.println(contains); boolean contains1 = array.contains(66); System.out.println(contains1); array.delete(3); System.out.println(array); array.deleteLast(); System.out.println(array); array.deleteFirst(); System.out.println(array); boolean b = array.deleteElement(10); System.out.println(b); boolean b1 = array.deleteElement(-100); System.out.println(array); System.out.println(b1); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution_1.java
data_struct_study/src/other_problem/Solution_1.java
package other_problem; /** * 题目描述:将给出的32位整数x翻转。 * 例1:x=123,返回321 * 例2:x=-123,返回-321 * 你有注意到翻转后的整数可能溢出吗?因为给出的是32位整数,则其数值范围为[−2^{31}, 2^{31} − 1]。 * 翻转可能会导致溢出,如果反转后的结果会溢出就返回0。 * * 关键点是如何判断溢出。 * 推荐解答用的是用long类型存储结果,如果结果大于0x7fffffff或者小于0x80000000就溢出 * 我的解法是每次计算新的结果时,再用逆运算判断与上一次循环的结果是否相同,不同就溢出 */ public class Solution_1 { public int reverse(int x) { // 1、创建一个返回变量res int res = 0; // 2、当x不等于0时,进行反转操作 while (x != 0){ // 1)、初始化tail记录当前最后一位(-123%10==-3),newRes记录中间反转值 int tail = x % 10; int newRes = res * 10 + tail; // 2)、异常处理:如果 (newRes-tail)/10!=res 说明产生了溢出, // 为什么?因为当newRes的值超过最大位数时,系统会将超过 // 最大位数的高位二进制数扔掉,从而导致了newRes!=res*10+tail。 // 此时计算的(newRes-tail)/10自然也就不等于res了。 if ((newRes - tail) / 10 != res) { return 0; } // 3)、更新res与x res = newRes; x = x / 10; } // 3、直到x等于0才返回res return res; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution146.java
data_struct_study/src/other_problem/Solution146.java
package other_problem; import java.util.LinkedHashMap; /** * LRU Cache 146: * * 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制 。 * 实现 LRUCache 类: * LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存 * int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。 * void put(int key, int value) 如果关键字已经存在,则变更其数据值; * 如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时, * 它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。 * * 最简单的方式是直接继承LinkedHashMap,并重写getOrDefault、put、removeEldestEntry方法。 * 时间复杂度:put/get都是O(1) * 空间复杂度:O(capacity) */ class Solution146 { int capacity; // 初始化LinkedHashMap作为cache LinkedHashMap<Integer, Integer> cache = new LinkedHashMap<>(); public Solution146(int capacity) { // 初始化容量 this.capacity = capacity; } public int get(int key) { // 1、如果cache中不包含key,则返回-1 if (!cache.containsKey(key)) { return -1; } // 2、否则将 key 变为最近使用,然后再从cache中获取对应值 makeRecently(key); return cache.get(key); } public void put(int key, int val) { // 1、如果cache中包含key,则覆盖key的值,并将key变为最近使用的 if (cache.containsKey(key)) { cache.put(key, val); makeRecently(key); return; } // 2、如果cache大小大于等于容量,则移除链表头部那个最近最久未使用的值, // 然后再将新值添加到链表尾部 if (cache.size() >= this.capacity) { int oldestKey = cache.keySet().iterator().next(); cache.remove(oldestKey); } cache.put(key, val); } // 将value变为最近使用的:删除 key,重新插入到链表尾部 private void makeRecently(int key) { int val = cache.get(key); cache.remove(key); cache.put(key, val); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution393.java
data_struct_study/src/other_problem/Solution393.java
package other_problem; public class Solution393 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution69.java
data_struct_study/src/other_problem/Solution69.java
package other_problem; public class Solution69 { public int mySqrt(int x) { if (x <= 1) { return x; } int l = 0, h = x; while (l <= h) { int mid = l + (h - l) / 2; int sqrt = x / mid; if (mid == sqrt) { return mid; } else if (mid > sqrt) { h = mid - 1; } else { l = mid + 1; } } return h; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution169.java
data_struct_study/src/other_problem/Solution169.java
package other_problem; import java.util.Arrays; public class Solution169 { public int majorityElement(int[] nums) { Arrays.sort(nums); return nums[nums.length / 2]; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution169_2.java
data_struct_study/src/other_problem/Solution169_2.java
package other_problem; public class Solution169_2 { // 多数元素:我将这种算法称为 【抱对自杀】,每当一个众数碰到一个非众数就会抱住, // 然后双双自杀。如果没有遇到足够的非众数,众数们就会等人到齐了再抱对自杀。 // 最后留下来的当然是众数。 public int majorityElement(int[] nums) { int cnt = 0, majority = nums[0]; for (int num:nums) { majority = (cnt == 0) ? num : majority; cnt = (majority == num) ? cnt + 1 : cnt - 1; } return majority; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution176.java
data_struct_study/src/other_problem/Solution176.java
package other_problem; public class Solution176 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution432.java
data_struct_study/src/other_problem/Solution432.java
package other_problem; public class Solution432 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution231_2.java
data_struct_study/src/other_problem/Solution231_2.java
package other_problem; /** * 利用 1000 & 0111 == 0 */ public class Solution231_2 { public boolean isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution136.java
data_struct_study/src/other_problem/Solution136.java
package other_problem; /** * 两个相同的数异或的结果为 0,对所有数进行异或操作,最后的结果就是单独出现的那个数。 */ public class Solution136 { public int singleNumber(int[] nums) { int res = 0; for (int num:nums) { res = res ^ num; } return res; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution292.java
data_struct_study/src/other_problem/Solution292.java
package other_problem; public class Solution292 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Main.java
data_struct_study/src/other_problem/Main.java
package other_problem; /** * Bloom Filter 布隆过滤器: * 1、一个很长的二进制向量和一个映射函数。 * 2、用于检索一个元素是否在一个集合中。 * 3、优点是空间和查询时间效率越超一般算法,缺点是有一定的误识别率(仅当存在时)和删除困难, * 所以仅仅是一个预先处理模块。 * * 位运算操作: * 1、X & 1 == 1 OR == 0 判断奇偶(X % 2 == 1) * 2、X = X & (X-1) => 清零最低位的1 * 3、X & -X => 得到最低位的1。 * 0s 表示一串 0,1s 表示一串 1。 * * ``` * x ^ 0s = x x & 0s = 0 x | 0s = x * x ^ 1s = ~x x & 1s = x x | 1s = 1s * x ^ x = 0 x & x = x x | x = x * ``` * * 利用 x ^ 1s = \~x 的特点,可以将一个数的位级表示翻转;利用 x ^ x = 0 的特点,可以将三个数中重复的两个数去除,只留下另一个数。 * * ``` * 1^1^2 = 2 * ``` * * 利用 x & 0s = 0 和 x & 1s = x 的特点,可以实现掩码操作。一个数 num 与 mask:00111100 进行位与操作,只保留 num 中与 mask 的 1 部分相对应的位。 * * ``` * 01011011 & * 00111100 * -------- * 00011000 * ``` * * 利用 x | 0s = x 和 x | 1s = 1s 的特点,可以实现设值操作。一个数 num 与 mask:00111100 进行位或操作,将 num 中与 mask 的 1 部分相对应的位都设置为 1。 * * ``` * 01011011 | * 00111100 * -------- * 01111111 * ``` * * **位与运算技巧** * * n&(n-1) 去除 n 的位级表示中最低的那一位 1。例如对于二进制表示 01011011,减去 1 得到 01011010,这两个数相与得到 01011010。 * * ``` * 01011011 & * 01011010 * -------- * 01011010 * ``` * * n&(-n) 得到 n 的位级表示中最低的那一位 1。-n 得到 n 的反码加 1,也就是 -n=\~n+1。例如对于二进制表示 10110100,-n 得到 01001100,相与得到 00000100。 * * ``` * 10110100 & * 01001100 * -------- * 00000100 * ``` * * n-(n&(-n)) 则可以去除 n 的位级表示中最低的那一位 1,和 n&(n-1) 效果一样。 * * **移位运算** * * \>\> n 为算术右移,相当于除以 2n,例如 -7 \>\> 2 = -2。 * * ``` * 11111111111111111111111111111001 >> 2 * -------- * 11111111111111111111111111111110 * ``` * * \>\>\> n 为无符号右移,左边会补上 0。例如 -7 \>\>\> 2 = 1073741822。 * * ``` * 11111111111111111111111111111001 >>> 2 * -------- * 00111111111111111111111111111111 * ``` * * << n 为算术左移,相当于乘以 2n。-7 << 2 = -28。 * * ``` * 11111111111111111111111111111001 << 2 * -------- * 11111111111111111111111111100100 * ``` * * **mask 计算** * * 要获取 111111111,将 0 取反即可,\~0。 * * 要得到只有第 i 位为 1 的 mask,将 1 向左移动 i-1 位即可,1<<(i-1) 。例如 1<<4 得到只有第 5 位为 1 的 mask :00010000。 * * 要得到 1 到 i 位为 1 的 mask,(1<<i)-1 即可,例如将 (1<<4)-1 = 00010000-1 = 00001111。 * * 要得到 1 到 i 位为 0 的 mask,只需将 1 到 i 位为 1 的 mask 取反,即 \~((1<<i)-1)。 * * **Java 中的位操作** * * ```html * static int Integer.bitCount(); // 统计 1 的数量 * static int Integer.highestOneBit(); // 获得最高位 * static String toBinaryString(int i); // 转换为二进制表示的字符串 * ``` * * 面试答题四件套: * 1、询问:题目细节、边界条件、可能的极端错误情况。 * 2、可能的解决方案:1)、时间 & 空间。2)、最优解。 * 3、编码。 * 4、测试用例。 * * JsonChao的其它核心题库:11题 */ public class Main { public static void main(String[] args) { } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution9.java
data_struct_study/src/other_problem/Solution9.java
package other_problem; /** * 要求不能使用额外空间,也就不能将整数转换为字符串进行判断。 * * 将整数分成左右两部分,右边那部分需要转置,然后判断这两部分是否相等。 */ public class Solution9 { public boolean isPalindrome(int x) { if (x == 0) { return true; } if (x < 0 || x % 10 == 0) { return false; } int right = 0; while (right < x) { right = right * 10 + x % 10; x /= 10; } return x == right || x == right / 10; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution7.java
data_struct_study/src/other_problem/Solution7.java
package other_problem; public class Solution7 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution_2.java
data_struct_study/src/other_problem/Solution_2.java
package other_problem; import java.util.LinkedList; import java.util.Queue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicInteger; /** * 生产者消费者 */ public class Solution_2 { // 1、定义一个Message类 public class Message { private int code; private String msg; // Handler target; public Message() { } public Message(int code, String msg) { this.code = code; this.msg = msg; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } } // 2、定义一个IMessageQueue接口:包含next与enqueueMessage抽象方法 public interface IMessageQueue { Message next() throws InterruptedException; void enqueueMessage(Message message) throws InterruptedException; } // 3、阻塞队列LinkedBlockingQueue实现阻塞式生产者消费者模式:take/put 是阻塞方法 public class MessageQueue implements IMessageQueue { private final BlockingQueue<Message> queue; public MessageQueue(int cap) { this.queue = new LinkedBlockingQueue<>(cap); } public Message next() throws InterruptedException { return queue.take(); } public void enqueueMessage(Message message) { try { queue.put(message); } catch (InterruptedException e) { e.printStackTrace(); } } } // 4、链表 + synchronized + wait/notifyAll + volatile变量 + 对象锁实现非阻塞式的生产者消费者模式 public class MessageQueue1 implements IMessageQueue { private Queue<Message> queue; private volatile int count; private final Object BUFFER_LOCK = new Object(); public MessageQueue1(int cap) { this.count = cap; queue = new LinkedList<>(); } @Override public Message next() throws InterruptedException { synchronized (BUFFER_LOCK) { while (queue.size() == 0) { BUFFER_LOCK.wait(); } Message message = queue.poll(); BUFFER_LOCK.notifyAll(); return message; } } @Override public void enqueueMessage(Message message) throws InterruptedException { synchronized (BUFFER_LOCK) { while (queue.size() == count) { BUFFER_LOCK.wait(); } queue.offer(message); BUFFER_LOCK.notifyAll(); } } } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/other_problem/Solution231.java
data_struct_study/src/other_problem/Solution231.java
package other_problem; /** * 二进制表示只有一个 1 存在。 */ public class Solution231 { public boolean isPowerOfTwo(int n) { return n > 0 && Integer.bitCount(n) == 1; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/map/Map.java
data_struct_study/src/map/Map.java
package map; public interface Map<K, V> { void add(K key, V value); V remove(K key); boolean contains(K key); V get(K key); void set(K key, V newValue); int getSize(); boolean isEmpty(); }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/map/Solution1.java
data_struct_study/src/map/Solution1.java
package map; import java.util.ArrayList; import java.util.TreeMap; /** * Leetcode 350. Intersection of Two Arrays II * https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ */ class Solution1 { public int[] intersect(int[] nums1, int[] nums2) { TreeMap<Integer, Integer> map = new TreeMap<>(); for (Integer item:nums1) { if (!map.containsKey(item)) { map.put(item, 1); } else { map.put(item, map.get(item) + 1); } } ArrayList<Integer> list = new ArrayList<>(); for (Integer item:nums2) { if (map.containsKey(item)) { list.add(item); map.put(item, map.get(item) - 1); if (map.get(item) == 0) { map.remove(item); } } } int[] res = new int[list.size()]; for (int i = 0; i < list.size(); i++) { res[i] = list.get(i); } return res; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/map/LinkedListMap.java
data_struct_study/src/map/LinkedListMap.java
package map; import set.FileOperation; import java.util.ArrayList; public class LinkedListMap<K, V> implements Map<K, V> { private class Node { K key; V value; Node next; public Node(K key, V value, Node next) { this.key = key; this.value = value; this.next = next; } public Node(K key, V value) { this(key, value, null); } public Node() { this(null, null, null); } @Override public String toString() { return key.toString() + " : " + value.toString(); } } private Node dummyHead; private int size; public LinkedListMap() { this.dummyHead = new Node(); this.size = 0; } private Node getNode(K k) { Node pre = dummyHead; while (pre.next != null) { if (pre.next.key.equals(k)) { return pre.next; } pre = pre.next; } return null; } @Override public void add(K key, V value) { Node node = getNode(key); if (node == null) { dummyHead.next = new Node(key, value, dummyHead.next); size ++; } else { node.value = value; } } @Override public V remove(K key) { Node pre = dummyHead; while (pre.next != null) { if (pre.next.key.equals(key)) { break; } pre = pre.next; } if (pre.next != null) { Node delNode = pre.next; pre.next = delNode.next; delNode.next = null; size --; return delNode.value; } return null; } @Override public boolean contains(K key) { return getNode(key) != null; } @Override public V get(K key) { Node node = getNode(key); return node == null ? null:node.value; } @Override public void set(K key, V newValue) { Node node = getNode(key); if (node == null) { throw new IllegalArgumentException(key + " no this node~"); } node.value = newValue; } @Override public int getSize() { return size; } @Override public boolean isEmpty() { return size == 0; } public static void main(String[] args){ System.out.println("Pride and Prejudice"); ArrayList<String> words = new ArrayList<>(); if(FileOperation.readFile("pride-and-prejudice.txt", words)) { System.out.println("Total words: " + words.size()); LinkedListMap<String, Integer> map = new LinkedListMap<>(); for (String word : words) { if (map.contains(word)) map.set(word, map.get(word) + 1); else map.add(word, 1); } System.out.println("Total different words: " + map.getSize()); System.out.println("Frequency of PRIDE: " + map.get("pride")); System.out.println("Frequency of PREJUDICE: " + map.get("prejudice")); } System.out.println(); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/map/BSTMap.java
data_struct_study/src/map/BSTMap.java
package map; import set.FileOperation; import java.util.ArrayList; /** * 映射 Map * 1)、存储 Key:value 数据对的数据结构。 * 2)、根据 Key,寻找 Value。 * * 非常容易使用链表或者二分搜索树来实现。 * LinkedListMap BSTMap 平均 最差 * add、remove、set、get、contains O(n) O(h) O(logn) O(n) */ public class BSTMap<K extends Comparable<K>, V> implements Map<K, V> { private class Node { private K key; private V value; private Node left; private Node right; public Node(K key, V value) { this.key = key; this.value = value; this.left = null; this.right = null; } } private Node root; private int size; public BSTMap() { this.root = null; this.size = 0; } // 向二分搜索树中添加新的元素(key,value) @Override public void add(K key, V value) { root = add(root, key, value); } // 向以 Node 为根节点的二分搜索树中插入元素(key,value),递归算法 // 返回插入新节点后二分搜索树的根 private Node add(Node node, K key, V value) { if (node == null) { size ++; return new Node(key, value); } if (key.compareTo(node.key) < 0) { node.left = add(node.left, key, value); } else if (key.compareTo(node.key) > 0) { node.right = add(node.right, key, value); } else { node.value = value; } return node; } // 返回以 Node 为根节点的二分搜索树中,key 所在的节点 private Node getNode(Node node, K key) { if (node == null) { return null; } if (node.key.equals(key)) { return node; } else if (key.compareTo(node.key) < 0) { return getNode(node.left, key); } else { return getNode(node.right, key); } } // 从二分搜索树中删除键为 key 的节点 @Override public V remove(K key) { Node node = getNode(root, key); if (node != null) { root = remove(root, key); return node.value; } return null; } private Node remove(Node node, K key) { if (node == null) { return null; } if (key.compareTo(node.key) < 0) { node.left = remove(node.left, key); return node; } else if (key.compareTo(node.key) > 0) { node.right = remove(node.right, key); return node; } else { // 待删除节点左子树为空的情况 if (node.left == null) { Node rightNode = node.right; node.right = null; size --; return rightNode; } // 待删除节点右子树为空的情况 if (node.right == null) { Node leftNode = node.left; node.left = null; size --; return leftNode; } // 待删除节点左右子树都为空的情况 // 找到比待删除节点大的最小节点,即待删除节点右子树的最小节点 // 用这个节点顶替待删除节点的位置 Node successor = minimum(node.right); successor.right = removeMin(node.right); successor.left = node.left; node.left = node.right = null; return successor; } } // 返回以 Node 为根的二分搜索树的最小值所在的节点 private Node minimum(Node node) { if (node.left == null) { return node; } return minimum(node.left); } // 删除掉以 Node 为根的二分搜索树中的最小节点 // 返回删除节点后新的二分搜索树的根 private Node removeMin(Node node) { if (node.left == null) { Node rightNode = node.right; node.right = null; size --; return rightNode; } node.left = removeMin(node.left); return node; } @Override public boolean contains(K key) { return getNode(root, key) != null; } @Override public V get(K key) { Node node = getNode(root, key); return node == null ? null:node.value; } @Override public void set(K key, V newValue) { Node node = getNode(root, key); if (node == null) { throw new IllegalArgumentException(key + " on exist~"); } node.value = newValue; } @Override public int getSize() { return size; } @Override public boolean isEmpty() { return size == 0; } public static void main(String[] args){ System.out.println("Pride and Prejudice"); ArrayList<String> words = new ArrayList<>(); if(FileOperation.readFile("pride-and-prejudice.txt", words)) { System.out.println("Total words: " + words.size()); BSTMap<String, Integer> map = new BSTMap<>(); for (String word : words) { if (map.contains(word)) map.set(word, map.get(word) + 1); else map.add(word, 1); } System.out.println("Total different words: " + map.getSize()); System.out.println("Frequency of PRIDE: " + map.get("pride")); System.out.println("Frequency of PREJUDICE: " + map.get("prejudice")); } System.out.println(); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/recursion/Solution2.java
data_struct_study/src/recursion/Solution2.java
package recursion; /** * 不使用虚拟头结点 */ public class Solution2 { public ListNode removeElements(ListNode head, int val) { while (head != null && head.val == val) { // ListNode delNode = head; // head = head.next; // delNode = null; // 在 LeetCode不需要考虑用于内存释放的第一、三步 head = head.next; } if (head == null) { return null; } ListNode pre = head; while (pre.next != null) { if (pre.next.val == val) { // ListNode delNode = pre.next; // pre.next = delNode.next; // delNode = null; // 在 LeetCode不需要考虑用于内存释放的第一、三步 pre.next = pre.next.next; } else { pre = pre.next; } } return head; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/recursion/Sum.java
data_struct_study/src/recursion/Sum.java
package recursion; public class Sum { public static int sum(int[] arr){ return sum(arr, 0); } // 计算arr[l...n)这个区间内所有数字的和 private static int sum(int[] arr, int l){ if(l == arr.length) return 0; return arr[l] + sum(arr, l + 1); } public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5, 6, 7, 8}; System.out.println(sum(nums)); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/recursion/Solution5.java
data_struct_study/src/recursion/Solution5.java
package recursion; /** * 理解递归调用过程: * * 1 -> 2 -> 6 -> 3 -> 4 -> 5 -> 6 -> NULL * Call: remove 6 in 1 -> 2 -> 6 -> 3 -> 4 -> 5 -> 6 -> NULL * --Call: remove 6 in 2 -> 6 -> 3 -> 4 -> 5 -> 6 -> NULL * ----Call: remove 6 in 6 -> 3 -> 4 -> 5 -> 6 -> NULL * ------Call: remove 6 in 3 -> 4 -> 5 -> 6 -> NULL * --------Call: remove 6 in 4 -> 5 -> 6 -> NULL * ----------Call: remove 6 in 5 -> 6 -> NULL * ------------Call: remove 6 in 6 -> NULL * --------------Call: remove 6 in null * --------------Return: null * ------------After remove 6: null * ------------Return: null * ----------After remove 6: null * ----------Return: 5 -> NULL * --------After remove 6: 5 -> NULL * --------Return: 4 -> 5 -> NULL * ------After remove 6: 4 -> 5 -> NULL * ------Return: 3 -> 4 -> 5 -> NULL * ----After remove 6: 3 -> 4 -> 5 -> NULL * ----Return: 3 -> 4 -> 5 -> NULL * --After remove 6: 3 -> 4 -> 5 -> NULL * --Return: 2 -> 3 -> 4 -> 5 -> NULL * After remove 6: 2 -> 3 -> 4 -> 5 -> NULL * Return: 1 -> 2 -> 3 -> 4 -> 5 -> NULL * 1 -> 2 -> 3 -> 4 -> 5 -> NULL */ public class Solution5 { public ListNode removeElements(ListNode head, int val, int depth) { String depthString = generateDepthString(depth); System.out.print(depthString); System.out.println("Call: remove " + val + " in " + head); if(head == null){ System.out.print(depthString); System.out.println("Return: " + head); return head; } ListNode res = removeElements(head.next, val, depth + 1); System.out.print(depthString); System.out.println("After remove " + val + ": " + res); ListNode ret; if(head.val == val) ret = res; else{ head.next = res; ret = head; } System.out.print(depthString); System.out.println("Return: " + ret); return ret; } private String generateDepthString(int depth){ StringBuilder res = new StringBuilder(); for(int i = 0 ; i < depth ; i ++) res.append("--"); return res.toString(); } public static void main(String[] args) { int[] nums = {1, 2, 6, 3, 4, 5, 6}; ListNode head = new ListNode(nums); System.out.println(head); ListNode res = (new Solution5()).removeElements(head, 6, 0); System.out.println(res); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/recursion/Solution4.java
data_struct_study/src/recursion/Solution4.java
package recursion; public class Solution4 { public ListNode removeElements(ListNode head, int val) { if (head == null) { return null; } head.next = removeElements(head.next, val); return head.val == val ? head.next : head; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/recursion/Solution.java
data_struct_study/src/recursion/Solution.java
package recursion; /** * Leetcode 203: Remove Linked List Elements * 删除多个节点时使用 while 循环。 * 1、Solution:注意内存释放 + 不使用虚拟头结点。 * 2、Solution2:不使用虚拟头结点。 * 3、Solution3:使用虚拟头结点。 */ class Solution { public ListNode removeElements(ListNode head, int val) { // 1、由于没有使用虚拟头结点,要删除的节点时头结点时需要特殊处理。 while (head != null && head.val == val) { ListNode delNode = head; head = head.next; delNode = null; } // 2、如果 head 为 null,说明已经删除完毕,直接返回 null 即可。 if (head == null) { return null; } // 3、从中间开始才有需要删除的节点。 // 重要细节:需要使用一个新变量记录待删除节点的前一个节点 ListNode pre = head; while (pre.next != null) { if (pre.next.val == val) { ListNode delNode = pre.next; pre.next = delNode.next; delNode = null; } else { pre = pre.next; } } return head; } public static void main(String[] args) { int[] nums = {1, 2, 6, 3, 4, 5, 6}; ListNode head = new ListNode(nums); System.out.println(head); ListNode res = (new Solution()).removeElements(head, 6); System.out.println(res); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/recursion/Solution3.java
data_struct_study/src/recursion/Solution3.java
package recursion; /** * 使用虚拟头结点 */ public class Solution3 { public ListNode removeElements(ListNode head, int val) { // 给真实的头结点前面添加一个虚拟的头结点 ListNode dummyHead = new ListNode(-1); dummyHead.next = head; ListNode pre = dummyHead; while (pre.next != null) { if (pre.next.val == val) { pre.next = pre.next.next; } else { pre = pre.next; } } return dummyHead.next; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/recursion/Main.java
data_struct_study/src/recursion/Main.java
package recursion; /** * 递归:本质就是将原来的问题转换为更小的问题。 * * 1)、注意递归函数的宏观语义。 * 2)、递归函数就是一个普通的函数,仅完成一个功能而已。 * * 递归算法通常分为两步: * * 1)、求解基本问题。 * 2)、把原问题转化为更小的问题。 * * 递归调用是有代价的:函数调用 + 系统栈空间 * * 其它常见的链表类型: * 1)、双向链表,每一个 ListNode 同时具有 pre、next 指针 * 2)、双向循环链表:能够更进一步地封装很多便利的操作,Java 中的 LinkedList 的本质就是双向循环链表。 * 3)、数组链表:如果明确知道要存储元素的总数,使用数组链表会更加方便, * 数组中存储一个个的 Node,Node 包含元素 e 与 int 型的 next 指针。 * */ public class Main { /** * 给用户调用的函数 * * @param arr 数组 * @return 数组的和 */ public static int sum(int[] arr) { return sum(arr, 0); } /** * 递归函数,求解 [l~n] 的和 * * @param arr 数组 * @param l 起始值 * @return 数组的和 */ private static int sum(int[] arr, int l) { // 1、求解基本问题 if (l == arr.length) { return 0; } // 2、把原问题转化为更小的问题 return arr[l] + sum(arr, l + 1); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/recursion/ListNode.java
data_struct_study/src/recursion/ListNode.java
package recursion; /** * Definition for singly-linked list. * */ public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } ListNode(int[] arr) { if (arr == null || arr.length == 0) { throw new IllegalArgumentException("ListNode is empty!"); } this.val = arr[0]; ListNode cur = this; for (int i = 1; i < arr.length; i++) { cur.next = new ListNode(arr[i]); cur = cur.next; } } @Override public String toString() { StringBuilder sb = new StringBuilder(); ListNode cur = this; while (cur != null) { sb.append(cur.val).append(" -> "); cur = cur.next; } sb.append("NULL"); return sb.toString(); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/queue_problem/Solution127.java
data_struct_study/src/queue_problem/Solution127.java
package queue_problem; public class Solution127 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/queue_problem/Solution23.java
data_struct_study/src/queue_problem/Solution23.java
package queue_problem; public class Solution23 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/queue_problem/Solution102.java
data_struct_study/src/queue_problem/Solution102.java
package queue_problem; import com.sun.tools.javac.util.Pair; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; /** * 树:层序遍历。102、107、103、199 * O(n) * O(n) * 二叉树的层次遍历:层次遍历和广度优先遍历其实是很像的, * 在循环中使用队列就好。唯一的不同是每一层单独一个list, * 因此我们就需要想想办法让每层分隔开,我使用的办法就是 * 每次塞进去一个root作为分隔。 */ public class Solution102 { // Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } // 层次遍历类似于BFS,在while循环中使用队列 + 每层单独一个list public List<List<Integer>> levelOrder(TreeNode root) { // 1、创建一个双层列表用于保存结果, 如果root为null,则直接返回 List<List<Integer>> res = new ArrayList<>(); if (root == null) { return res; } // 2、使用队列先进先出的特性:创建一个队列, 并首先将根节点入队 LinkedList<Pair<TreeNode, Integer>> queue = new LinkedList<>(); queue.addLast(new Pair<>(root, 0)); // 3、当队列不为空时 while (!queue.isEmpty()) { // 1)、取出队首元素,如果当前元素层次等于res的层次,则说明还有新的子节点要添加, // 此时需要添加新的列表到res中 Pair<TreeNode, Integer> pair = queue.removeFirst(); TreeNode node = pair.fst; int level = pair.snd; if (level == res.size()) { res.add(new ArrayList<>()); } // 2)、异常边界处理:确保res的大小大于当前元素的层次 assert res.size() > level; // 3)、添加当前元素节点值到res对应层次中 res.get(level).add(node.val); // 4)、如果当前节点还有左右子节点,则添加到队列尾部 if (node.left != null) { queue.addLast(new Pair<>(node.left, level + 1)); } if (node.right != null) { queue.addLast(new Pair<>(node.right, level + 1)); } } return res; } public static void main(String[] args) { } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/queue_problem/Solution279.java
data_struct_study/src/queue_problem/Solution279.java
package queue_problem; import LinkedList.LinkedList; import com.sun.tools.javac.util.Pair; /** * 图:无权图的最短路径。279 * 1、建模成图论问题:从n到0,每个数字表示一个节点, * 如果两个数字x到y相差一个完全平方数,则连接一条边。 * 我们得到了一个无权图。原问题转换为,求这个无权图中 * 从n到0的最短路径。 * * 暴力求解 * O(2^n) * O(2^n) */ public class Solution279 { public int numSquares(int n) { LinkedList<Pair<Integer, Integer>> queue = new LinkedList<Pair<Integer, Integer>>(); queue.addLast(new Pair<>(n, 0)); while (!queue.isEmpty()) { Pair<Integer, Integer> pair = queue.removeFirst(); int sum = pair.fst; int step = pair.snd; if (sum == 0) { return step; } for (int i = 1; sum - i * i >= 0; i++) { queue.addLast(new Pair<>(sum - i * i, step + 1)); } } throw new IllegalArgumentException("no this num squares!"); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/queue_problem/Solution199.java
data_struct_study/src/queue_problem/Solution199.java
package queue_problem; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; /** * 题目描述:给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。 * * 一、BFS * 思路: 利用 BFS 进行层次遍历,记录下每层的最后一个元素。 * 时间复杂度: O(N),每个节点都入队出队了 1 次。 * 空间复杂度: O(N),使用了额外的队列空间。 * * 二、DFS (时间100%) * 思路: 我们按照 「根结点 -> 右子树 -> 左子树」 的顺序访问, * 就可以保证每层都是最先访问最右边的节点的。 * * (与先序遍历 「根结点 -> 左子树 -> 右子树」 正好相反,先序 * 遍历每层最先访问的是最左边的节点) * * 时间复杂度: O(N),每个节点都访问了 1 次。 * 空间复杂度: O(N),因为这不是一棵平衡二叉树,二叉树的深度 * 最少是logN, 最坏的情况下会退化成一条链表,深度就是 N, * 因此递归时使用的栈空间是 O(N) 的。 */ public class Solution199 { // Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } // 1、BFS: 利用 BFS 进行层次遍历,记录下每层的最后一个元素。 // 时间复杂度: O(N),每个节点都入队出队了 1 次。 // 空间复杂度: O(N),使用了额外的队列空间。 public List<Integer> rightSideView(TreeNode root) { // 1、创建一个列表用于保存返回值,如果root等于null,则返回 List<Integer> res = new ArrayList<>(); if (root == null) { return res; } // 2、使用队列先进先出的特性:创建一个队列(入队出队每一层的所有节点),并将根节点入队 Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); // 3、当队列不为空时 while (!queue.isEmpty()) { // 1)、遍历当前层次对应的队列,如果当前节点有左右节点则入队,如果遍历的当前节点 // 是当前层的最后一个节点则放入结果列表 int size = queue.size(); for (int i = 0; i < size; i++) { TreeNode node = queue.poll(); if (node.left != null) { queue.offer(node.left); } if (node.right != null) { queue.offer(node.right); } if (i == size - 1) { // 只将当前层的最后一个节点放入结果列表 res.add(node.val); } } } return res; } // 1、创建一个全局返回列表,便于操作 List<Integer> res = new ArrayList<>(); // 2、DFS:我们按照 「根结点 -> 右子树 -> 左子树」 的顺序访问, // 就可以保证每层都是最先访问最右边的节点的。 // 时间复杂度: O(N),每个节点都访问了 1 次。 // 空间复杂度: O(N),因为这不是一棵平衡二叉树,二叉树的深度 // 最少是logN, 最坏的情况下会退化成一条链表,深度就是 N, // 因此递归时使用的栈空间是 O(N) 的。 public List<Integer> rightSideView2(TreeNode root) { dfs(root, 0); // 从根节点开始访问,根节点深度是0 return res; } private void dfs(TreeNode root, int depth) { // 1)、异常处理,如果根节点为空,直接返回 if (root == null) { return; } // 2)、记录下当前层的访问节点 if (depth == res.size()) { res.add(root.val); } // 3)、深度+1,保证每一层只记录一个节点 depth++; // 4)、保证最先访问右子节点 dfs(root.right, depth); dfs(root.left, depth); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/queue_problem/Solution107.java
data_struct_study/src/queue_problem/Solution107.java
package queue_problem; public class Solution107 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/queue_problem/Solution126.java
data_struct_study/src/queue_problem/Solution126.java
package queue_problem; /** * 优先队列(底层实现:堆) * 1、C++:priority_queue 底层默认实现是最大堆。 * 2、C++:priority_queue<int, vector<int>, greater<int>> 底层是最小堆。 */ public class Solution126 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/queue_problem/Solution103.java
data_struct_study/src/queue_problem/Solution103.java
package queue_problem; /** * 二叉树的之字形层序遍历 */ public class Solution103 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/queue_problem/Main.java
data_struct_study/src/queue_problem/Main.java
package queue_problem; /** * 队列的基本应用 - 广度优先遍历 * * JsonChao的队列核心题库:9题 */ public class Main { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/queue_problem/Solution347.java
data_struct_study/src/queue_problem/Solution347.java
package queue_problem; import com.sun.tools.javac.util.Pair; import java.util.*; /** * 347: * 1、最简单的思路:扫描一遍统计概率,排序找到前k个出现频率最高的元素。时间复杂度:O(nlogn) * 2、使用优先队列不停地维护我们能找到的前k个出现频率最高的元素。时间复杂度:O(nlogk) */ public class Solution347 { private class MinComparator implements Comparator<Pair<Integer, Integer>> { @Override public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) { if (!o1.fst.equals(o2.fst)) { return o1.fst - o2.fst; } return o1.snd - o2.snd; } } // public List<Integer> topKFrequent(int[] nums, int k) { // // if (k <= 0) { // throw new IllegalArgumentException("illegal argument!"); // } // // // 1、统计每一个元素出现的频率 // HashMap<Integer, Integer> freq = new HashMap<>(); // for (int i = 0; i < nums.length; i++) { // if (freq.containsKey(nums[i])) { // freq.put(nums[i], freq.get(nums[i] + 1)); // } else { // freq.put(nums[i], 1); // } // } // // if (k > freq.size()) { // throw new IllegalArgumentException("illegal argument!"); // } // // // 2、使用底层为最小堆的优先队列维护前 k 个频率最大的元素 // PriorityQueue<Pair<Integer, Integer>> heap = new PriorityQueue<>(new MinComparator()); // for (Integer value:freq.keySet()) { // int valueFreq = freq.get(value); // if (heap.size() == k) { // if (heap.peek().fst < valueFreq) { // heap.poll(); // heap.add(new Pair<>(valueFreq, value)); // } // } else { // heap.add(new Pair<>(valueFreq, value)); // } // } // // ArrayList<Integer> res = new ArrayList<>(); // while (!heap.isEmpty()) { // res.add(heap.poll().snd); // } // // return res; // } // 时间复杂度:O(nlogk), 空间复杂度:O(n) public int[] topKFrequent(int[] nums, int k) { // 1、将所有 元素:出现频次 添加到 TreeMap 中 TreeMap<Integer, Integer> map = new TreeMap<>(); for (int num:nums) { if (map.containsKey(num)) { map.put(num, map.get(num) + 1); } else { map.put(num, 1); } } // 2、使用最小堆实现的优先队列维护前 Top k 个元素 PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> map.get(o1) - map.get(o2)); for (int key:map.keySet()) { if (pq.size() < k) { pq.add(key); } else if (map.get(key) > map.get(pq.peek())) { pq.remove(); pq.add(key); } } // 3、返回保存 Top k 个元素的数组 int[] values = new int[k]; for (int i = values.length - 1; i >= 0; i--) { values[i] = pq.remove(); } return values; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/queue_problem/Solution279_2.java
data_struct_study/src/queue_problem/Solution279_2.java
package queue_problem; import LinkedList.LinkedList; import com.sun.tools.javac.util.Pair; /** * 使用 visited 数组,记录每一个入队元素 * O(n) * O(n) */ public class Solution279_2 { public int numSquares(int n) { if (n == 0) { return 0; } LinkedList<Pair<Integer, Integer>> queue = new LinkedList<Pair<Integer, Integer>>(); queue.addLast(new Pair<>(n, 0)); boolean[] visited = new boolean[n + 1]; visited[n] = true; while (!queue.isEmpty()) { Pair<Integer, Integer> pair = queue.removeFirst(); int sum = pair.fst; int step = pair.snd; if (sum == 0) { return step; } for (int i = 1; sum - i * i >= 0; i++) { int a = sum - i * i; if (!visited[a]) { if (a == 0) { return step + 1; } queue.addLast(new Pair<>(a, step + 1)); visited[a] = true; } } } throw new IllegalArgumentException("no this num squares!"); } public static void main(String[] args) { System.out.println((new Solution279_2()).numSquares(12)); System.out.println((new Solution279_2()).numSquares(13)); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList/LinkedListStack.java
data_struct_study/src/LinkedList/LinkedListStack.java
package LinkedList; import stack.Stack; public class LinkedListStack<E> implements Stack<E> { private LinkedList<E> list = new LinkedList<E>(); @Override public void push(E e) { list.addFirst(e); } @Override public E pop() { return list.removeFirst(); } @Override public E peek() { return list.getFirst(); } @Override public int getSize() { return list.getSize(); } @Override public boolean isEmpty() { return list.isEmpty(); } @Override public String toString() { return " LinkedListStack: TOP " + list; } public static void main(String[] args) { LinkedListStack<Integer> stack = new LinkedListStack<Integer>(); for (int i = 0; i < 5; i++) { stack.push(i); System.out.println(stack); } stack.pop(); System.out.println(stack); stack.peek(); System.out.println(stack); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList/LinkedListQueue.java
data_struct_study/src/LinkedList/LinkedListQueue.java
package LinkedList; import queue.LoopQueue; import queue.Queue; public class LinkedListQueue<E> implements Queue<E> { /** * Node 应该被设置成私有的,用户对此是无感知的。 */ private class Node { public E e; public Node next; public Node(E e, Node next) { this.e = e; this.next = next; } public Node(E e) { this(e, null); } public Node() { this(null, null); } @Override public String toString() { return e.toString(); } } private Node head; private Node tail; private int size; public LinkedListQueue() { head = null; tail = null; size = 0; } @Override public void enqueue(E e) { // 没有使用 dummyHead 时,入队之前需要处理队列为空的情况 if (head == null) { head = new Node(e); tail = head; } else { tail.next = new Node(e); tail = tail.next; } size++; } @Override public E dequeue() { if (size == 0) { throw new IllegalArgumentException("queue is empty!"); } Node netNode = head; head = head.next; netNode.next = null; // 没有使用 dummyHead 时,出队之后需要处理队列为空的情况 if (head == null) { tail = null; } size--; return netNode.e; } @Override public boolean isEmpty() { return size == 0; } @Override public int getSize() { return size; } @Override public E getFront() { if (size == 0) { throw new IllegalArgumentException("queue is empty!"); } return head.e; } @Override public String toString() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(String.format("Queue size is %d\n", size)); stringBuilder.append("Front [ "); Node netNode = head; while (netNode != null) { stringBuilder.append(netNode.e).append(" -> "); netNode = netNode.next; } stringBuilder.append(" NULL ] Tail"); return stringBuilder.toString(); } public static void main(String[] args) { LinkedListQueue<Object> queue = new LinkedListQueue<>(); for (int i = 0; i < 10; i++) { queue.enqueue(i); if (i % 3 == 2) { queue.dequeue(); } System.out.println(queue); } } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList/Main.java
data_struct_study/src/LinkedList/Main.java
package LinkedList; public class Main { public static void main(String[] args) { LinkedList<Integer> list = new LinkedList<Integer>(); for (int i = 0; i < 5; i++) { list.addFirst(i); System.out.println(list); } list.add(3, 99); System.out.println(list); list.remove(3); System.out.println(list); list.removeLast(); System.out.println(list); list.removeFirst(); System.out.println(list); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/LinkedList/LinkedList.java
data_struct_study/src/LinkedList/LinkedList.java
package LinkedList; /** * 为什么链表很重要? * 不同于 动态数组、栈、队列的实现:其底层是依托静态数组,靠 resize 解决固定容量问题, * 链表是真正的动态数据结构,也是最简单的动态数据结构。 * 能够帮助我们更深入地理解引用(指针)与递归。 * 优势:真正的动态,不需要处理固定容量的问题。 * 逆势:不同于数组其底层的数据是连续分布的,链表底层的数据分布是随机的, * 紧靠next(pre)指针连接,因此链表相对于数组丧失了随机访问的能力。 * * 数组和链表的区别? * 数组最好被应用于索引有语义的情况,例如 Students[1] * 最大的优势:支持动态查询。 * * 链表不能被应用于索引有语义的情况。 * 最大的优势:动态。 * * 链表的时间复杂度: * 增: O(n) * 删: O(n) * 改: O(n) * 查: O(n) * * 总结:链表不适合去修改,且只适合 增、删、查 链表头的元素,此时时间复杂度为 O(1)。 */ public class LinkedList<E> { /** * Node 应该被设置成私有的,用户对此是无感知的。 */ private class Node { public E e; public Node next; public Node(E e, Node next) { this.e = e; this.next = next; } public Node(E e) { this(e, null); } public Node() { this(null, null); } @Override public String toString() { return e.toString(); } } // 设立一个 dummyHead(虚拟头节点,即头结点的前一个节点),为了后续编写统一逻辑的方便 private Node dummyHead; private int size; public LinkedList() { this.dummyHead = new Node(); this.size = 0; } /** * 获取链表的大小 * * @return 链表的大小 */ public int getSize() { return size; } /** * 判断链表是否为空 * * @return 链表是否为空 */ public boolean isEmpty() { return size == 0; } /** * 在链表指定位置添加元素 * * @param index 指定位置 * @param e E */ public void add(int index, E e) { if ((index < 0 || index > size)) { throw new IllegalArgumentException("add failed!"); } // 设立一个 dummyHead(虚拟头节点,即头结点的前一个节),为了后续编写统一逻辑的方便 Node pre = dummyHead; for (int i = 0; i < index; i++) { pre = pre.next; } // Node node = new Node(e); // node.next = pre.next; // pre.next = node; pre.next = new Node(e, pre.next); size++; } /** * 在链表头结点添加元素 * * @param e E * @return Node */ public void addFirst(E e) { add(0, e); } /** * 在链表最后一个位置添加元素 * * @param e E */ public void addLast(E e) { add(size, e); } /** * 查询链表中指定节点的元素 * * @param index 指定节点的下标 * @return 指定节点的元素 */ public E get(int index) { if (index < 0 || index >= size) { throw new IllegalArgumentException("no node!"); } Node cur = dummyHead.next; for (int i = 0; i < index; i++) { cur = cur.next; } return cur.e; } /** * 获取链表头部的元素 * * @return 链表头部的元素 */ public E getFirst() { return get(0); } /** * 获取链表尾部的元素 * * @return 链表尾部的元素 */ public E getLast() { return get(size - 1); } /** * 修改链表指定位置的元素 * * @param index 链表指定位置 * @param e 链表指定位置的元素 */ public void set(int index, E e) { if (index < 0 || index >= size) { throw new IllegalArgumentException("no index!"); } Node cur = dummyHead.next; for (int i = 0; i < index; i++) { cur = cur.next; } cur.e = e; } /** * 删除 {@index} 处的元素 * * @param index 指定位置 * @return E */ public E remove(int index) { if ((index < 0 || index >= size)) { throw new IllegalArgumentException("index is illegal!"); } Node pre = dummyHead; for (int i = 0; i < index; i++) { pre = pre.next; } Node cur = pre.next; pre.next = cur.next; cur.next = null; size--; return cur.e; } /** * 删除链表头部的元素 * * @return 链表头部的元素 */ public E removeFirst() { return remove(0); } /** * 删除链表尾部的元素 * * @return 链表尾部的元素 */ public E removeLast() { return remove(size - 1); } /** * 从链表中删除元素e * * @param e E */ public void removeElement(E e){ Node prev = dummyHead; while(prev.next != null){ if(prev.next.e.equals(e)) break; prev = prev.next; } if(prev.next != null){ Node delNode = prev.next; prev.next = delNode.next; delNode.next = null; size --; } } /** * 判断链表中是否包含元素 E * * @param e E * @return 链表中是否包含元素 E */ public boolean isContains(E e) { Node cur = dummyHead.next; while (cur != null) { if (cur.e.equals(e)) { return true; } cur = cur.next; } return false; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("LinkedList: "); // Node cur = dummyHead.next; // while (cur != null) { // sb.append(cur).append(" -> "); // cur = cur.next; // } for (Node cur = dummyHead.next; cur != null; cur = cur.next) { sb.append(cur).append(" -> "); } sb.append("NULL"); return sb.toString(); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution100.java
data_struct_study/src/binary_search_tree_problem/Solution100.java
package binary_search_tree_problem; public class Solution100 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution124.java
data_struct_study/src/binary_search_tree_problem/Solution124.java
package binary_search_tree_problem; public class Solution124 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution110.java
data_struct_study/src/binary_search_tree_problem/Solution110.java
package binary_search_tree_problem; public class Solution110 { // Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } private boolean result = true; public boolean isBalanced(TreeNode root) { maxDepth(root); return result; } private int maxDepth(TreeNode root) { if (root == null) { return 0; } int l = maxDepth(root.left); int r = maxDepth(root.right); if (Math.abs(l - r) > 1) { result = false; } return 1 + Math.max(l , r); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution104.java
data_struct_study/src/binary_search_tree_problem/Solution104.java
package binary_search_tree_problem; /** * 二叉树的最大深度 * 时间复杂度:O(n) * 空间复杂度:O(h) */ public class Solution104 { // Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public int maxDepth(TreeNode root) { // 1、如果根节点为null,则最大深度为0 if (root == null) { return 0; } // 2、1 + 递归取左右子树深度的最大值 return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution113.java
data_struct_study/src/binary_search_tree_problem/Solution113.java
package binary_search_tree_problem; /** * 定义递归问题 */ public class Solution113 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution198_8.java
data_struct_study/src/binary_search_tree_problem/Solution198_8.java
package binary_search_tree_problem; /** * DP:改变状态定义,优化转移方程 * O(n) * O(n) */ public class Solution198_8 { public int rob(int[] nums) { int n = nums.length; if (n == 0) { return 0; } // 考虑抢劫 [0..n) 房子得到的最大价值 int[] memo = new int[n]; memo[0] = nums[0]; for (int i = 1; i < n; i++) { memo[i] = Math.max(memo[i - 1], nums[i] + (i - 2 >= 0 ? memo[i - 2] : 0)); } return memo[n - 1]; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution111.java
data_struct_study/src/binary_search_tree_problem/Solution111.java
package binary_search_tree_problem; public class Solution111 { public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public int minDepth(TreeNode root) { if (root == null) { return 0; } int left = minDepth(root.left); int right = minDepth(root.right); if (left == 0 || right == 0) { return left + right + 1; } return Math.min(left, right) + 1; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution437.java
data_struct_study/src/binary_search_tree_problem/Solution437.java
package binary_search_tree_problem; /** * 更复杂的递归逻辑 * 1、node 在路径的情况 && 在 node 的左右子树中去查找它们的和是否为 sum。 * 2、node 在路径的情况需要处理为负数的情况,并且不一定需要计算到叶子节点。 * O(n) * O(h) */ public class Solution437 { public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } // 在以root为根节点的二叉树中,寻找和为sum的路径,返回这样的路径个数 public int pathSum(TreeNode root, int sum) { if (root == null) { return 0; } return findPath(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum); } // 在以 root 为根节点的二叉树中,统计和为 sum 的节点的个数 private int findPath(TreeNode root, int sum) { if (root == null) { return 0; } int res = 0; if (root.val == sum) { res += 1; } res += findPath(root.left, sum - root.val); res += findPath(root.right, sum - root.val); return res; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution_1.java
data_struct_study/src/binary_search_tree_problem/Solution_1.java
package binary_search_tree_problem; /** * 二叉树根节点到所有叶子节点的路径之和: * 先序遍历的思想(根左右)+数字求和(每一层都比上层和*10+当前根节点的值) * 时间复杂度:O(n) * 空间复杂度:O(n) */ public class Solution_1 { // Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public int sumNumbers(TreeNode root) { // 1、创建一个路径和变量sum,如果root为null,则返回0,最后按照前序遍历的思想计算路径和 int sum = 0; if (root == null) { return sum; } return preOrderSumNumbers(root, sum); } public int preOrderSumNumbers(TreeNode root, int sum) { // 1)、如果root节点为null则返回路径0 if (root == null) { return 0; } // 2)、计算路径和:每一层都是上一层 * 10 + 当前节点值 sum = sum * 10 + root.val; // 3)、如果遍历到左右子节点都为null时,则说明已经是叶子节点,返回sum即可 if (root.left == null && root.right == null) { return sum; } // 4、使用先序遍历的思想去递归计算总的路径和 return preOrderSumNumbers(root.left, sum) + preOrderSumNumbers(root.right, sum); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution230.java
data_struct_study/src/binary_search_tree_problem/Solution230.java
package binary_search_tree_problem; public class Solution230 { public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } private int cnt = 0; private int val; public int kthSmallest(TreeNode root, int k) { inOrder(root, k); return val; } private void inOrder(TreeNode root, int k) { if (root == null) { return; } // 1、遍历到左边,由最小的值开始 inOrder(root.left, k); cnt++; if (cnt == k) { val = root.val; return; } // 2、然后遍历右边,得到次小的值 inOrder(root.right, k); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution222.java
data_struct_study/src/binary_search_tree_problem/Solution222.java
package binary_search_tree_problem; public class Solution222 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution101.java
data_struct_study/src/binary_search_tree_problem/Solution101.java
package binary_search_tree_problem; public class Solution101 { public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public boolean isSymmetric(TreeNode root) { if (root == null) { return true; } return isSymmetric(root.left, root.right); } private boolean isSymmetric(TreeNode l1, TreeNode l2) { if (l1 == null && l2 == null) { return true; } if (l1 == null || l2 == null) { return false; } if (l1.val != l2.val) { return false; } return isSymmetric(l1.left, l2.right) && isSymmetric(l1.right, l2.left); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution450.java
data_struct_study/src/binary_search_tree_problem/Solution450.java
package binary_search_tree_problem; /** * 1、若删除的节点不存在? * 2、是否可能有多个需要删除的节点。 * 3、删除的节点是否需要返回? */ public class Solution450 { }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution235.java
data_struct_study/src/binary_search_tree_problem/Solution235.java
package binary_search_tree_problem; /** * 题目描述:给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。 * 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q, * 最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x * 的深度尽可能大(一个节点也可以是它自己的祖先)。” * * 二叉搜索树的最近公共祖先 * 时间复杂度:O(lgn) * 空间复杂度:O(n) */ public class Solution235 { public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { // 1、异常处理:如果p或q为null,则抛出异常 if (p == null || q == null) { throw new IllegalArgumentException("q or p is not null!"); } // 2、异常处理:如果根节点为null,则直接返回 if (root == null) { return null; } // 3、如果p和q都在左边,则需要继续递归左子树查找最近公共祖先 // 如果p和q都在右边,则需要继续递归右子树查找最近公共祖先 if (p.val < root.val && q.val < root.val) { return lowestCommonAncestor(root.left, p, q); } if (p.val > root.val && q.val > root.val) { return lowestCommonAncestor(root.right, p, q); } // 4、异常处理:当p或q为当前根节点或p和q不在同一个子树中才返回当前的最近公共祖先 assert p.val == root.val || q.val == root.val || (root.val - p.val) * (root.val - q.val) < 0; return root; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution98.java
data_struct_study/src/binary_search_tree_problem/Solution98.java
package binary_search_tree_problem; /** * 验证二叉搜索树:【递归】,又用到了树的经典划分:树=根+左子树+右子树。注意 * 这里左子树和右子树是包含于原树的范围的; 避免 Integer. MIN_VALUE * 和 Integer.MAX_VALUE,否则一些特殊的测试用例就会挂掉--这也是我们 * 使用Integer 而不是int作为上下限变量的原因。 */ public class Solution98 { // Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public boolean isValidBST(TreeNode root){ return helper(root,null,null); } private boolean helper(TreeNode node, Integer a, Integer b){ if (node == null) return true; if(a!=null&&node.val<=a) return false; if(b!=null&&node.val>=b) return false; if (!helper(node.left,a,node.val)) return false; if (!helper(node.right,node.val,b)) return false; return true; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution226.java
data_struct_study/src/binary_search_tree_problem/Solution226.java
package binary_search_tree_problem; /** * O(n) * O(h) */ public class Solution226 { public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public TreeNode invertTree(TreeNode root) { if (root == null) { return null; } TreeNode left = invertTree(root.left); TreeNode right = invertTree(root.right); root.left = right; root.right = left; return root; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution257.java
data_struct_study/src/binary_search_tree_problem/Solution257.java
package binary_search_tree_problem; import java.util.ArrayList; import java.util.List; /** * 定义递归问题 * O(n) * O(h) */ public class Solution257 { public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public List<String> binaryTreePaths(TreeNode root) { ArrayList<String> res = new ArrayList<>(); if (root == null) { return res; } if (root.left == null && root.right == null) { res.add(Integer.toString(root.val)); return res; } List<String> leftPaths = binaryTreePaths(root.left); for (String s:leftPaths) { StringBuilder sb = new StringBuilder(Integer.toString(root.val)); sb.append("->"); sb.append(s); res.add(sb.toString()); } List<String> rightPaths = binaryTreePaths(root.right); for (String s:rightPaths) { StringBuilder sb = new StringBuilder(Integer.toString(root.val)); sb.append("->"); sb.append(s); res.add(sb.toString()); } return res; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution112.java
data_struct_study/src/binary_search_tree_problem/Solution112.java
package binary_search_tree_problem; /** * 注意递归的终止条件 */ public class Solution112 { public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public boolean hasPathSum(TreeNode root, int sum) { if (root == null) { return false; } if (root.left == null && root.right == null) { return root.val == sum; } return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution108.java
data_struct_study/src/binary_search_tree_problem/Solution108.java
package binary_search_tree_problem; public class Solution108 { public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public TreeNode sortedArrayToBST(int[] nums) { return toBST(nums, 0, nums.length - 1); } private TreeNode toBST(int[] nums, int start, int end) { if (start > end) { return null; } // 1、根节点为中间的值 int mid = start + (end - start) / 2; TreeNode root = new TreeNode(nums[mid]); // 2、中序遍历思想构造二叉搜索树 root.left = toBST(nums, start, mid - 1); root.right = toBST(nums, mid + 1, end); return root; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Main.java
data_struct_study/src/binary_search_tree_problem/Main.java
package binary_search_tree_problem; /** * 二叉树天然的递归结构,空也是一颗二叉树。 * * JsonChao的二叉树核心题库:20题 */ public class Main { public static void main(String[] args) { } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution236.java
data_struct_study/src/binary_search_tree_problem/Solution236.java
package binary_search_tree_problem; /** * 二叉树的最近公共祖先 * 时间复杂度:O(n) * 空间复杂度:O(n) */ public class Solution236 { public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { // 1、如果根节点为null或q或p,则根节点就是最近的公共祖先 if (root == null || root == p || root == q) { return root; } // 2、往左右子树递归到底得到左右子树节点, 如果左节点为null,则公共祖先为右节点, // 左节点不为null,并且右节点为null,则左节点为公共祖先,如果两者都不为null,则 // 公共祖先为当前的根节点 TreeNode left = lowestCommonAncestor(root.left, p, q); TreeNode right = lowestCommonAncestor(root.right, p, q); return left == null ? right : right == null ? left : root; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false
JsonChao/Awesome-Algorithm-Study
https://github.com/JsonChao/Awesome-Algorithm-Study/blob/f1c886eabf744b69e72a0b0a64b348032b439037/data_struct_study/src/binary_search_tree_problem/Solution_2.java
data_struct_study/src/binary_search_tree_problem/Solution_2.java
package binary_search_tree_problem; import java.util.*; /** * 题目描述:请实现一个函数按照之字形打印二叉树,即第一行按照 * 从左到右的顺序打印,第二层按照从右至左的顺序打印, * 第三行按照从左到右的顺序打印,其他行以此类推。 */ public class Solution_2 { // Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } // 队列 + 反转变量reverse记录当前行的打印顺序, // 时间复杂度:O(n), 空间复杂度:O(n) public ArrayList<ArrayList<Integer>> print(TreeNode pRoot) { // 1、创建一个返回链表 & 添加根节点的队列 ArrayList<ArrayList<Integer>> ret = new ArrayList<>(); Queue<TreeNode> queue = new LinkedList<>(); queue.add(pRoot); // 2、创建一个反转变量记录当前层是否需要反转,默认第一层不需要反转 // 当队列不为空时 boolean reverse = false; while (!queue.isEmpty()) { // 1)、创建一个列表添加当前层次的值 ArrayList<Integer> list = new ArrayList<>(); // 2)、当队列数量大于0时,取出队头节点,如果节点不为空, // 则添加值到列表中,并且把左右节点添加到队列中 int cnt = queue.size(); while (cnt-- > 0) { TreeNode node = queue.poll(); if (node == null) { continue; } list.add(node.val); queue.add(node.left); queue.add(node.right); } // 3)、偶数层数,反转变量为true,此时将列表反转,并将reverse变量置反 if (reverse) { Collections.reverse(list); } reverse = !reverse; // 4)、如果列表大小不等于0,则添加到返回列表中 if (list.size() != 0) { ret.add(list); } } return ret; } }
java
Apache-2.0
f1c886eabf744b69e72a0b0a64b348032b439037
2026-01-05T02:39:40.141219Z
false