蓝桥杯上岸每日N题第一期(二)!!!
发布人:shili8
发布时间:2025-01-20 00:37
阅读次数:0
**蓝桥杯上岸每日N题第一期(二)****前言**
蓝桥杯是中国的一项非常著名的编程竞赛活动,由蓝桥网主办。它旨在培养学生的编程能力、逻辑思维和问题解决能力等方面。蓝桥杯上岸每日N题第一期(二)是本次竞赛的第二部分。
**第一题:**
**题目描述**
给定一个整数数组,要求找出其中最长的连续子序列,其元素之和为零。
**示例**
输入:[-1,0,1,2, -1, -4]
输出:3(因为 [-1,-1,2] 是最长的连续子序列,其元素之和为零)
**代码实现**
def longest_zero_sum_subarray(nums): """ Finds the longest continuous subarray with a sum of zero. Args: nums (list): A list of integers. Returns: int: The length of the longest continuous subarray with a sum of zero. """ max_length =0 current_sum =0 # Initialize two pointers, one at the start and one at the end of the array left =0 right =0 while right < len(nums): # Add the element at the right pointer to the current sum current_sum += nums[right] # If the current sum is zero, update the max length and move both pointers if current_sum ==0: max_length = max(max_length, right - left +1) left = right # If the current sum is negative, reset it to zero and move the left pointer elif current_sum < 0: current_sum =0 left = right # Move the right pointer right +=1 return max_length
**注释**
* 我们使用了两个指针,分别从左到右和从右到左扫描数组。
* 当我们遇到一个元素时,我们将其添加到当前和中。如果当前和为零,我们更新最大长度并移动两个指针。
* 如果当前和为负数,我们重置它为零并移动左指针。
**第二题:**
**题目描述**
给定一个整数数组,要求找出其中最长的连续子序列,其元素之差为1。
**示例**
输入:[1,2,3,4,5]
输出:5(因为 [1,2,3,4,5] 是最长的连续子序列,其元素之差为1)
**代码实现**
def longest_one_diff_subarray(nums): """ Finds the longest continuous subarray with a difference of one. Args: nums (list): A list of integers. Returns: int: The length of the longest continuous subarray with a difference of one. """ max_length =1 current_length =1 # Iterate over the array from left to right for i in range(1, len(nums)): # If the difference between the current and previous elements is one, # increment the current length if nums[i] - nums[i -1] ==1: current_length +=1 # Otherwise, reset the current length to one else: current_length =1 # Update the max length if necessary max_length = max(max_length, current_length) return max_length
**注释**
* 我们使用了一个变量 `current_length` 来跟踪当前连续子序列的长度。
* 当我们遇到一个元素时,我们检查它与前一个元素之间的差值。如果差值为1,我们增加 `current_length`。否则,我们重置 `current_length` 为1。
* 我们更新最大长度 `max_length` 如果当前长度大于它。
**第三题:**
**题目描述**
给定一个整数数组,要求找出其中最长的连续子序列,其元素之和为奇数。
**示例**
输入:[1,2,3,4,5]
输出:5(因为 [1,2,3,4,5] 是最长的连续子序列,其元素之和为奇数)
**代码实现**
def longest_odd_sum_subarray(nums): """ Finds the longest continuous subarray with an odd sum. Args: nums (list): A list of integers. Returns: int: The length of the longest continuous subarray with an odd sum. """ max_length =0 current_sum =0 # Initialize two pointers, one at the start and one at the end of the array left =0 right =0 while right < len(nums): # Add the element at the right pointer to the current sum current_sum += nums[right] # If the current sum is odd, update the max length and move both pointers if current_sum %2 !=0: max_length = max(max_length, right - left +1) left = right # If the current sum is even, reset it to zero and move the left pointer elif current_sum %2 ==0: current_sum =0 left = right # Move the right pointer right +=1 return max_length
**注释**
* 我们使用了两个指针,分别从左到右和从右到左扫描数组。
* 当我们遇到一个元素时,我们将其添加到当前和中。如果当前和为奇数,我们更新最大长度并移动两个指针。
* 如果当前和为偶数,我们重置它为零并移动左指针。
**第四题:**
**题目描述**
给定一个整数数组,要求找出其中最长的连续子序列,其元素之差绝对值不超过3。
**示例**
输入:[1,2,3,4,5]
输出:5(因为 [1,2,3,4,5] 是最长的连续子序列,其元素之差绝对值不超过3)
**代码实现**
def longest_abs_diff_not_exceed_3_subarray(nums): """ Finds the longest continuous subarray with an absolute difference not exceeding3. Args: nums (list): A list of integers. Returns: int: The length of the longest continuous subarray with an absolute difference not exceeding3. """ max_length =0 current_sum =0 # Initialize two pointers, one at the start and one at the end of the array left =0 right =0 while right < len(nums): # Add the element at the right pointer to the current sum current_sum += nums[right] # If the absolute difference between the current and previous elements is not exceeding3, # update the max length and move both pointers if abs(nums[right] - nums[left]) <=3: max_length = max(max_length, right - left +1) left = right # Otherwise, reset the current sum to zero and move the left pointer else: current_sum =0 left = right # Move the right pointer right +=1 return max_length
**注释**
* 我们使用了两个指针,分别从左到右和从右到左扫描数组。
* 当我们遇到一个元素时,我们将其添加到当前和中。如果当前和与前一个元素之间的绝对差值不超过3,我们更新最大长度并移动两个指针。
* 如果当前和与前一个元素之间的绝对差值大于3,我们重置它为零并移动左指针。
**第五题:**
**题目描述**
给定一个整数数组,要求找出其中最长的连续子序列,其元素之差绝对值不超过2。
**示例**
输入:[1,2,3,4,5]
输出:5(因为 [1,2,3,4,5] 是最长的连续子序列,其元素之差绝对值不超过2)
**代码实现**
def longest_abs_diff_not_exceed_2_subarray(nums): """ Finds the longest continuous subarray with an absolute difference not exceeding2. Args: nums (list): A list of integers. Returns: int: The length of the longest continuous subarray with an absolute difference not exceeding2. """ max_length =0 current_sum =0 # Initialize two pointers, one at the start and one at the end of the array left =0 right =0