1.给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
answer.
python暴力破解:
class Solution(object):
def twoSum(self, nums, target):
“”"
:type nums: List[int]
:type target: int
:rtype: List[int]
“”"
for i in range(0, len(nums)): //数组
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
//暴力破解主要思路是遍历数组

本文深入探讨了经典的两数之和算法问题,旨在寻找数组中和为目标值的两个整数并返回其下标。通过示例解析,介绍了暴力破解的实现方式,为读者提供了理解与实践的基础。
1045

被折叠的 条评论
为什么被折叠?



