代码随想录算法训练营第2天|977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II

977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II

977. Squares of a Sorted Array

Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.

Example:
Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].

Sol1: Two Pointers

class Solution(object):
    def sortedSquares(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        p1,p2=0,len(nums)-1
        res=[]
        while p1<=p2:
            p1_s,p2_s = nums[p1]**2, nums[p2]**2
            if p1_s<p2_s:
                res.insert(0,p2_s)
                p2-=1
            else:
                res.insert(0,p1_s)
                p1+=1
        return res

  • Note: the larger one pointer move, each loop move one pointer only; “res.insert(0,ele)”

  • Complexity. Time O(n); Space O(n)

Sol2: Built-in Sort + List Comprehension

class Solution_1(object):
    def sortedSquares(self, nums):
        return sorted([x*x for x in nums])
  • Note: “sorted”

  • Complexity. Time O(nlogn); Space O(n)

================================================================

209. Minimum Size Subarray Sum

Given an array of positive integers nums and a positive integer target, return the minimal length of a
subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.

Example 1:
Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.

Sol: Two Pointers - Sliding Window

class Solution(object):
    def minSubArrayLen(self, target, nums):
        """
        :type target: int
        :type nums: List[int]
        :rtype: int
        """
        l,r,cur_sum = 0,0,0
        min_length = float('inf')
        while r<len(nums):
            cur_sum+=nums[r]
            while cur_sum>=target:
                cur_sub_len=r-l+1
                min_length = min(min_length, cur_sub_len)
                cur_sum-=nums[l]
                l+=1
            r+=1
        return 0 if min_length==float('inf') else min_length
  • Note: 外层先确定right pointer,内层left pointer;>sum 移动left pointer, <sum 移动right pointer
  • Time: O(n), each element only check twice: in window, out window; left pointer 一直在左移,没有重复计算
  • Space: O(1), only two pointers.

================================================================

59. Spiral Matrix II

Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

Example 1:
Input: n = 3
Output: [[1,2,3],[8,9,4],[7,6,5]]

class Solution(object):
    def generateMatrix(self, n):
        """
        :type n: int
        :rtype: List[List[int]]
        """
        count=1
        loop=n//2
        res = [[0]*n for _ in range(n)]

        for offset in range(loop):
            for i in range(offset,n-offset-1):
                res[offset][i] = count
                count+=1
            for i in range(offset,n-offset-1):
                res[i][n-offset-1] = count
                count+=1
            for i in range(n-offset-1,offset,-1):
                res[n-offset-1][i] = count
                count+=1
            for i in range(n-offset-1,offset,-1):
                res[i][offset]=count
                count+=1
        if n%2==1:
            res[loop][loop]=count
        return res
           
  • Note: no specific algorithm, simply fill in as asked.
  • “offset”, range(, ,-1), count starts from 1

================================================================

总结

数组

  • Binary search
  • Two pointers
  • Sliding window
  • Action simulation
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值