977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II
977.有序数组的平方
题目链接 :
代码 :
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.长度最小的子数组
题目链接 :
代码 :
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++;