mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2026-03-14 13:51:15 +00:00
代码重构 【Github Actions】
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
|
||||
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
>
|
||||
> 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。
|
||||
>
|
||||
@@ -18,7 +16,6 @@
|
||||
|
||||
请你返回这一天营业下来,最多有多少客户能够感到满意的数量。
|
||||
|
||||
|
||||
示例:
|
||||
|
||||
> 输入:customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], X = 3
|
||||
@@ -28,8 +25,6 @@
|
||||
书店老板在最后 3 分钟保持冷静。
|
||||
感到满意的最大客户数量 = 1 + 1 + 1 + 1 + 7 + 5 = 16.
|
||||
|
||||
|
||||
|
||||
该题目思想就是,我们将 customer 数组的值分为三部分, leftsum, winsum, rightsum。我们题目的返回值则是三部分的最大和。
|
||||
|
||||
注意这里的最大和,我们是怎么计算的。
|
||||
@@ -50,7 +45,7 @@ rightsum 是窗口右区间的值,和左区间加和方式一样。那么我
|
||||
|
||||
则左半区间范围扩大,但是 leftsum 的值没有变,这时因为新加入的值,所对应的 grumpy[i] == 1,所以其值不会发生改变,因为我们只统计 grumpy[i] == 0 的值,
|
||||
|
||||
右半区间范围减少,rightsum 值也减少,因为右半区间减小的值,其对应的 grumpy[i] == 0,所以 rightsum -= grumpy[i]。
|
||||
右半区间范围减少,rightsum 值也减少,因为右半区间减小的值,其对应的 grumpy[i] == 0,所以 rightsum -= grumpy[i]。
|
||||
|
||||
winsum 也会发生变化, winsum 需要加上新加入窗口的值,减去刚离开窗口的值, 也就是 customer[left-1],left 代表窗口左边缘。
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
#### [1438. 绝对差不超过限制的最长连续子数组](https://leetcode-cn.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/)
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
示例
|
||||
|
||||
> 输入:nums = [10,1,2,4,7,2], limit = 5
|
||||
> 输出:4
|
||||
> 输出:4
|
||||
> 解释:满足题意的最长子数组是 [2,4,7,2],其最大绝对差 |2-7| = 5 <= 5 。
|
||||
|
||||
**提示:**
|
||||
@@ -48,7 +48,7 @@ Java Code:
|
||||
```java
|
||||
class Solution {
|
||||
public int longestSubarray(int[] nums, int limit) {
|
||||
|
||||
|
||||
Deque<Integer> maxdeque = new LinkedList<>();
|
||||
Deque<Integer> mindeque = new LinkedList<>();
|
||||
int len = nums.length;
|
||||
@@ -63,7 +63,7 @@ class Solution {
|
||||
}
|
||||
//需要更多视频解算法,可以来我的公众号:袁厨的算法小屋
|
||||
maxdeque.addLast(nums[right]);
|
||||
mindeque.addLast(nums[right]);
|
||||
mindeque.addLast(nums[right]);
|
||||
while (maxdeque.peekFirst() - mindeque.peekFirst() > limit) {
|
||||
if (maxdeque.peekFirst() == nums[left]) maxdeque.removeFirst();
|
||||
if (mindeque.peekFirst() == nums[left]) mindeque.removeFirst();
|
||||
@@ -96,7 +96,7 @@ class Solution:
|
||||
maxdeque.pop()
|
||||
while len(mindeque) != 0 and mindeque[-1] > nums[right]:
|
||||
mindeque.pop()
|
||||
|
||||
|
||||
maxdeque.append(nums[right])
|
||||
mindeque.append(nums[right])
|
||||
while (maxdeque[0] - mindeque[0]) > limit:
|
||||
@@ -114,6 +114,7 @@ class Solution:
|
||||
Swift Code
|
||||
|
||||
Swift:数组模拟,超时(58 / 61 个通过测试用例)
|
||||
|
||||
```swift
|
||||
class Solution {
|
||||
func longestSubarray(_ nums: [Int], _ limit: Int) -> Int {
|
||||
@@ -147,7 +148,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Swift:使用双端队列(击败了100.00%)
|
||||
Swift:使用双端队列(击败了 100.00%)
|
||||
|
||||
```swift
|
||||
class Solution {
|
||||
@@ -259,4 +260,3 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
#### [1. 两数之和](https://leetcode-cn.com/problems/two-sum/)
|
||||
|
||||
@@ -25,11 +25,11 @@
|
||||
|
||||
**解析**
|
||||
|
||||
双指针(L,R)法的思路很简单,L指针用来指向第一个值,R指针用来从第L指针的后面查找数组中是否含有和L指针指向值和为目标值的数。见下图
|
||||
双指针(L,R)法的思路很简单,L 指针用来指向第一个值,R 指针用来从第 L 指针的后面查找数组中是否含有和 L 指针指向值和为目标值的数。见下图
|
||||
|
||||

|
||||
|
||||
例:绿指针指向的值为3,蓝指针需要在绿指针的后面遍历查找是否含有 target - 3 = 2的元素,若含有返回即可。
|
||||
例:绿指针指向的值为 3,蓝指针需要在绿指针的后面遍历查找是否含有 target - 3 = 2 的元素,若含有返回即可。
|
||||
|
||||
**题目代码**
|
||||
|
||||
@@ -85,7 +85,7 @@ class Solution {
|
||||
if count < 2 {
|
||||
return [0]
|
||||
}
|
||||
|
||||
|
||||
var rearr: [Int] = []
|
||||
// 查询元素
|
||||
for i in 0..<count {
|
||||
@@ -106,7 +106,7 @@ class Solution {
|
||||
|
||||
**解析**
|
||||
|
||||
哈希表的做法很容易理解,我们只需通过一次循环即可,假如我们的 target 值为 9,当前指针指向的值为 2 ,我们只需从哈希表中查找是否含有 7,因为9 - 2 =7 。如果含有 7 我们直接返回即可,如果不含有则将当前的2存入哈希表中,指针移动,指向下一元素。注: key 为元素值,value 为元素索引。
|
||||
哈希表的做法很容易理解,我们只需通过一次循环即可,假如我们的 target 值为 9,当前指针指向的值为 2 ,我们只需从哈希表中查找是否含有 7,因为 9 - 2 =7 。如果含有 7 我们直接返回即可,如果不含有则将当前的 2 存入哈希表中,指针移动,指向下一元素。注: key 为元素值,value 为元素索引。
|
||||
|
||||
**动图解析:**
|
||||
|
||||
@@ -137,7 +137,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
C++ Code:
|
||||
C++ Code:
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
@@ -171,7 +171,7 @@ const twoSum = function (nums, target) {
|
||||
|
||||
Python3 Code:
|
||||
|
||||
```python
|
||||
```python
|
||||
from typing import List
|
||||
class Solution:
|
||||
def twoSum(self, nums: List[int], target: int)->List[int]:
|
||||
@@ -201,4 +201,4 @@ class Solution {
|
||||
return [0]
|
||||
}
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
### [219 数组中重复元素2](https://leetcode-cn.com/problems/contains-duplicate-ii/)
|
||||
### [219 数组中重复元素 2](https://leetcode-cn.com/problems/contains-duplicate-ii/)
|
||||
|
||||
**题目描述**
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
|
||||
**Hashmap**
|
||||
|
||||
这个题目和我们上面那个数组中的重复数字几乎相同,只不过是增加了一个判断相隔是否小于K位的条件,我们先用 HashMap 来做一哈,和刚才思路一致,我们直接看代码就能整懂
|
||||
这个题目和我们上面那个数组中的重复数字几乎相同,只不过是增加了一个判断相隔是否小于 K 位的条件,我们先用 HashMap 来做一哈,和刚才思路一致,我们直接看代码就能整懂
|
||||
|
||||
Java Code:
|
||||
|
||||
@@ -45,7 +45,7 @@ class Solution {
|
||||
if (map.containsKey(nums[i])) {
|
||||
//判断是否小于K,如果小于等于则直接返回
|
||||
int abs = Math.abs(i - map.get(nums[i]));
|
||||
if (abs <= k) return true;//小于等于则返回
|
||||
if (abs <= k) return true;//小于等于则返回
|
||||
}
|
||||
//更新索引,此时有两种情况,不存在,或者存在时,将后出现的索引保存
|
||||
map.put(nums[i],i);
|
||||
@@ -72,7 +72,7 @@ class Solution:
|
||||
# 判断是否小于K,如果小于等于则直接返回
|
||||
a = abs(i - m[nums[i]])
|
||||
if a <= k:
|
||||
return True# 小于等于则返回
|
||||
return True# 小于等于则返回
|
||||
# 更新索引,此时有两种情况,不存在,或者存在时,将后出现的索引保存
|
||||
m[nums[i]] = i
|
||||
return False
|
||||
@@ -128,8 +128,6 @@ class Solution {
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
**题目代码**
|
||||
|
||||
Java Code
|
||||
@@ -223,4 +221,4 @@ class Solution {
|
||||
return false
|
||||
}
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
#### [27. 移除元素](https://leetcode-cn.com/problems/remove-element/)
|
||||
|
||||
@@ -36,17 +36,15 @@
|
||||
|
||||
**解析**
|
||||
|
||||
该题目也算是简单题目,适合新手来做,然后大家也不要看不起暴力解法,我们可以先写出暴力解法,然后再思考其他方法,这对于我们的编码能力有很大的帮助。我们来解析一下这个题目的做题思路,他的含义就是让我们删除掉数组中的元素,然后将数组后面的元素跟上来。最后返回删除掉元素的数组长度即可。比如数组长度为 10,里面有2个目标值,我们最后返回的长度为 8,但是返回的 8 个元素,需要排在数组的最前面。那么暴力解法的话则就需要两个 for 循环,一个用来找到删除,另一个用来更新数组。
|
||||
该题目也算是简单题目,适合新手来做,然后大家也不要看不起暴力解法,我们可以先写出暴力解法,然后再思考其他方法,这对于我们的编码能力有很大的帮助。我们来解析一下这个题目的做题思路,他的含义就是让我们删除掉数组中的元素,然后将数组后面的元素跟上来。最后返回删除掉元素的数组长度即可。比如数组长度为 10,里面有 2 个目标值,我们最后返回的长度为 8,但是返回的 8 个元素,需要排在数组的最前面。那么暴力解法的话则就需要两个 for 循环,一个用来找到删除,另一个用来更新数组。
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
总体思路就是这样的,后面的会不断往前覆盖。暴力解法也是不超时的,实现也不算太简单主要需要注意两个地方。
|
||||
|
||||
(1)需要先定义变量len获取数组长度,因为后面我们的返回的数组长度是改变的,所以不可以用 nums.length 作为上界
|
||||
(1)需要先定义变量 len 获取数组长度,因为后面我们的返回的数组长度是改变的,所以不可以用 nums.length 作为上界
|
||||
|
||||
(2)我们每找到一个需要删除的值的时候,需要i--,防止出现多个需要删除的值在一起的情况,然后漏删。
|
||||
(2)我们每找到一个需要删除的值的时候,需要 i--,防止出现多个需要删除的值在一起的情况,然后漏删。
|
||||
|
||||
**题目代码**
|
||||
|
||||
@@ -54,7 +52,7 @@ Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public int removeElement(int[] nums, int val) {
|
||||
public int removeElement(int[] nums, int val) {
|
||||
//获取数组长度
|
||||
int len = nums.length;
|
||||
if (len == 0) {
|
||||
@@ -72,7 +70,7 @@ class Solution {
|
||||
len--;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -178,9 +176,9 @@ class Solution {
|
||||
nums[i] = nums[j]
|
||||
i += 1
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return i
|
||||
}
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
>
|
||||
> 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。
|
||||
>
|
||||
@@ -31,7 +31,7 @@
|
||||
|
||||

|
||||
|
||||
上图中,我们遍历一遍原数组,将正整数保存到新数组中,然后遍历新数组,第一次发现 newnum[i] != i 时,则说明该值是缺失的,返回即可,例如我上图中的第一个示例中的 2,如果遍历完新数组,发现说所有值都对应,说明缺失的是 新数组的长度对应的那个数,比如第二个示例中 ,新数组的长度为 5,此时缺失的为 5,返回长度即可,很容易理解。
|
||||
上图中,我们遍历一遍原数组,将正整数保存到新数组中,然后遍历新数组,第一次发现 newnum[i] != i 时,则说明该值是缺失的,返回即可,例如我上图中的第一个示例中的 2,如果遍历完新数组,发现说所有值都对应,说明缺失的是 新数组的长度对应的那个数,比如第二个示例中 ,新数组的长度为 5,此时缺失的为 5,返回长度即可,很容易理解。
|
||||
|
||||
注:我们发现我们新的数组长度比原数组大 1,是因为我们遍历新数组从 1,开始遍历。
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
>
|
||||
> 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。
|
||||
>
|
||||
@@ -6,13 +6,13 @@
|
||||
|
||||
#### [485. 最大连续 1 的个数](https://leetcode-cn.com/problems/max-consecutive-ones/)
|
||||
|
||||
给定一个二进制数组, 计算其中最大连续1的个数。
|
||||
给定一个二进制数组, 计算其中最大连续 1 的个数。
|
||||
|
||||
示例 1:
|
||||
|
||||
> 输入: [1,1,0,1,1,1]
|
||||
> 输出: 3
|
||||
> 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
|
||||
> 解释: 开头的两位和最后的三位都是连续 1,所以最大连续 1 的个数是 3.
|
||||
|
||||
我的这个方法比较奇怪,但是效率还可以,战胜了 100% , 尽量减少了 Math.max()的使用,我们来看一下具体思路,利用 right 指针进行探路,如果遇到 1 则继续走,遇到零时则停下,求当前 1 的个数。
|
||||
|
||||
@@ -22,11 +22,7 @@
|
||||
|
||||
下面我们通过一个视频模拟代码执行步骤大家一下就能搞懂了。
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
|
||||

|
||||
|
||||
下面我们直接看代码吧
|
||||
|
||||
@@ -143,7 +139,6 @@ class Solution {
|
||||
|
||||
Python3 Code:
|
||||
|
||||
|
||||
```py
|
||||
class Solution:
|
||||
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
|
||||
@@ -211,4 +206,4 @@ public:
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
||||
```
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
#### [54. 螺旋矩阵](https://leetcode-cn.com/problems/spiral-matrix/)
|
||||
|
||||
题目描述
|
||||
|
||||
*给定一个包含 m* x n个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
|
||||
|
||||
|
||||
_给定一个包含 m_ x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
|
||||
|
||||
示例一
|
||||
|
||||
@@ -22,24 +20,14 @@
|
||||
> 输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
|
||||
> 输出:[1,2,3,4,8,12,11,10,9,5,6,7]
|
||||
|
||||
|
||||
|
||||
这个题目很细非常细,思路很容易想到,但是要是完全实现也不是特别容易,我们一起分析下这个题目,我们可以这样理解,我们像剥洋葱似的一步步的剥掉外皮,直到遍历结束,见下图。
|
||||
|
||||
|
||||
|
||||
*
|
||||
|
||||
|
||||
\*
|
||||
|
||||
题目很容易理解,但是要想完全执行出来,也是不容易的,因为这里面的细节太多了,我们需要认真仔细的考虑边界。
|
||||
|
||||
|
||||
|
||||
我们也要考虑重复遍历的情况即什么时候跳出循环。刚才我们通过箭头知道了我们元素的遍历顺序,这个题目也就完成了一大半了,下面我们来讨论一下什么时候跳出循环,见下图。
|
||||
|
||||
|
||||
|
||||
注:这里需要注意的是,框框代表的是每个边界。
|
||||
|
||||

|
||||
@@ -55,7 +43,7 @@ class Solution {
|
||||
List<Integer> arr = new ArrayList<>();
|
||||
int left = 0, right = matrix[0].length-1;
|
||||
int top = 0, down = matrix.length-1;
|
||||
|
||||
|
||||
while (true) {
|
||||
for (int i = left; i <= right; ++i) {
|
||||
arr.add(matrix[top][i]);
|
||||
@@ -77,7 +65,7 @@ class Solution {
|
||||
}
|
||||
left++;
|
||||
if (left > right) break;
|
||||
|
||||
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
@@ -192,4 +180,3 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
|
||||
|
||||
### [leetcode560. 和为K的子数组](https://leetcode-cn.com/problems/subarray-sum-equals-k/)
|
||||
### [leetcode560. 和为 K 的子数组](https://leetcode-cn.com/problems/subarray-sum-equals-k/)
|
||||
|
||||
**题目描述**
|
||||
|
||||
@@ -43,9 +43,9 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python3版本的代码会超时
|
||||
Python3 版本的代码会超时
|
||||
|
||||
Swift版本的代码会超时
|
||||
Swift 版本的代码会超时
|
||||
|
||||
下面我们我们使用前缀和的方法来解决这个题目,那么我们先来了解一下前缀和是什么东西。其实这个思想我们很早就接触过了。见下图
|
||||
|
||||
@@ -57,7 +57,7 @@ presum [2] = presum[1] + nums[1],presum[3] = presum[2] + nums[2] ... 所以我
|
||||
|
||||
例如我们需要获取 nums[2] 到 nums[4] 这个区间的和,我们则完全根据 presum 数组得到,是不是有点和我们之前说的字符串匹配算法中 BM,KMP 中的 next 数组和 suffix 数组作用类似。
|
||||
|
||||
那么我们怎么根据presum 数组获取 nums[2] 到 nums[4] 区间的和呢?见下图
|
||||
那么我们怎么根据 presum 数组获取 nums[2] 到 nums[4] 区间的和呢?见下图
|
||||
|
||||

|
||||
|
||||
@@ -94,7 +94,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Python3版本的代码也会超时
|
||||
Python3 版本的代码也会超时
|
||||
|
||||
我们通过上面的例子我们简单了解了前缀和思想,那么我们如果继续将其优化呢?
|
||||
|
||||
@@ -216,4 +216,4 @@ public:
|
||||
return result;
|
||||
}
|
||||
};
|
||||
```
|
||||
```
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
|
||||
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
### [59.螺旋矩阵 II](https://leetcode-cn.com/problems/spiral-matrix-ii)
|
||||
|
||||
@@ -22,15 +20,11 @@
|
||||
|
||||
其实我们只要做过了螺旋矩阵 第一题,这个题目我们完全可以一下搞定,几乎没有进行更改,我们先来看下 **leetcode 54** 题的解析。
|
||||
|
||||
|
||||
|
||||
### leetcode 54 螺旋矩阵
|
||||
|
||||
题目描述
|
||||
|
||||
*给定一个包含 m* x n个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
|
||||
|
||||
|
||||
_给定一个包含 m_ x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
|
||||
|
||||
示例一
|
||||
|
||||
@@ -42,24 +36,14 @@
|
||||
> 输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
|
||||
> 输出:[1,2,3,4,8,12,11,10,9,5,6,7]
|
||||
|
||||
|
||||
|
||||
这个题目很细非常细,思路很容易想到,但是要是完全实现也不是特别容易,我们一起分析下这个题目,我们可以这样理解,我们像剥洋葱似的一步步的剥掉外皮,直到遍历结束,见下图。
|
||||
|
||||
|
||||
|
||||
**
|
||||
|
||||
|
||||
__
|
||||
|
||||
题目很容易理解,但是要想完全执行出来,也是不容易的,因为这里面的细节太多了,我们需要认真仔细的考虑边界。
|
||||
|
||||
|
||||
|
||||
我们也要考虑重复遍历的情况即什么时候跳出循环。刚才我们通过箭头知道了我们元素的遍历顺序,这个题目也就完成了一大半了,下面我们来讨论一下什么时候跳出循环,见下图。
|
||||
|
||||
|
||||
|
||||
注:这里需要注意的是,框框代表的是每个边界。
|
||||
|
||||

|
||||
@@ -75,7 +59,7 @@ class Solution {
|
||||
List<Integer> arr = new ArrayList<>();
|
||||
int left = 0, right = matrix[0].length-1;
|
||||
int top = 0, down = matrix.length-1;
|
||||
|
||||
|
||||
while (true) {
|
||||
for (int i = left; i <= right; ++i) {
|
||||
arr.add(matrix[top][i]);
|
||||
@@ -97,7 +81,7 @@ class Solution {
|
||||
}
|
||||
left++;
|
||||
if (left > right) break;
|
||||
|
||||
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
@@ -220,7 +204,7 @@ Java Code:
|
||||
```java
|
||||
class Solution {
|
||||
public int[][] generateMatrix(int n) {
|
||||
|
||||
|
||||
int[][] arr = new int[n][n];
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
@@ -230,13 +214,13 @@ class Solution {
|
||||
int numsize = n*n;
|
||||
while (true) {
|
||||
for (int i = left; i <= right; ++i) {
|
||||
arr[top][i] = num++;
|
||||
arr[top][i] = num++;
|
||||
}
|
||||
top++;
|
||||
if (num > numsize) break;
|
||||
for (int i = top; i <= buttom; ++i) {
|
||||
arr[i][right] = num++;
|
||||
|
||||
|
||||
}
|
||||
right--;
|
||||
if (num > numsize) break;
|
||||
@@ -250,7 +234,7 @@ class Solution {
|
||||
}
|
||||
left++;
|
||||
if (num > numsize) break;
|
||||
|
||||
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
@@ -309,7 +293,7 @@ public:
|
||||
int left = 0, right = n-1, top = 0, buttom = n - 1, num = 1, numsize = n * n;
|
||||
while (true) {
|
||||
for (int i = left; i <= right; ++i) {
|
||||
arr[top][i] = num++;
|
||||
arr[top][i] = num++;
|
||||
}
|
||||
top++;
|
||||
if (num > numsize) break;
|
||||
@@ -328,7 +312,7 @@ public:
|
||||
}
|
||||
left++;
|
||||
if (num > numsize) break;
|
||||
|
||||
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
#### [66. 加一](https://leetcode-cn.com/problems/plus-one/)
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
|
||||
则我们根据什么来判断属于第几种情况呢?
|
||||
|
||||
我们可以根据当前位 余10来判断,这样我们就可以区分属于第几种情况了,大家直接看代码吧,很容易理解的。
|
||||
我们可以根据当前位 余 10 来判断,这样我们就可以区分属于第几种情况了,大家直接看代码吧,很容易理解的。
|
||||
|
||||
Java Code:
|
||||
|
||||
@@ -56,10 +56,10 @@ class Solution {
|
||||
if (digits[i] != 0) {
|
||||
return digits;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
//第三种情况,因为数组初始化每一位都为0,我们只需将首位设为1即可
|
||||
int[] arr = new int[len+1];
|
||||
int[] arr = new int[len+1];
|
||||
arr[0] = 1;
|
||||
return arr;
|
||||
}
|
||||
@@ -122,4 +122,3 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
### [75 颜色分类](https://leetcode-cn.com/problems/sort-colors/)
|
||||
### [75 颜色分类](https://leetcode-cn.com/problems/sort-colors/)
|
||||
|
||||
题目描述:
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
|
||||
**做题思路**
|
||||
|
||||
这个题目我们使用 Arrays.sort() 解决,哈哈,但是那样太无聊啦,题目含义就是让我们将所有的 0 放在前面,2放在后面,1 放在中间,是不是和我们上面说的荷兰国旗问题一样。我们仅仅将 1 做为 pivot 值。
|
||||
这个题目我们使用 Arrays.sort() 解决,哈哈,但是那样太无聊啦,题目含义就是让我们将所有的 0 放在前面,2 放在后面,1 放在中间,是不是和我们上面说的荷兰国旗问题一样。我们仅仅将 1 做为 pivot 值。
|
||||
|
||||
下面我们直接看代码吧,和三向切分基本一致。
|
||||
|
||||
@@ -48,7 +48,7 @@ class Solution {
|
||||
//这里和三向切分不完全一致
|
||||
int i = left;
|
||||
int right = len-1;
|
||||
|
||||
|
||||
while (i <= right) {
|
||||
if (nums[i] == 2) {
|
||||
swap(nums,i,right--);
|
||||
@@ -57,7 +57,7 @@ class Solution {
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void swap (int[] nums, int i, int j) {
|
||||
int temp = nums[i];
|
||||
@@ -72,7 +72,7 @@ Python3 Code:
|
||||
```python
|
||||
from typing import List
|
||||
class Solution:
|
||||
def sortColors(self, nums: List[int]):
|
||||
def sortColors(self, nums: List[int]):
|
||||
leng = len(nums)
|
||||
left = 0
|
||||
# 这里和三向切分不完全一致
|
||||
@@ -89,7 +89,7 @@ class Solution:
|
||||
else:
|
||||
i += 1
|
||||
return nums
|
||||
|
||||
|
||||
def swap(self, nums: List[int], i: int, j: int):
|
||||
temp = nums[i]
|
||||
nums[i] = nums[j]
|
||||
@@ -112,7 +112,7 @@ public:
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
@@ -153,7 +153,7 @@ class Solution {
|
||||
|
||||
例如:[0,0,0,1,1,1,2,2,2]
|
||||
|
||||
此时我们完全符合情况,不需要交换元素,但是按照我们上面的代码,0,2 的每个元素会和自己进行交换,所以这里我们可以根据 i 和 left 的值是否相等来决定是否需要交换,大家可以自己写一下。
|
||||
此时我们完全符合情况,不需要交换元素,但是按照我们上面的代码,0,2 的每个元素会和自己进行交换,所以这里我们可以根据 i 和 left 的值是否相等来决定是否需要交换,大家可以自己写一下。
|
||||
|
||||
下面我们看一下另外一种写法
|
||||
|
||||
@@ -177,7 +177,7 @@ class Solution {
|
||||
int len = nums.length;
|
||||
int right = len - 1;
|
||||
for (int i = 0; i <= right; ++i) {
|
||||
if (nums[i] == 0) {
|
||||
if (nums[i] == 0) {
|
||||
swap(nums,i,left);
|
||||
left++;
|
||||
}
|
||||
@@ -190,7 +190,7 @@ class Solution {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
public void swap (int[] nums,int i, int j) {
|
||||
int temp = nums[i];
|
||||
@@ -220,7 +220,7 @@ class Solution:
|
||||
# 如果不等于 1 则需要继续判断,所以不移动 i 指针,i--
|
||||
if nums[i] != 1:
|
||||
i -= 1
|
||||
i += 1
|
||||
i += 1
|
||||
return nums
|
||||
|
||||
def swap(self, nums: List[int], i: int, j: int):
|
||||
@@ -238,7 +238,7 @@ public:
|
||||
int left = 0, len = nums.size();
|
||||
int right = len - 1;
|
||||
for (int i = 0; i <= right; ++i) {
|
||||
if (nums[i] == 0) {
|
||||
if (nums[i] == 0) {
|
||||
swap(nums[i],nums[left++]);
|
||||
}
|
||||
if (nums[i] == 2) {
|
||||
@@ -265,7 +265,7 @@ class Solution {
|
||||
//nums.swapAt(i, left) 直接调用系统方法
|
||||
self.swap(&nums, i, left) // 保持风格统一走自定义交换
|
||||
left += 1
|
||||
}
|
||||
}
|
||||
if nums[i] == 2 {
|
||||
//nums.swapAt(i, right) 直接调用系统方法
|
||||
self.swap(&nums, i, right) // 保持风格统一走自定义交换
|
||||
@@ -285,4 +285,4 @@ class Solution {
|
||||
nums[j] = temp
|
||||
}
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
#### [剑指 Offer 03. 数组中重复的数字](https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/)
|
||||
|
||||
@@ -10,14 +10,13 @@
|
||||
|
||||
找出数组中重复的数字。
|
||||
|
||||
|
||||
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
|
||||
在一个长度为 n 的数组 nums 里的所有数字都在 0 ~ n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
|
||||
|
||||
示例 1:
|
||||
|
||||
输入:
|
||||
[2, 3, 1, 0, 2, 5, 3]
|
||||
输出:2 或 3
|
||||
输出:2 或 3
|
||||
|
||||
#### **HashSet**
|
||||
|
||||
@@ -87,8 +86,6 @@ class Solution {
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
**题目代码**
|
||||
|
||||
Java Code:
|
||||
@@ -176,4 +173,4 @@ class Solution {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[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>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <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/)
|
||||
|
||||
@@ -14,12 +14,10 @@
|
||||
|
||||
示例:
|
||||
|
||||
> 输入:s = 7, nums = [2,3,1,2,4,3]
|
||||
> 输入:s = 7, nums = [2,3,1,2,4,3]
|
||||
> 输出:2
|
||||
> 解释:子数组 [4,3] 是该条件下的长度最小的子数组。
|
||||
|
||||
|
||||
|
||||
#### 题目解析
|
||||
|
||||
滑动窗口:**就是通过不断调节子数组的起始位置和终止位置,进而得到我们想要的结果**,滑动窗口也是双指针的一种。
|
||||
@@ -28,14 +26,10 @@
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
好啦,该题的解题思路我们已经了解啦,下面我们看一下,代码的运行过程吧。
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
#### 题目代码
|
||||
|
||||
Java Code:
|
||||
@@ -55,7 +49,7 @@ class Solution {
|
||||
sum -= nums[i];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return windowlen == Integer.MAX_VALUE ? 0 : windowlen;
|
||||
|
||||
}
|
||||
@@ -84,7 +78,7 @@ public:
|
||||
|
||||
Python3 Code:
|
||||
|
||||
```python
|
||||
```python
|
||||
from typing import List
|
||||
import sys
|
||||
class Solution:
|
||||
@@ -99,7 +93,7 @@ class Solution:
|
||||
windowlen = min(windowlen, j - i + 1)
|
||||
sum -= nums[i]
|
||||
i += 1
|
||||
|
||||
|
||||
if windowlen == sys.maxsize:
|
||||
return 0
|
||||
else:
|
||||
@@ -124,4 +118,4 @@ class Solution {
|
||||
return windowlen == Int.max ? 0 : windowlen
|
||||
}
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user