Leetcode_1. 两数之和

1. 两数之和

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

f1 双重循环暴力破解

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        #首先判断给的数组是否为空
        if len(nums):
        	return []
        #利用python的内置函数enumerate,可以返回下标和数值
        for index , value in enumerate:
        	for index2 in range(index+1,len(nums)):
        		if value + nums[index2] == target:
        			return [index , count]

普通的 for 循环
>>>i = 0
>>> seq = ['one', 'two', 'three']
>>> for element in seq:
...     print i, seq[i]
...     i +=1
... 
0 one
1 two
2 three

for 循环使用 enumerate
>>>seq = ['one', 'two', 'three']
>>> for i, element in enumerate(seq):
...     print i, element

0 one
1 two
2 three

f2求差值,判断差值是否在nums数组里

列表[]取值
nums.index(值)
class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        for index1 in range(len(nums)):
            value2 = target - nums[index1]
            if value2 in nums:
            	#求对应的值的下标
                index2 = nums.index(value2)
                if index2!=index1:
                    return index1,index2

时间复杂度:O(n2),空间复杂度:O(1) (补充:python中list对象的存储结构采用的是线性表,因此其查询复杂度为O(n) 也就是 if b in nums 时间复杂度是O(n))

f3创建一个空字典

求差值、把差值存进字典里作为键、索引作为值,第一次循环理解:d[7]=0 即字典d={7:0},表示为索引0需要数组里值为7的元素配对。 if 判断是否为前面元素所需要配对的值 , 是则返回两个索引值。(补充:nums[x] in d 是判断值是否在字典某个key里面) 差值:索引

class Solution:
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        [2,7,11,15]
        9
        """           
        n = len(nums)
        #创建一个空字典
        d = {}
        for index1 in range(n):
            value2 = target - nums[index1]
            #value2 = 9-2=7,index=0
            #value2 = 9-7=2,index1=1时,7在d中符合
            #字典d中存在nums[index1]时
            if nums[index1] in d:
                return d[nums[index1]],index1
            #否则往字典增加键/值对
            else:
                d[value2] = index1
        #边往字典增加键/值对,边与nums[x]进行对比
        #第一次{7,0}

dict对象的存储结构采用的是散列表(hash表),其在最优情况下查询复杂度为O(1)

JAVA

class Solution {
    public int[] twoSum(int[] nums, int target) {
        for (int i = 0 ; i<nums.length;i++){
            for (int j=i+1;j<nums.length;j++){
                if(num[i]+nums[j]==target){
                    return new int[]{i,j};
                }
            }
        }
        return null;
    }
}

class Solution {
    public int[] twoSum(int[] nums, int target) {
        for (int i = 0 ; i<nums.length;i++){
            for (int j=i+1;j<nums.length;j++){
                if(nums[i] + nums[j] == target){
                    return new int[]{i,j};
                }
            }
        }
        return null;
    }
}
class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i=0 ; i<nums.length ; i++){
            int complement = target - nums[i];
            if (map.containsKey(complement)){
                return new int[]{map.get(complement),i};
            }
            map.put(nums[i],i);
        }
        return new int []{};
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值