977 有序数组的平方
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
vector<int> result(nums.size(),0);
int k = nums.size()-1;
for (int i=0,j=k;i<=j;){
if(pow(nums[i],2) > pow(nums[j],2)){
result[k]=pow(nums[i],2);
k--;
i++;
}
else{
result[k]=pow(nums[j],2);
k--;
j--;
}
}
return result;
}
};
209 长度最小的子数组
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int i=0;
int result=nums.size()+1;
int sum=0;
for(int j=0;j<nums.size();j++){
sum=sum+nums[j];
while(sum>=target){
int subl=j-i+1;
result=min(result,subl);
sum=sum-nums[i];
i++;
}
}
return result==nums.size()+1?0:result;
}
};
59 螺旋矩阵
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> nums(n, vector<int>(n, 0));
int startx=0;
int starty=0;
int offset=1;
int count=1;
int loop=n/2;
int mid=n/2;
int i,j;
while(loop--){
i=startx;
j=starty;
for (j=starty;j<n-offset;j++){
nums[startx][j]=count;
count++;
}
for (i=startx;i<n-offset;i++){
nums[i][j]=count;
count++;
}
for (;j>starty;j--){
nums[i][j]=count;
count++;
}
for (;i>startx;i--){
nums[i][j]=count;
count++;
}
startx++;
starty++;
offset++;
}
if(n%2){
nums[mid][mid]=count;
}
return nums;
}
};
标签:977,count,nums,int,随想录,++,vector,result,数组 From: https://www.cnblogs.com/ryh0530/p/17013669.html