![](https://img-blog.csdnimg.cn/20201014180756780.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数组
Vaccy Zhu
自律是解决人生问题最主要的工具,也是解决人生痛苦最重要的方法!
展开
-
矩阵置零-数组73-c++
【代码】矩阵置零-数组73-c++原创 2024-04-28 20:21:16 · 103 阅读 · 0 评论 -
盛水最多的容器-双指针11-python&c++
没看答案,用双指针遍历height数组的左右两端,对于相对短的一边就移动(木桶效应),并不断更新答案。原创 2022-06-24 20:46:57 · 178 阅读 · 0 评论 -
找到最高海拔-数组1732-c++
前缀和easy难度题。原创 2024-03-31 11:52:32 · 57 阅读 · 0 评论 -
删掉一个元素以后全为 1 的最长子数组-数组1493-c++
动态调整滑动窗口的size。原创 2024-03-31 10:43:26 · 61 阅读 · 0 评论 -
最大连续1的个数 III-数组1004-c++
动态调整滑动窗口的size。原创 2024-03-31 10:32:09 · 68 阅读 · 0 评论 -
子数组最大平均数I-数组643-c++
滑动窗口easy难度题。原创 2024-03-30 22:22:51 · 76 阅读 · 0 评论 -
K和数对的最大数目-数组&双指针1679-c++
原则为较小的数优先和较大的数匹配,所以先排序。原创 2024-03-27 20:39:35 · 93 阅读 · 0 评论 -
递增的三元子序列-数组334-c++
利用栈的暴力解法,O(n^2)的时间复杂度,但是leetcode报错超时。原创 2024-03-27 00:39:15 · 201 阅读 · 0 评论 -
种花问题-数组605-c++
先在前后各插入0,这样可以保证flowerbed的长度大于等于3,然后从第2个到倒数第2个元素循环判断最近邻3元组是否都是0,是则在中间的0上插花。原创 2024-02-29 13:07:30 · 333 阅读 · 0 评论 -
拥有最多糖果的孩子-数组1431-c++
【代码】拥有最多糖果的孩子-数组1431-c++原创 2024-02-28 17:56:23 · 329 阅读 · 0 评论 -
压缩字符串-数组443-c++
【代码】压缩字符串-数组443-c++原创 2024-02-28 17:48:22 · 379 阅读 · 0 评论 -
除自身以外数组的乘积-数组238-c++
我们不必将所有数字的乘积除以给定索引处的数字得到相应的答案,而是利用索引左侧所有数字的乘积和右侧所有数字的乘积(即前缀与后缀)相乘得到答案。对于给定索引 i,我们将使用它左边所有数字的乘积乘以右边所有数字的乘积。下面让我们更加具体的描述这个算法。原创 2024-02-28 16:21:04 · 360 阅读 · 0 评论 -
多数元素-数组169-python
没看答案,哈希表法。时间复杂度O(n),空间复杂度O(n)。摩尔投票法,时间复杂度O(n),空间复杂度O(1):原创 2022-06-30 22:43:39 · 287 阅读 · 0 评论 -
按奇偶排序数组II-双指针922-python
没看答案,找到不符合要求位置的奇数和偶数进行调换即可。class Solution: def sortArrayByParityII(self, nums: List[int]) -> List[int]: n = len(nums) i, j = 0, 1 while True: while i < n and nums[i] % 2 == 0: i += 2原创 2022-05-04 14:35:59 · 105 阅读 · 0 评论 -
寻找数组的中心下标-数组724-python
没看答案,按照题意,遍历每一个位置,并不断比较left和right的值是否相等即可。class Solution: def pivotIndex(self, nums: List[int]) -> int: n = len(nums) right = sum(nums) left = 0 for i in range(n): right -= nums[i] if i >=原创 2022-05-04 14:21:32 · 597 阅读 · 0 评论 -
有效的山脉数组-数组941-python
没看答案。class Solution: def validMountainArray(self, arr: List[int]) -> bool: n = len(arr) if n < 3: return False i = 1 # 爬山 while i < n and arr[i] > arr[i-1]: i += 1 # 没有爬原创 2022-05-04 14:19:48 · 281 阅读 · 0 评论 -
有效的山脉数组-数组941-python
没看答案。class Solution: def validMountainArray(self, arr: List[int]) -> bool: n = len(arr) if n < 3: return False i = 1 # 爬山 while i < n and arr[i] > arr[i-1]: i += 1 # 没有爬原创 2022-05-03 14:49:17 · 257 阅读 · 0 评论 -
有多少小于当前数字的数字-排序1365-python
没看答案,暴力解法时间复杂度O(N^2),排序时间复杂度O(NlogN),所以采用计数排序,时间复杂度O(N)。class Solution: def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: bucket = [0] * 101 n = len(nums) res = [0] * n for num in nums:原创 2022-05-03 14:19:04 · 284 阅读 · 0 评论 -
水果成篮-数组904-python
没看答案,滑动窗口问题。import sysclass Solution: def totalFruit(self, fruits: List[int]) -> int: length = len(fruits) window = {} left, right = 0, 0 res = -sys.maxsize while right < length: num = fruit原创 2022-02-14 11:35:05 · 371 阅读 · 0 评论 -
有序数组的平方-数组977-python
没看答案,负数的平方也是整数,所以先把输入数组中的所有负数变成正数,然后把所有元素的平方加入到新数组中并排序输出。class Solution: def sortedSquares(self, nums: List[int]) -> List[int]: res = [] for i in nums: if i < 0: i = -i res.append(原创 2022-02-11 11:08:55 · 549 阅读 · 0 评论 -
移动零-数组283-python&c++
没看答案class Solution: def moveZeroes(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ if not nums: return L = len(nums) i, j = 0, 1原创 2022-02-09 14:10:06 · 61 阅读 · 0 评论 -
寻找旋转排序数组中的最小值II-二分查找154-python
pythonclass Solution: def findMin(self, nums: List[int]): left, right = 0, len(nums)-1 while left < right: mid = int(left + (right-left)/2) if nums[mid] > nums[right]: left = mid + 1原创 2021-09-09 15:30:47 · 76 阅读 · 0 评论 -
寻找旋转排序数组中的最小值-二分查找153-python
pythonclass Solution: def findMin(self, nums: List[int]): left, right = 0, len(nums)-1 while left < right: mid = int(left + (right-left)/2) if nums[mid] <= nums[right]: right = mid原创 2021-09-09 15:06:43 · 83 阅读 · 0 评论 -
和等于 k 的最长子数组长度-数组325-python
pythonclass Solution: def maxSubArrayLen(self, nums: List[int], k: int): n = len(nums) needs = {0:[0]} res = 0 preNum = 0 for i in range(1, n+1): preNum += nums[i-1] target = pre原创 2021-09-08 20:59:44 · 213 阅读 · 0 评论 -
和为 K 的子数组-数组560-python&c++
前缀和算法: 可以O(1)时间下得到子数组的和。pythonclass Solution: def subarraySum(self, nums: List[int], k: int): n = len(nums) preNum = [0] * (n+1) needs = {0:1} count = 0 for i in range(n): preNum[i+1] = preNum[i]原创 2021-09-08 20:07:41 · 263 阅读 · 0 评论 -
三个数的最大乘积-数组628-python
pythonclass Solution: def maximumProduct(self, nums: List[int]): nums = sorted(nums) tmp = nums[-3:] if 0 in tmp: if tmp[0] == 0 or tmp[1] == 0: res = nums[0]*nums[1]*nums[-1] else:原创 2021-09-07 17:18:58 · 129 阅读 · 0 评论 -
在排序数组中查找数字I-二分查找(剑指Offer 53-I)-python
pythonclass Solution: def search(self, nums: List[int], target: int): left, right = 0, len(nums)-1 res = 0 while left <= right: mid = int(left + (right-left)/2) if nums[mid] == target:原创 2021-09-05 15:09:26 · 65 阅读 · 0 评论 -
数组中的逆序对-数组(剑指offer51)-python
pythonclass Solution: def mergeSort(self, nums, tmp, l, r): if l >= r: return 0 mid = (l + r) // 2 inv_count = self.mergeSort(nums, tmp, l, mid) + self.mergeSort(nums, tmp, mid + 1, r) i, j, pos = l, mid原创 2021-08-27 18:43:28 · 63 阅读 · 0 评论 -
按奇偶排序数组-数组905-python
python时间复杂度和空间复杂度都是O(n)class Solution: def reOrderArray(self , array ): # write code here odd = [] even = [] for num in array: if num % 2 == 1: odd.append(num) else:原创 2021-08-27 16:33:44 · 72 阅读 · 0 评论 -
搜索二维矩阵II-数组240-python&c++
pythonclass Solution: def searchMatrix(self, matrix: List[List[int]], target: int): m = len(matrix) n = len(matrix[0]) raw, col = m-1, 0 while raw >= 0 and col <= n-1: if matrix[raw][col] ==原创 2021-08-26 21:59:22 · 75 阅读 · 0 评论 -
旋转数组-数组189-python&c++
python时间复杂度和空间复杂度都是O(n)from copy import deepcopyclass Solution: def rotate(self, nums: List[int], k: int) -> None: """ Do not return anything, modify nums in-place instead. """ n = len(nums) res = deepcopy原创 2021-08-26 21:10:55 · 64 阅读 · 0 评论 -
旋转图像-数组48-python&c++
python将原矩阵先水平翻转,再沿主对角线(左上到右下)翻转,就是顺时针翻转90度的效果了。class Solution: def rotate(self, matrix: List[List[int]]) -> None: """ Do not return anything, modify matrix in-place instead. """ n = len(matrix) # 水平线翻转原创 2021-08-18 23:42:39 · 97 阅读 · 0 评论 -
前k个高频单词-数组692-python
python哈希表+排序import collectionsclass Solution: def topKFrequent(self, words: List[str], k: int): counter = collections.Counter(words) res = [] counter = sorted(counter.items(), key=lambda kv:(-kv[1], kv[0])) for i in原创 2021-08-16 14:15:44 · 116 阅读 · 0 评论 -
搜索二维矩阵-数组74-python&c++
二分查找pythonclass Solution: def searchMatrix(self, matrix: List[List[int]], target: int): m = len(matrix) n = len(matrix[0]) for line in matrix: if target < line[0] or target > line[-1]: contin原创 2021-08-15 22:29:47 · 59 阅读 · 0 评论 -
数组中未出现的最小正整数-数组NC30-python
python## return the min number# @param arr int整型一维数组 the array# @return int整型#import sysclass Solution: def minNumberdisappered(self , arr ): # write code here res = 1 num = sys.maxsize for i in arr:原创 2021-08-15 22:10:16 · 487 阅读 · 0 评论 -
最长连续序列-数组128-python
并查集:在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并。pythonclass Solution: def longestConsecutive(self, arr: List[int]): n = len(arr) res = 0 dic = {} for data in arr: tmp = 0原创 2021-08-14 23:19:52 · 143 阅读 · 0 评论 -
寻找两个正序数组的中位数-二分查找4-python&c++
python一. 无脑傻瓜版本:1.把两个数组合并成一个数组并排序;2.找到新数组的中位数。class Solution: def findMedianSortedArrays(self, nums1, nums2): ls = list() for i in nums1: ls.append(i) for j in nums2: ls.append(j) ls.原创 2021-02-07 18:09:57 · 217 阅读 · 1 评论 -
三数之和-双指针15-python&C++
python1.a+b+c=0,自由度为2,所以可以不用三循环,而使用双循环;2.先给数组从小到大排序,即保证a≤b≤c,从而避免答案出现[a,b,c],[b,c,a]…等重复数组;3.固定a后,寻找b,c的过程可以利用双指针,b和c分别从前往后和从后往前枚举,从而避免重复查找。class Solution: def threeSum(self, nums): n = len(nums) nums.sort() # 为避免重复输出,从小到大对输入数组排序原创 2021-02-05 22:41:48 · 145 阅读 · 1 评论 -
四数之和-数组18-python
不管是几数之和,都能变成两数之和问题,可以用递归思想pythonclass Solution: def fourSum(self, nums: List[int], target: int): nums = sorted(nums) def N_Sum(nums, n, start, target): length = len(nums) res = [] if n < 2 or l原创 2021-08-12 20:07:52 · 75 阅读 · 0 评论 -
区间列表的交集-数组986-python
区间问题画图可以理解,然后把所有情况列举出来判断即可。pythonclass Solution: def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]): m = len(firstList) n = len(secondList) i, j = 0, 0 left, right = 0, 0原创 2021-08-10 21:35:33 · 173 阅读 · 0 评论