题目描述:给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
1.暴力破解:双重循环(两个变量遍历)
index()方法语法:
str.index(str, beg=0, end=len(string))
参数
str -- 指定检索的字符串
beg -- 开始索引,默认为0。
end -- 结束索引,默认为字符串的长度。
class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i in nums: for j in nums: if i+j==target and i!=j: return [nums.index(i),nums.index(j)]