首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >数组矩阵杂题

数组矩阵杂题

原创
作者头像
王脸小
修改于 2020-08-06 03:10:47
修改于 2020-08-06 03:10:47
72600
代码可运行
举报
文章被收录于专栏:王漂亮王漂亮
运行总次数:0
代码可运行

双指针

42. 接雨水

Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!

Example:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6

Solution

暴力法:n方,每个位置求左右最大值

以下方法O(3n),res[i] = min(left_max, right_max),一次性求好左边最大值和右边最大值

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution:
    def trap(self, height) -> int:
        if len(height) <= 2: return 0
        left_max, right_max = [height[0]] + [0]*(len(height)-1), [0]*(len(height)-1) + [height[-1]]
        
        res = 0
        for i in range(1, len(height)):
            left_max[i] = max(left_max[i-1], height[i])
            
        for i in reversed(range(len(height)-1)):
            right_max[i] = max(right_max[i+1], height[i])
            
        for i in range(1, len(height)-1):
            cur = min(left_max[i-1], right_max[i+1]) - height[i]
            res += cur if cur>0 else 0
            
        return res

Solution - 双指针

labuladong的讲解

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution(object):
    def trap(self, height):
        if not height: return 0
        left, right = 0, len(height)-1
        res = 0
        left_max = height[0]
        right_max = height[-1]
        
        while left <= right:
            left_max = max(left_max, height[left])
            right_max = max(right_max, height[right])  
            
            if left_max < right_max:
                res = res + left_max - height[left] 
                left += 1
            else:
                res = res + right_max - height[right] 
                right -= 1            
                
        return res

Follow up是如果不是从天而降的雨水,而是一桶x unit的水,从给定的index往下倒水,最后困住多少水。只讲思路

11. 盛最多水的容器

Container With Most Water

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: [1,8,6,2,5,4,8,3,7]
Output: 49

Solution

思路(并不是自己想的):Reference,左右双指针,l从前往后,r从后往前,每次移动l和r中较小的值,算当前面积

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution:
    def maxArea(self, height: List[int]) -> int:
        if not height: return 0
        
        res = 0
        l, r = 0, len(height)-1
        while l < r:
            res = max(res, (r-l)*min(height[l],height[r]))
            if height[l] <= height[r]:
                l += 1
            else:
                r -= 1
                
        return res

数组

239. 滑动窗口最大值 - H

Sliding Window Maximum

Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window.

Example:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
Output: [3,3,5,5,6,7] 
Explanation: 

Window position                Max
---------------               -----
[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

Note: You may assume k is always valid, 1 ≤ k ≤ input array's size for non-empty array.

Follow up: Could you solve it in linear time?

Solution - 暴力

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution:
    def maxSlidingWindow(self, nums, k: int):
        if not nums: return []
        res = []
        
        for end in range(k, len(nums)+1):
            start = end-k
            res += [max(nums[start:end])]
            
        return res

Solution - 单调队列

On时间,Ok空间

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
from collections import deque
class MonotonicQueue:
    def __init__(self):
        self.data = deque()
    
    def push(self, x):
        while self.data and self.data[-1] < x:
            self.data.pop()
        self.data.append(x)
            
    def max(self):
        return self.data[0]
    
    def pop(self, x):
        if self.data and self.data[0] == x:
            self.data.popleft()
    
class Solution(object):
    def maxSlidingWindow(self, nums, k):
        res =[]
        window = MonotonicQueue()
        
        for i in range(len(nums)):
            if i < k-1:
                window.push(nums[i])
            else:
                window.push(nums[i])
                res += [window.max()]
                window.pop(nums[i-k+1])
                
        return res

巨好的单调队列讲解 Python答案

41. 缺失的第一个正数

First Missing Positive

Given an unsorted integer array, find the smallest missing positive integer.

Example 1:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: [1,2,0]
Output: 3

Example 2:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: [3,4,-1,1]
Output: 2

Example 3:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: [7,8,9,11,12]
Output: 1

Note:

Your algorithm should run in O(n) time and uses constant extra space.

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution:
    def firstMissingPositive(self, nums) -> int:
        n = len(nums)
        for i in range(n):
            while 1 <= nums[i] <= n and nums[nums[i] - 1] != nums[i]:
                nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1]
        for i in range(n):
            if nums[i] != i + 1:
                return i + 1
        return n + 1

51.上一个排列

Given a list of integers, which denote a permutation.

Find the previous permutation in ascending order.

The list may contains duplicate integers.

