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