mirror of
				https://github.com/chefyuan/algorithm-base.git
				synced 2025-10-31 19:52:42 +00:00 
			
		
		
		
	chefyuan
This commit is contained in:
		
							
								
								
									
										10
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								README.md
									
									
									
									
									
								
							| @@ -81,6 +81,16 @@ | |||||||
| - [【动画模拟】leetcode 82 删除排序链表的重复元素2](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode82%E5%88%A0%E9%99%A4%E6%8E%92%E5%BA%8F%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E9%87%8D%E5%A4%8D%E5%85%83%E7%B4%A0II.md) | - [【动画模拟】leetcode 82 删除排序链表的重复元素2](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode82%E5%88%A0%E9%99%A4%E6%8E%92%E5%BA%8F%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E9%87%8D%E5%A4%8D%E5%85%83%E7%B4%A0II.md) | ||||||
| - [【动画模拟】面试题 02.05 链表求和](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/%E9%9D%A2%E8%AF%95%E9%A2%98%2002.05.%20%E9%93%BE%E8%A1%A8%E6%B1%82%E5%92%8C.md) | - [【动画模拟】面试题 02.05 链表求和](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/%E9%9D%A2%E8%AF%95%E9%A2%98%2002.05.%20%E9%93%BE%E8%A1%A8%E6%B1%82%E5%92%8C.md) | ||||||
|  |  | ||||||
|  | ### 🚁双指针 | ||||||
|  |  | ||||||
|  | - [【动画模拟】二分查找详解](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE%E5%8F%8A%E5%85%B6%E5%8F%98%E7%A7%8D/%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE%E8%AF%A6%E8%A7%A3.md) | ||||||
|  | - [【动画模拟】leetcode 35 搜索插入位置](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE%E5%8F%8A%E5%85%B6%E5%8F%98%E7%A7%8D/leetcode35%E6%90%9C%E7%B4%A2%E6%8F%92%E5%85%A5%E4%BD%8D%E7%BD%AE.md) | ||||||
|  | - [【动画模拟】leetcode 27 移除元素](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E6%95%B0%E7%BB%84%E7%AF%87/leetcode27%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0.md) | ||||||
|  | - 【动画模拟】 | ||||||
|  | - 【动画模拟】 | ||||||
|  | - 【动画模拟】 | ||||||
|  | - 【动画模拟】 | ||||||
|  |  | ||||||
| ### 🏳🌈栈和队列 | ### 🏳🌈栈和队列 | ||||||
|  |  | ||||||
| - [【动画模拟】leetcode 225 队列实现栈](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E6%A0%88%E5%92%8C%E9%98%9F%E5%88%97/225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.md) | - [【动画模拟】leetcode 225 队列实现栈](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E6%A0%88%E5%92%8C%E9%98%9F%E5%88%97/225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.md) | ||||||
|   | |||||||
							
								
								
									
										62
									
								
								animation-simulation/数组篇/长度最小的子数组.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								animation-simulation/数组篇/长度最小的子数组.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,62 @@ | |||||||
|  | > 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友  **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注  github  + 题目 + 问题  向我反馈 | ||||||
|  | > | ||||||
|  | > 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。 | ||||||
|  | > | ||||||
|  | > 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。  | ||||||
|  |  | ||||||
|  | #### [209. 长度最小的子数组](https://leetcode-cn.com/problems/minimum-size-subarray-sum/) | ||||||
|  |  | ||||||
|  | 我们下面再看一种新类型的双指针,也就是我们大家熟知的滑动窗口。这也是我们做题时经常用到的,下面我们来看一下题目吧! | ||||||
|  |  | ||||||
|  | #### 题目描述 | ||||||
|  |  | ||||||
|  | > 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的 连续 子数组,并返回其长度。如果不存在符合条件的子数组,返回 0。 | ||||||
|  |  | ||||||
|  | 示例: | ||||||
|  |  | ||||||
|  | > 输入:s  =  7, nums  =  [2,3,1,2,4,3] | ||||||
|  | > 输出:2 | ||||||
|  | > 解释:子数组 [4,3] 是该条件下的长度最小的子数组。 | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #### 题目解析 | ||||||
|  |  | ||||||
|  | 滑动窗口:**就是通过不断调节子数组的起始位置和终止位置,进而得到我们想要的结果**,滑动窗口也是双指针的一种。 | ||||||
|  |  | ||||||
|  | 下面我们来看一下这道题目的做题思路,其实原理也很简单,我们创建两个指针,一个指针负责在前面探路,并不断累加遍历过的元素的值,当和大于等于我们的目标值时,后指针开始进行移动,判断去除当前值时,是否仍能满足我们的要求,直到不满足时后指针 停止,前面指针继续移动,直到遍历结束。是不是很简单呀。前指针和后指针之间的元素个数就是我们的滑动窗口的窗口大小。见下图 | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 好啦,该题的解题思路我们已经了解啦,下面我们看一下,代码的运行过程吧。 | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #### 题目代码 | ||||||
|  |  | ||||||
|  | ```java | ||||||
|  | class Solution { | ||||||
|  |     public int minSubArrayLen(int s, int[] nums) { | ||||||
|  |  | ||||||
|  |         int len = nums.length; | ||||||
|  |         int windowlen = Integer.MAX_VALUE; | ||||||
|  |         int i = 0; | ||||||
|  |         int sum = 0; | ||||||
|  |         for (int j = 0; j < len; ++j) { | ||||||
|  |             sum += nums[j]; | ||||||
|  |             while (sum >= s) { | ||||||
|  |                 windowlen = Math.min (windowlen, j - i + 1); | ||||||
|  |                 sum -= nums[i]; | ||||||
|  |                 i++; | ||||||
|  |             } | ||||||
|  |         }      | ||||||
|  |         return windowlen == Integer.MAX_VALUE ? 0 : windowlen; | ||||||
|  |  | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | ``` | ||||||
|  |  | ||||||
							
								
								
									
										58
									
								
								animation-simulation/链表篇/leetcode141环形链表.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								animation-simulation/链表篇/leetcode141环形链表.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,58 @@ | |||||||