Example 1:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input:[1]
Output:[1]

Example 2:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input:[1,3,2,3]
Output:[1,2,3,3]

Example 3:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input:[1,2,3,4]
Output:[4,3,2,1]

Solution

从后往前找顺序的第一个i,然后找i后面比i小的最大值j,swap(i,j), reverse[i+1:]

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution:
    def previousPermuation(self, nums):
        if len(nums) <= 1: return nums
        i = len(nums) - 1
        while i > 0 and nums[i] >= nums[i - 1]:
            i -= 1
            
        if i == 0: return nums[::-1]
        
        j = len(nums) - 1
        while nums[j] >= nums[i - 1]:
            j -= 1
        
        nums[i-1], nums[j] = nums[j], nums[i-1]
        
        return nums[:i] + list(reversed(nums[i:]))

重新写吧朋友, Reference

Follow up: 下一个排列

238. 除自身之外的乘积

Given an array nums of n integers where n > 1,  return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].

Example:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input:  [1,2,3,4]
Output: [24,12,8,6]

Note: Please solve it without division and in O(n).

Follow up: Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)

powcai

思路:遍历一遍算i位左边乘积,遍历一遍算i位右边乘积

矩阵

48. 旋转图像 - M

Rotate Image

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Note:

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Given input matrix = 
[
  [1,2,3],
  [4,5,6],
  [7,8,9]
],

rotate the input matrix in-place such that it becomes:
[
  [7,4,1],
  [8,5,2],
  [9,6,3]
]

Example 2:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Given input matrix =
[
  [ 5, 1, 9,11],
  [ 2, 4, 8,10],
  [13, 3, 6, 7],
  [15,14,12,16]
], 

rotate the input matrix in-place such that it becomes:
[
  [15,13, 2, 5],
  [14, 3, 4, 1],
  [12, 6, 8, 9],
  [16, 7,10,11]
]

Solution

powcai

找规律直接计算对应位置

54. 螺旋矩阵 - M

Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.

Example 1:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]

Solution

两种解法-powcai

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        if not matrix: return []
        top, bottom = 0, len(matrix) - 1
        left, right = 0, len(matrix[0]) - 1
        
        res = []
        while top <= bottom and left <= right:
            #从左到右
            for i in range(left, right+1):
                res.append(matrix[top][i])
            top += 1
            if top > bottom: break
            #从上到下
            for i in range(top, bottom+1):
                res.append(matrix[i][right])
            right -= 1
            if left > right: break
            #从右到左
            for i in range(right, left-1, -1):
                res.append(matrix[bottom][i])
            bottom -= 1
            #从下到上
            for i in range(bottom, top-1, -1):
                res.append(matrix[i][left])
            left += 1
            
        return res

思路:寻常思路,一直逆时针旋转

304. 2D区域和检索(不可变)

Range Sum Query 2D - Immutable

Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

Example:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Given matrix = [
  [3, 0, 1, 4, 2],
  [5, 6, 3, 2, 1],
  [1, 2, 0, 1, 5],
  [4, 1, 0, 1, 7],
  [1, 0, 3, 0, 5]
]

sumRegion(2, 1, 4, 3) -> 8
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12

Note: You may assume that row1 ≤ row2 and col1 ≤ col2.

powcai, init里面一次性求好ij位置左上方的sum

等于黄色的部分总和 - 两个橙色总和 + 红色部分 ( 因为我们发现当我们减去橙色部分, 红色部分多删除一次)

Math

204. 计算质数

Count Primes

Count the number of prime numbers less than a non-negative number, n.

Example:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

Solution

