diff --git "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/03-\351\223\276\350\241\250.md" "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/03-\351\223\276\350\241\250.md" index 59eefbb..47455f7 100644 --- "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/03-\351\223\276\350\241\250.md" +++ "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/03-\351\223\276\350\241\250.md" @@ -260,10 +260,30 @@ public boolean hasCycle(ListNode head) { + + ### 约瑟夫问题(Josephus Problem) ![image-20220418015634464](/Users/guo/Notes/学习算法与数据结构笔记/images/约瑟夫问题.png) +通过双向循环链表,来实现更为简单 + +```swift +CircleLinkedList list = new CircleLinkedList<>(); +for (int i = 1; i < 9; i++) { + list.add(i); +} +list.reset(); +while (!list.isEmpty()) { + list.next(); + list.next(); + Integer e = list.remove(); + System.out.println(e); +} +``` + + + #### 静态链表 > 通过数组来模拟链表,称为静态链表 diff --git "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/04-\346\240\210.md" "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/04-\346\240\210.md" index c615a48..e03fbb0 100644 --- "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/04-\346\240\210.md" +++ "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/04-\346\240\210.md" @@ -15,3 +15,80 @@ ![image-20220418235423024](/Users/guo/Notes/学习算法与数据结构笔记/images/栈_03.png) +#### LeetCode 题目 + +1、[有效的括号](https://leetcode.cn/problems/valid-parentheses/) + +给定一个只包括 `'('`,`')'`,`'{'`,`'}'`,`'['`,`']'` 的字符串 `s` ,判断字符串是否有效。 + +有效字符串需满足: + +1. 左括号必须用相同类型的右括号闭合。 +2. 左括号必须以正确的顺序闭合。 +3. 每个右括号都有一个对应的相同类型的左括号。 + +**示例 1:** + +``` +输入:s = "()" +输出:true +``` + +**示例 2:** + +``` +输入:s = "()[]{}" +输出:true +``` + +**示例 3:** + +``` +输入:s = "(]" +输出:false +``` + + + +**提示:** + +- `1 <= s.length <= 104` +- `s` 仅由括号 `'()[]{}'` 组成 + +**解题思路** + +**一、特殊方式** + +因为要判断的括号类型有限,所以可以通过replace 将 {}、[]、() 替换为"",最后来判断字符串是否为空 ,如果为空,则说明括号是有效的,否则说明是无效的(**效率很低**) + +```swift +public boolean isValid(String s) { + while (s.contains("{}") || s.contains("[]") || s.contains("()")) { + s = s.replace("{}", ""); + s = s.replace("()", ""); + s = s.replace("[]", ""); + } + return s.isEmpty(); +} +``` + +**二、利用栈来处理** + +1、遇到左字符,将左字符入栈 + +2、遇到右字符 + +* 如果栈是空的,说明括号无效 +* 如果栈不为空 ,将栈顶字符出栈,与右字符进行匹配 + * 如果左右字符不匹配,说明括号无效 + * 如果左右字符匹配,继续扫描下一个字符 +* 所有字符扫描完毕 + * 栈为空,说明括号有效 + * 栈不为空,说明括号无效 + + + +```swift + +``` + diff --git "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/05-\351\230\237\345\210\227.md" "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/05-\351\230\237\345\210\227.md" index 4282bde..0258e61 100644 --- "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/05-\351\230\237\345\210\227.md" +++ "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/05-\351\230\237\345\210\227.md" @@ -12,13 +12,91 @@ ![image-20220419014401016](/Users/guo/Notes/学习算法与数据结构笔记/images/队列_02.png) +#### LeetCode 题目 + +1、[用栈实现队列](https://leetcode.cn/problems/implement-queue-using-stacks/) + +**解题思路** + +1、准备两个栈:inStack、outStack + +2、在入队时,将元素 push 到 inStack中 + +3、出队时, + +* 如果outStack为空,将inStack所有元素逐一弹出,push 到 outStack,outStack 弹出栈顶元素 +* 如果 outStack 不为空,outStack 弹出栈顶元素 + +![image-20231210162341843](/Users/guo/Notes/学习算法与数据结构笔记/images/链表/队列(用栈实现队列).png) + + + +```swift +class MyQueue { + private Stack inStack; + private Stack outStack; + + public MyQueue() { + inStack = new Stack<>(); + outStack = new Stack<>(); + } + /** + * 出队 + * @return + */ + public void push(int x) { + inStack.push(x); + } + /** + * 入队 + * @return + */ + public int pop() { + checkOutStack(); + return outStack.pop(); + } + + /** + * 返回队列开头的元素 + * @return + */ + public int peek() { + checkOutStack(); + return outStack.peek(); + } + + public boolean empty() { + return inStack.isEmpty() && outStack.isEmpty(); + } + + private void checkOutStack() { + if (outStack.isEmpty()) { + while (!inStack.isEmpty()) { + outStack.push(inStack.pop()); + } + } + } +} + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.peek(); + * boolean param_4 = obj.empty(); + */ +``` + + + ### 双端队列(Deque) -> 是能在队尾端添加、删除的队列 +> 是能在**头尾**两端**添加**、**删除**的队列 > -> 英文 deque 是 double ended queue 的简称 +> 英文 deque 是 **double ended queue** 的简称 ![image-20220419224623298](/Users/guo/Notes/学习算法与数据结构笔记/images/双端队列_01.png) @@ -41,8 +119,13 @@ E front(); E rear(); ``` + + ### 循环队列(Circle Queue) -> 底层使用数组来实现 +> 底层使用**数组**来实现 ![image-20220419230424338](/Users/guo/Notes/学习算法与数据结构笔记/images/循环队列_01.png) + + + diff --git "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/images/\351\223\276\350\241\250/\345\233\236\346\226\207\351\223\276\350\241\250.png" "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/images/\351\223\276\350\241\250/\345\233\236\346\226\207\351\223\276\350\241\250.png" new file mode 100644 index 0000000..85af19f Binary files /dev/null and "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/images/\351\223\276\350\241\250/\345\233\236\346\226\207\351\223\276\350\241\250.png" differ diff --git "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/images/\351\223\276\350\241\250/\346\234\200\345\260\217\346\240\21001.png" "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/images/\351\223\276\350\241\250/\346\234\200\345\260\217\346\240\21001.png" new file mode 100644 index 0000000..bbe3446 Binary files /dev/null and "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/images/\351\223\276\350\241\250/\346\234\200\345\260\217\346\240\21001.png" differ diff --git "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/images/\351\223\276\350\241\250/\351\230\237\345\210\227(\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227).png" "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/images/\351\223\276\350\241\250/\351\230\237\345\210\227(\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227).png" new file mode 100644 index 0000000..6297b61 Binary files /dev/null and "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/images/\351\223\276\350\241\250/\351\230\237\345\210\227(\347\224\250\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227).png" differ diff --git "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\346\240\210_\351\230\237\345\210\227/\345\217\214\347\253\257\351\230\237\345\210\227.png" "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\346\240\210_\351\230\237\345\210\227/\345\217\214\347\253\257\351\230\237\345\210\227.png" new file mode 100644 index 0000000..f0ea2a5 Binary files /dev/null and "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\346\240\210_\351\230\237\345\210\227/\345\217\214\347\253\257\351\230\237\345\210\227.png" differ diff --git "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\346\240\210_\351\230\237\345\210\227/\346\234\200\345\260\217\346\240\210.md" "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\346\240\210_\351\230\237\345\210\227/\346\234\200\345\260\217\346\240\210.md" new file mode 100644 index 0000000..bb22293 --- /dev/null +++ "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\346\240\210_\351\230\237\345\210\227/\346\234\200\345\260\217\346\240\210.md" @@ -0,0 +1,83 @@ +## 最小栈 + +[链接](https://leetcode.cn/problems/min-stack/description/) + +**题目** + +设计一个支持 `push` ,`pop` ,`top` 操作,并能在常数时间内检索到最小元素的栈。 + +实现 `MinStack` 类: + +- `MinStack()` 初始化堆栈对象。 +- `void push(int val)` 将元素val推入堆栈。 +- `void pop()` 删除堆栈顶部的元素。 +- `int top()` 获取堆栈顶部的元素。 +- `int getMin()` 获取堆栈中的最小元素。 + +**示例 1:** + +``` +输入: +["MinStack","push","push","push","getMin","pop","top","getMin"] +[[],[-2],[0],[-3],[],[],[],[]] + +输出: +[null,null,null,null,-3,null,0,-2] + +解释: +MinStack minStack = new MinStack(); +minStack.push(-2); +minStack.push(0); +minStack.push(-3); +minStack.getMin(); --> 返回 -3. +minStack.pop(); +minStack.top(); --> 返回 0. +minStack.getMin(); --> 返回 -2. +``` + +#### 解题思路 + +第一种,利用两个栈来处理 + +![image-20231211232842368](/Users/guo/Notes/学习算法与数据结构笔记/images/链表/最小栈01.png) + +```swift +public class MinStack { + /* 用来存档正常数据 */ + private Stack stack1; + /* 用来存档最小数据 */ + private Stack minStack; + + public MinStack() { + stack1 = new Stack<>(); + minStack = new Stack<>(); + } + + public void push(int val) { + stack1.push(val); + if (minStack.isEmpty()) { + minStack.push(val); + } else { + int top = minStack.peek(); + minStack.push(Math.min(top, val)); + } + } + + public void pop() { + stack1.pop(); + minStack.pop(); + } + + public int top() { + return stack1.peek(); + } + + public int getMin() { + return minStack.peek(); + } +} +``` + + + +第二种, \ No newline at end of file diff --git "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\346\240\210_\351\230\237\345\210\227/\346\273\221\345\212\250\347\252\227\345\217\243.png" "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\346\240\210_\351\230\237\345\210\227/\346\273\221\345\212\250\347\252\227\345\217\243.png" new file mode 100644 index 0000000..061a75f Binary files /dev/null and "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\346\240\210_\351\230\237\345\210\227/\346\273\221\345\212\250\347\252\227\345\217\243.png" differ diff --git "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\346\240\210_\351\230\237\345\210\227/\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\346\240\210_\351\230\237\345\210\227/\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" new file mode 100644 index 0000000..554ad67 --- /dev/null +++ "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\346\240\210_\351\230\237\345\210\227/\346\273\221\345\212\250\347\252\227\345\217\243\346\234\200\345\244\247\345\200\274.md" @@ -0,0 +1,60 @@ +## 队列 + +[滑动窗口最大值](https://leetcode.cn/problems/sliding-window-maximum/description/) + +题目 + +给你一个整数数组 `nums`,有一个大小为 `k` 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 `k` 个数字。滑动窗口每次只向右移动一位。 + +返回 *滑动窗口中的最大值* 。 + + + +**示例 1:** + +``` +输入:nums = [1,3,-1,-3,5,3,6,7], k = 3 +输出:[3,3,5,5,6,7] +解释: +滑动窗口的位置 最大值 +--------------- ----- +[1 3 -1] -3 5 3 6 7 3 + 1 [3 -1 -3] 5 3 6 7 3 + 1 3 [-1 -3 5] 3 6 7 5 + 1 3 -1 [-3 5 3] 6 7 5 + 1 3 -1 -3 [5 3 6] 7 6 + 1 3 -1 -3 5 [3 6 7] 7 +``` + +**示例 2:** + +``` +输入:nums = [1], k = 1 +输出:[1] +``` + +**提示:** + +- `1 <= nums.length <= 105` +- `-104 <= nums[i] <= 104` +- `1 <= k <= nums.length` + +#### 解题思路 + +1、基于队列思路,使用双端队列 + +![image-20231217230751611](/Users/guo/Notes/学习算法与数据结构笔记/算法题/栈_队列/双端队列.png) + +![image-20231217230831149](/Users/guo/Notes/学习算法与数据结构笔记/算法题/栈_队列/滑动窗口.png) + +执行步骤 + +1、如果nums[i] > nums[队尾],不断删除队尾,直到nums[队尾] > nums[i] 为止 + +2、将i 加入队尾 + +3、如果 w >= 0, 那么 + +(1)、如果对头失效,就移除队头(如果队头 < w,就代表失效) + +(2)、设置w 窗口的最大值为 nums[队头] \ No newline at end of file diff --git "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\351\223\276\350\241\250/\345\233\236\346\226\207\351\223\276\350\241\250.md" "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\351\223\276\350\241\250/\345\233\236\346\226\207\351\223\276\350\241\250.md" index f125c84..7f45361 100644 --- "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\351\223\276\350\241\250/\345\233\236\346\226\207\351\223\276\350\241\250.md" +++ "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\351\223\276\350\241\250/\345\233\236\346\226\207\351\223\276\350\241\250.md" @@ -28,3 +28,12 @@ **解题** +1、先找到哦中间节点 + +2、翻转中间节点的右半部分 + +3、分别通过head和右半部分节点逐次遍历比较,在右半部分遇到null之前,如果节点的val都相同,则认为是回文列表,否则认为不是 + +**这个解题思路的问题在于会破坏原链表的结构** + +![image-20231210101631804](/Users/guo/Notes/学习算法与数据结构笔记/images/链表/回文链表.png) diff --git "a/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\351\223\276\350\241\250/\350\247\243\351\242\230\346\212\200\345\267\247.md" "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\351\223\276\350\241\250/\350\247\243\351\242\230\346\212\200\345\267\247.md" new file mode 100644 index 0000000..0221076 --- /dev/null +++ "b/\345\255\246\344\271\240\347\256\227\346\263\225\344\270\216\346\225\260\346\215\256\347\273\223\346\236\204\347\254\224\350\256\260/\347\256\227\346\263\225\351\242\230/\351\223\276\350\241\250/\350\247\243\351\242\230\346\212\200\345\267\247.md" @@ -0,0 +1,19 @@ +## 解题思路 + +#### 建议 + +1、多画图 + +#### 技巧 + +* 虚拟头节点 +* 快慢指针 +* 多指针 + +#### 代码要非常熟练 + +* 链表节点的插入、删除 +* 反转(翻转) 一个链表 +* 快慢指针求中心节点 +* 计算链表的长度 + diff --git "a/\347\247\221\345\255\246\346\226\271\345\274\217/2022-11-25-oracle.md" "b/\347\247\221\345\255\246\346\226\271\345\274\217/2022-11-25-oracle.md" index d50c7ea..f8ae018 100644 --- "a/\347\247\221\345\255\246\346\226\271\345\274\217/2022-11-25-oracle.md" +++ "b/\347\247\221\345\255\246\346\226\271\345\274\217/2022-11-25-oracle.md" @@ -4,7 +4,7 @@ 1、域名 -目前是在2022-11-24日在[nameSilo](www.namesilo.com)购买了1年的域名,购买的域名是:guoj.xyz,有效期一年,价格:1.99$ +目前是在2023-12-9日在[nameSilo](www.namesilo.com)购买了1年的域名,购买的域名是:guoj.shop,有效期一年,价格:1.25$ 域名解析是在 cloudflare 上面,[地址](https://dash.cloudflare.com/8bc71dd98544b5c2c59731999c5b9c32) @@ -119,7 +119,7 @@ bash <(curl -Ls https://raw.githubusercontent.com/vaxilu/x-ui/master/install.sh) 2023年-5-16-注册新账号 -c 账号:gjcoder@hotmail.com + 账号:gjcoder@hotmail.com [参考地址](https://github.com/bigdongdongCLUB/GoodGoodStudyDayDayUp/issues/8)