|  | > 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友  **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注  github  + 题目 + 问题  向我反馈 | ||||||
|  | > | ||||||
|  | > 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。 | ||||||
|  | > | ||||||
|  | > 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。  | ||||||
|  |  | ||||||
|  | ### [141. 环形链表](https://leetcode-cn.com/problems/linked-list-cycle/) | ||||||
|  |  | ||||||
|  | 下面我们再来了解一种双指针,我们称之为快慢指针,顾名思义一个指针速度快,一个指针速度慢。 | ||||||
|  |  | ||||||
|  | #### 题目描述 | ||||||
|  |  | ||||||
|  | > 给定一个链表,判断链表中是否有环。pos代表环的入口,若为-1,则代表无环 | ||||||
|  | > | ||||||
|  | > 如果链表中存在环,则返回 true 。 否则,返回 false 。 | ||||||
|  |  | ||||||
|  | 示例1: | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | > 输入:head = [3,2,0,-4], pos = 1 | ||||||
|  | > 输出:true | ||||||
|  | > 解释:链表中有一个环,其尾部连接到第二个节点。 | ||||||
|  |  | ||||||
|  | #### 题目解析 | ||||||
|  |  | ||||||
|  | 题目很容易理解,让我们判断链表中是否有环,我们只需通过我们的快慢指针即可,我们试想一下,如果链表中有环的话,一个速度快的指针和一个速度慢的指针在环中运动的话,若干圈后快指针肯定可以追上慢指针的。这是一定的。 | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 好啦,做题思路已经有了,让我们一起看一下代码的执行过程吧。\ | ||||||
|  |  | ||||||
|  | **动画模拟** | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | **题目代码** | ||||||
|  |  | ||||||
|  | ```java | ||||||
|  | public class Solution { | ||||||
|  |     public boolean hasCycle(ListNode head) { | ||||||
|  |      | ||||||
|  |          ListNode fast = head; | ||||||
|  |          ListNode low = head; | ||||||
|  |          while (fast != null && fast.next != null) { | ||||||
|  |              fast = fast.next.next; | ||||||
|  |              low = low.next; | ||||||
|  |              if (fast == low) { | ||||||
|  |                  return true; | ||||||
|  |              } | ||||||
|  |          } | ||||||
|  |          return false; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | ``` | ||||||
|  |  | ||||||
							
								
								
									
										62
									
								
								animation-simulation/链表篇/leetcode328奇偶链表.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								animation-simulation/链表篇/leetcode328奇偶链表.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,62 @@ | |||||||
|  | > 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友  **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注  github  + 题目 + 问题  向我反馈 | ||||||
|  | > | ||||||
|  | > 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。 | ||||||
|  | > | ||||||
|  | > 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。  | ||||||
|  |  | ||||||
|  | ### [328. 奇偶链表](https://leetcode-cn.com/problems/odd-even-linked-list/) | ||||||
|  |  | ||||||
|  | 下面我们再来看一种双指针,我称之为交替领先双指针,起名鬼才哈哈。下面我们一起来看看吧。 | ||||||
|  |  | ||||||
|  | #### 题目描述 | ||||||
|  |  | ||||||
|  | > 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。 | ||||||
|  | > | ||||||
|  | > 请尝试使用原地算法完成。你的算法的空间复杂度应为 O(1),时间复杂度应为 O(nodes),nodes 为节点总数。 | ||||||
|  |  | ||||||
|  | 示例 1: | ||||||
|  |  | ||||||
|  | > 输入: 1->2->3->4->5->NULL | ||||||
|  | > 输出: 1->3->5->2->4->NULL | ||||||
|  |  | ||||||
|  | 示例 2: | ||||||
|  |  | ||||||
|  | > 输入: 2->1->3->5->6->4->7->NULL  | ||||||
|  | > 输出: 2->3->6->7->1->5->4->NULL | ||||||
|  |  | ||||||
|  | #### 题目解析 | ||||||
|  |  | ||||||
|  | 题目也很容易理解就是让我们将原来奇数位的结点放一起,偶数位的结点放一起。这里需要注意,和结点值无关,是奇数位和偶数位结点。 | ||||||
|  |  | ||||||
|  | 我们可以先将奇数位和在一起,再将偶数位和在一起,最后再将两个链表合并很简单,我们直接看动画模拟吧。 | ||||||
|  |  | ||||||
|  | #### **动画模拟** | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | #### 题目代码 | ||||||
|  |  | ||||||
|  | ```java | ||||||
|  | class Solution { | ||||||
|  |     public ListNode oddEvenList(ListNode head) { | ||||||
|  |          if (head == null || head.next == null) { | ||||||
|  |              return head; | ||||||
|  |          } | ||||||
|  |          ListNode odd = head; | ||||||
|  |          ListNode even = head.next; | ||||||
|  |          ListNode evenhead = even; | ||||||
|  |  | ||||||
|  |          while (odd.next != null && even.next != null) { | ||||||
|  |              //将偶数位合在一起,奇数位合在一起 | ||||||
|  |              odd.next = even.next; | ||||||
|  |              odd = odd.next; | ||||||
|  |              even.next = odd.next; | ||||||
|  |              even = even.next; | ||||||
|  |          }   | ||||||
|  |          //链接 | ||||||
|  |          odd.next = evenhead; | ||||||
|  |          return head; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | ``` | ||||||
|  |  | ||||||
		Reference in New Issue
	
	Block a user