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

977. 有序数组的平方:


代码思路

基本思路就是平方的同时排序,采用归并排序的并操作来排序,因为已经“归好了”,就是拆成两个数组,一个是负数的有序数组,一个是正数的有序数组,所以时间复杂度不是以往归并排序的O(nlogn),而是O(n)。

class Solution:
    def sortedSquares(self, nums: List[int]) -> List[int]:
    	# 这个for循环是找出切分点
        for i in range(len(nums)):
            if nums[i] >= 0:
                if i > 0:
                    mid = i
                    break
                if i == 0:
                    return [j*j for j in nums]
            elif i == len(nums) - 1:
                return [j*j for j in nums][::-1]
        # 并操作开始           
        left = nums[:mid][::-1]
        right = nums[mid:]
        length_left = len(left)
        length_right= len(right)
        left_index = 0
        right_index = 0
        ans = []
        while right_index <= length_right - 1 and left_index <= length_left - 1:
            if left[left_index]*left[left_index] > right[right_index]*right[right_index]:                
                ans.append(right[right_index]*right[right_index])
                right_index += 1

            else:                
                ans.append(left[left_index]*left[left_index])
                left_index += 1
		# 把剩余没添加进最终数组的添加进去
        if right_index <= length_right - 1:
            return ans + [i*i for i in right[right_index:]]
        else:
            return ans + [i*i for i in left[left_index:]]

209. 长度最小的子数组


代码思路

快慢指针:当当前截取的数组和小于target,扩大数组截取范围,当当前截取的数组大于等于target时,先记录当前数组长度,然后尝试缩小数组看看有没有更短的。

class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        slow = 0
        fast = 0
        length = len(nums)
        min_length = length + 1
        dsum = nums[slow]
        while fast <= length - 1:
            if dsum < target:
                fast += 1
                if fast <= length - 1:
                    dsum += nums[fast]
            else:
                if min_length > (fast - slow + 1):
                    min_length = fast - slow + 1
                dsum -= nums[slow]
                slow += 1
        if min_length == length + 1:
            return 0
        else:
            return min_length

59. 螺旋矩阵II


代码思路

设置好上下左右边界,然后从左往右遍历其实就是left到right,从上到下就是top到bottom。当遍历完有一行或一列数据,其中一个边界会收窄。

class Solution:
    def generateMatrix(self, n: int) -> List[List[int]]:
        matrix = [[-1 for i in range(n)] for j in range(n)]
        # 设置边界条件
        left = 0
        right = n - 1   # 2
        top = 0
        bottom = n - 1
        temp = 1
        while temp <= n * n:
            for i in range(left, right+1):
                matrix[top][i] = temp 
                temp += 1
            top += 1
            for i in range(top, bottom+1):
                matrix[i][right] = temp
                temp += 1
            right -= 1
            for i in range(right, left - 1, -1):
                matrix[bottom][i] = temp
                temp += 1
            bottom -= 1
            for i in range(bottom, top - 1, -1):
                matrix[i][left] = temp
                temp += 1
            left += 1
        return matrix

你可能感兴趣的:(算法,矩阵,排序算法)