977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II
977.有序数组的平方
题目链接 : 977. 有序数组的平方 - 力扣(LeetCode)
代码 :
class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
//int length = end(nums) - begin(nums);
int length = nums.size();
int ptr_Left;
int ptr_Right;
vector<int> array_Receive(length);
int ptr_ArrayReceive ;
ptr_Left = 0;
ptr_Right = length - 1;
ptr_ArrayReceive = length - 1 ; // 平方 和 数组 从 最大值 侧 开始 放
while(ptr_Left <= ptr_Right)
{
// 注意 这里 需要 比较 的 是 “绝对值”
if(abs(nums[ptr_Left])>= abs(nums[ptr_Right]))
{
array_Receive[ptr_ArrayReceive] = ( nums[ptr_Left] * nums[ptr_Left] );
ptr_ArrayReceive--;
ptr_Left++;
}
else
{
array_Receive[ptr_ArrayReceive] = ( nums[ptr_Right] * nums[ptr_Right] );
ptr_ArrayReceive--;
ptr_Right--;
}
}
return array_Receive;
}
};
209.长度最小的子数组
题目链接 : 209. 长度最小的子数组 - 力扣(LeetCode)
代码 :
class Solution {
public:
int minSubArrayLen(int target, vector<int>& nums) {
int length = nums.size();
int ptr_Front ;
int ptr_Back ;
int countLength;
int MinLength;
int sum_Temp = 0;
int sum_Next_Temp;
ptr_Front = 0;
ptr_Back = 0;
countLength = 0;
while( ptr_Front < length && sum_Temp < target )
{
sum_Temp += nums[ptr_Front];
ptr_Front++;
countLength++;
}
// 边界 考虑
if(sum_Temp < target )
{
return 0;
}
MinLength = countLength;
// 在 这 道 题 中 首次 处理 跟随 的 缩短 长度 的 处理
//////////////////////////////////////
sum_Next_Temp = sum_Temp;
// 打印 测试
do
{
// 注意 边界 考虑
sum_Temp = sum_Next_Temp;
sum_Next_Temp = sum_Temp - nums[ptr_Back] ;
if(sum_Next_Temp >= target)
{
ptr_Back++;
countLength--;
if(countLength < MinLength)
{
MinLength = countLength;
}
}
}while(sum_Next_Temp >= target );
//////////////////////////////////////
while(ptr_Front < length )
{
// 打印 测试
// “ 向 右 添加 一格 ”
// “ 发展 ”
//“类 回溯 法”
sum_Temp += nums[ptr_Front];
ptr_Front++;
countLength++;
//从 左侧 尝试 缩短 长度
sum_Next_Temp = sum_Temp;
// 打印 测试
do
{
// 注意 边界 考虑
sum_Temp = sum_Next_Temp;
sum_Next_Temp = sum_Temp - nums[ptr_Back] ;
if(sum_Next_Temp >= target)
{
ptr_Back++;
countLength--;
if(countLength < MinLength)
{
MinLength = countLength;
}
}
}while(sum_Next_Temp >= target );
// 测试 时 对 溢出 情况 的 分析 ,/ “ 相关 分析 ”
// “ 可靠性 处理 ” , 健壮 性 处理
}
return MinLength;
}
};
59.螺旋矩阵II
题目链接 : 59. 螺旋矩阵 II - 力扣(LeetCode)
代码 :
class Solution {标签:977,59,Temp,nums,int,sum,++,数组,ptr From: https://www.cnblogs.com/brinisky/p/18262876
public:
vector<vector<int>> generateMatrix(int n) {
vector<vector<int>> array_TwoDimention_Receive( n , vector<int>(n) );
int worker_Outer_Thickness = n ;
int padding_Thickness = 0;
int i;
int j;
// 变量 初始化
int num_Accumulate ;
i = 0;
j = 0;
num_Accumulate = 1;
if(n == 1)
{
array_TwoDimention_Receive[0][0] = 1 ;
return array_TwoDimention_Receive;
}
while(worker_Outer_Thickness > 1)
{
for(; j < (n - padding_Thickness) - 1 ; j++ )
{
array_TwoDimention_Receive[i][j] = num_Accumulate ;
num_Accumulate ++ ;
}
for(; i < (n - padding_Thickness) - 1 ; i++ )
{
array_TwoDimention_Receive[i][j] = num_Accumulate ;
num_Accumulate ++ ;
}
for(; j > padding_Thickness ; j-- )
{
array_TwoDimention_Receive[i][j] = num_Accumulate ;
num_Accumulate ++ ;
}
for(; i > padding_Thickness ; i-- )
{
array_TwoDimention_Receive[i][j] = num_Accumulate ;
num_Accumulate ++ ;
}
// “ 变 向 ” / “ 变 向 操作 ”
i++;
j++;
worker_Outer_Thickness -= 2 ;
padding_Thickness += 1 ;
}
if(worker_Outer_Thickness == 1)
{
array_TwoDimention_Receive[i][j] = num_Accumulate ;
num_Accumulate ++ ;
}
return array_TwoDimention_Receive ;
}
};