题目

解题

def two_sum(numbers, target):
    left = 0
    right = len(numbers) - 1

    while left < right:
        current_sum = numbers[left] + numbers[right]
        if current_sum == target:
            return [left + 1, right + 1]  # 下标从1开始
        elif current_sum < target:
            left += 1
        else:
            right -= 1


# 测试例子
numbers = [2, 7, 11, 15]
target = 9
result = two_sum(numbers, target)
print("满足条件的下标:", result)
08-07 12:48