ladong, Onloglogn

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution(object):
    def countPrimes(self, n):
        if n<=2: return 0
        isPrime = [True]*(n)
        for i in range(2, int(n**0.5)+1):
            if isPrime[i]:
                # i 的倍数不可能是素数
                for j in range(2*i, n, j + i):
                    isPrime[j] = False
        
        cnt=0
        for p in isPrime:
            if p: cnt+=1
                
        return cnt-2

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
LeetCode 0042 - Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
Reck Zhang
2021/08/11
2450
算法:动态规划(DP)入门实践
入门 在知乎上看到徐凯强 Andy的答案后感觉入门了 实践 题目:仅包含0/1的矩阵,求其中最大的全1方阵(不能是矩形)的边长 题解:matrxi[100][100]表示0/1矩阵,dp[i][j]表示:以matrix[i][j]为右下角,边长最大为min(i,j)的,最大全1方阵的边长, if(matrix[i][j]==0) { dp[i][j]=0; } if(matrix[i][j]==1) { if(dp[i-1][j]==1&&dp[i][j-1]==1) { dp[i
keloli
2018/09/13
9270
重中之重的二分查找
There are two sorted arrays nums1 and nums2 of size m and n respectively.
王脸小
2020/07/28
5010
Data Structures and Algorithms Basics(013):Two Pointers
# 1,反转列表: def reverse(nums): n = len(nums) for i in range(len(nums) // 2): nums[i], nums[n-1-i] = nums[n-1-i], nums[i] print(nums) def reverse2(nums): i, j = 0, len(nums) - 1 while (i < j): nums[i], nums[j] = nums[j],
用户5473628
2019/08/08
2980
动态规划高频题
Given an unsorted array of integers, find the length of longest increasing subsequence.
王脸小
2019/10/18
6080
常见编程模式之双指针
双指针模式指使用两个一前一后的指针遍历数据结构,直到某个指针触发停止条件。该模式常用于在有序数组或链表中搜索元素对。使用双指针的好处在于和单指针相比,不用去连续遍历整个数组来找出答案,可以带来更好的时间或空间复杂度。
口仆
2020/08/14
2.1K0
回溯/贪心高频题
"有关递归的算法,都离不开“树”的遍历这一抽象模型。只不过对于不同的算法,在前(中)后序遍历的时候,所做的事不同而已。 "
王脸小
2019/10/28
1.4K0
打卡群刷题总结0710—— 螺旋矩阵
链接:https://leetcode-cn.com/problems/spiral-matrix
木又AI帮
2020/07/16
2620
Leetcode【26、80、962】
这道题是给一个排序好的数组,通过修改原数组,使得前 K 个元素都不同,返回 K,要求使用 O(1) 的空间。
echobingo
2019/07/15
6360
Leetcode 42题 接雨水(Trapping Rain Water)
https://leetcode-cn.com/problems/trapping-rain-water/
code随笔
2020/06/16
4660
LeetCode Weekly Contest 30解题思路
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/71023781
用户1147447
2019/05/26
4350
字符串基础题
总结:所有题目都已做,有些Easy没有做第二遍,有两道没有accept,请戳 link-en, link-cn
王脸小
2019/11/02
9840
每日两题 T15
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
合一大师
2020/07/20
3170
每日两题 T15
[Leetcode][python]Trapping Rain Water/接雨水
参考:http://www.cnblogs.com/zuoyuan/p/3781453.html
蛮三刀酱
2019/03/26
6800
算法刷题笔记01:Array
给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
Hsinyan
2022/08/30
4110
算法刷题笔记01:Array
[LeetCode]Array主题系列{1,11,15,16,18,26,27,31,33,34题}
1.内容介绍 开一篇文章记录在leetcode中array主题下面的题目和自己的思考以及优化过程,具体内容层次按照{题目,分析,初解,初解结果,优化解,优化解结果,反思}的格式来记录,供日后复习和反思。题目的顺序按照leetcode给出的题目顺序,有些题目在并不是按照题目本身序号顺序排列的,也不是严格按照难易程度来排列的。 因此,这篇文章并不具有很强的归类总结性,归类总结性知识将会在其他文章记录,本篇重点在记录解题过程中的思路,希望能对自己有所启发。 2.题目和解题过程 2.1 Container With
昊楠Hacking
2018/03/30
9500
Golang语言[6] 递增的三元子序列/笨阶乘/矩阵查找/直方图的水量 |Go主题月
给你一个整数数组 nums ,判断这个数组中是否存在长度为 3 的递增子序列。如果存在这样的三元组下标 (i, j, k) 且满足 i < j < k ,使得 nums[i] < nums[j] < nums[k] ,返回 true ;否则,返回 false 。
微芒不朽
2022/09/13
4160
【leetcode刷题】20T23-螺旋矩阵
https://leetcode-cn.com/problems/spiral-matrix/
木又AI帮
2020/02/26
3450
堆-高频题
Given a non-empty array of integers, return the k most frequent elements.
王脸小
2019/11/07
8590
经典接雨水【单调栈】【动态规划】
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
韩旭051
2021/04/14
8060
经典接雨水【单调栈】【动态规划】
相关推荐
LeetCode 0042 - Trapping Rain Water
更多 >
LV.1
这个人很懒,什么都没有留下~
交个朋友
加入腾讯云官网粉丝站
蹲全网底价单品 享第一手活动信息
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验