Test1
由bqjdzn7f创建,最终由bqjdzn7f 被浏览 92 用户
#102
def int_most(arr):
max_element = None
max_count = 0
current_element = None
current_count = 0
for element in arr:
if element == current_element:
current_count += 1
else:
current_element = element
current_count = 1
if current_count > max_count:
max_count = current_count
max_element = current_element
return max_element
# 示例用法
sorted_array = [1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5]
result = int_most(sorted_array)
print("出现次数最多的元素是:", result)
#103
def re_duplicates(nums):
if not nums:
return 0
unique_count = 1 # 用于记录不重复元素的个数
for i in range(1, len(nums)):
if nums[i] != nums[i - 1]:
nums[unique_count] = nums[i]
unique_count += 1
return unique_count
# 示例用法
sorted_array = [1, 1, 2, 2, 2, 3, 4, 4, 5]
new_length = re_duplicates(sorted_array)
print("去重后的数组:", sorted_array[:new_length])
#104
def find_two_sum_count(nums, target):
count = 0 # 用于记录符合条件的情况数
left, right = 0, len(nums) - 1
while left < right:
current_sum = nums[left] + nums[right]
if current_sum == target:
count += 1
left += 1 # 移动左指针,寻找下一个可能的组合
right -= 1 # 移动右指针,寻找下一个可能的组合
elif current_sum < target:
left += 1 # 和太小,增加左边的数
else:
right -= 1 # 和太大,减小右边的数
return count
# 示例用法
sorted_array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
target = 10
count = find_two_sum_count(sorted_array, target)
print("符合条件的情况数为:", count)
\