数组part02
- 977有序数组的平方
- 209.长度最小的子数组
- 59.螺旋矩阵II
977.有序数组的平方
给你一个按 非递减顺序 排序的整数数组
nums
,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。示例 1:
输入:nums = [-4,-1,0,3,10] 输出:[0,1,9,16,100] 解释:平方后,数组变为 [16,1,0,9,100] 排序后,数组变为 [0,1,9,16,100]示例 2:
输入:nums = [-7,-3,2,3,11] 输出:[4,9,9,49,121]
平方后直接排序
python:(可以直接使用 a.sort() 函数)
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
for i in range(len(nums)):
nums[i] *= nums[i]
nums.sort()
return nums
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
return sorted(x*x for x in nums)
sort 与 sorted 区别:
sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。
list 的 sort 方法返回的是对已经存在的列表进行操作,无返回值
内建函数 sorted 方法返回的是一个新的 list
之前没有用过sorted这个函数,学习到了!
C++:
sort排序
时间复杂度:n*lg(n)
形式
sort(a.begin(),a.end())默认升序
sort(a.begin(),a.end(),cmp)根据cmp决定
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
for(int i=0; i <nums.size(); i++){
nums[i] *= nums[i];
}
sort(nums.begin(),nums.end());
return nums;
}
};
双指针法
python:
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
result = [float('inf')] * len(nums)
k = len(nums)-1
left = 0
right = len(nums)-1
while left<=right:
if nums[left]**2 > nums[right]**2:
result[k] = nums[left]**2
left = left + 1
else:
result[k] = nums[right]**2
right = right - 1
k = k - 1
return result
注意学习python中这种规定长度的列表创建的方法
C++:
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
int k = nums.size() - 1;
int j = nums.size() - 1;
int i = 0;
vector<int> result(nums.size(), 0);
while(i<=j){
if (nums[i] * nums[i] < nums[j] * nums[j]) {
result[k] = nums[j] * nums[j];
j--;
}
else {
result[k] = nums[i] * nums[i];
i++;
}
k--;
}
return result;
}
};
注意学习C++中这种规定长度的列表创建的方法
209.长度最小的子数组
给定一个含有
n
个正整数的数组和一个正整数target
。找出该数组中满足其总和大于等于
target
的长度最小的 连续子数组[numsl, numsl+1, ..., numsr-1, numsr]
,并返回其长度。如果不存在符合条件的子数组,返回0
。示例 1:
输入:target = 7, nums = [2,3,1,2,4,3] 输出:2 解释:子数组[4,3]
是该条件下的长度最小的子数组。示例 2:
输入:target = 4, nums = [1,4,4] 输出:1示例 3:
输入:target = 11, nums = [1,1,1,1,1,1,1,1] 输出:0
滑动窗口
所谓滑动窗口,就是不断的调节子序列的起始位置和终止位置,从而得出我们要想的结果。
实现滑动窗口,主要确定如下三点:
- 窗口内是什么?
- 如何移动窗口的起始位置?
- 如何移动窗口的结束位置?
本题中,窗口就是满足其和 ≥ s 的长度最小的连续子数组。
窗口的起始位置如何移动:如果当前窗口的值大于s了,窗口就要向前移动了。
窗口的结束位置如何移动:窗口的结束位置就是遍历数组的指针,也就是for循环里的索引。
python:
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
left, right = 0, 0
sum = 0
min_len = float('inf')
while right < len(nums):
sum = sum + nums[right]
while sum >= target:
min_len = min(min_len, right-left+1)
sum = sum - nums[left]
left = left+1
right = right +1
return min_len if min_len != float('inf') else 0
C++:
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int left = 0;
int right = 0;
int sum = 0;
int min_num = INT_MAX;
while(right<nums.size()){
sum += nums[right];
while(sum>=target){
min_num = min(min_num,right-left+1);
sum -= nums[left];
left += 1;
}
right += 1;
}
return min_num == INT_MAX ? 0 : min_num;
}
};
注意内循环里处理的顺序,先判断最小,再处理sum,最后移动起始位置
python,C++中正无穷定义有区别,return语句的处理上也有所不同。
59.螺旋矩阵II
给你一个正整数
n
,生成一个包含1
到n2
所有元素,且元素按顺时针顺序螺旋排列的n x n
正方形矩阵matrix
。示例 1:
输入:n = 3 输出:[[1,2,3],[8,9,4],[7,6,5]]示例 2:
输入:n = 1 输出:[[1]]提示:
1 <= n <= 20
循环不变量
本题都遵循左闭右开是原则
n为奇数时,最中间需要单独赋值
正确处理边界条件!
python:
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
start_x = start_y = 0
nums = [[0] * n for _ in range(n)]
count = 1
offset = 1
for round in range(n//2):
for i in range(start_y, n-offset):
nums[start_x][i]=count
count +=1
for i in range(start_x, n-offset):
nums[i][n-offset]=count
count +=1
for i in range(n-offset, start_y,-1):
nums[n-offset][i]=count
count +=1
for i in range(n-offset, start_x,-1):
nums[i][start_y]=count
count +=1
start_x+=1
start_y+=1
offset +=1
if n % 2 != 0:
nums[start_x][start_y]=count
return nums
for _ in range(n)
for _in range(n)
仅将循环运行n次,等效于for i in range(n)
,只不过_
在后面不会用到,只是占位符,这里的_
可以替换成任何符合规定的字符串。
C++:
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
int i=0;
int start_x = 0;
int start_y = 0;
int count = 1;
int offset = 1;
int round = n / 2;
vector<vector<int>> res(n, vector<int>(n, 0)); // 使用vector定义一个二维数组
while(round){
for(i=start_y;i<n-offset;i++){
res[start_x][i]=count;
count+=1;
}
for(i=start_x;i<n-offset;i++){
res[i][n-offset]=count;
count+=1;
}
for(i=n-offset;i>start_x;i--){
res[n-offset][i]=count;
count+=1;
}
for(i=n-offset;i>start_y;i--){
res[i][start_y]=count;
count+=1;
}
start_x += 1;
start_y += 1;
offset += 1;
round -= 1;
}
if(n%2!=0){res[start_x][start_y]=count;}
return res;
}
};