• LeeCode 1-Two Sum


    Two Sum

    Total Accepted: 125096 Total Submissions: 705262

    Question Solution

    Given an array of integers, find two numbers such that they add up to a specific target number.

    The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

    You may assume that each input would have exactly one solution.

    Input: numbers={2, 7, 11, 15}, target=9
    Output: index1=1, index2=2

    在一个数组中,找出两个数字的和等于target的下标。

    Java直接暴露找,提交还通过过了,时间复杂度:O(N^2)

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

    下面是根据hashmap,在把数组中的数存在Map之间,先要把target减去数组中的这个数字,对后来进入的数组,判断这个数是否在map中,如果在,说明找到了这两个数

    这样的时间复杂度是O(N)

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

    下面用Python以此实现上面的两个程序

    class Solution(object):
        def twoSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            res=[0,0]
            for i in range(len(nums)):
                for j in range(len(nums)):
                    if nums[i] + nums[j] == target:
                        res[0]= i+1
                        res[1]= j+1
                        return res

    很荣幸,这个提示时间超时

    根据字典dict

    class Solution(object):
        def twoSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            dicts = {}
            for i in range(len(nums)):
                if nums[i]  in dicts.keys():
                    return (dicts[nums[i]],i+1)
                else:
                    dicts[target- nums[i]] = i + 1

    字典的key = nums[i],value=i+1

    这里每次是吧target-nums[i] i+1 分布以key 和value 存入字典的,对后面的nums[i] 判断是否在字典中,在的话,说明结束程序

    当然也可以每次吧nums[i] i+1 存在字典中,判断target-nums[i] 是否已经在字典中了

    如下程序,一样AC

    class Solution(object):
        def twoSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            dicts = {}
            for i in range(len(nums)):
                if target - nums[i]  in dicts.keys():
                    return (dicts[target - nums[i]],i+1)
                else:
                    dicts[nums[i]] = i + 1
  • 相关阅读:
    luogu P4587 [FJOI2016]神秘数
    luogu P4042 [AHOI2014/JSOI2014]骑士游戏
    luogu P2597 [ZJOI2012]灾难
    一则胡乱科普
    NJU Static Program Analysis 09: Pointer Analysis II
    NJU Static Program Analysis 08: Pointer Analysis I
    NJU Static Program Analysis 07: Interprocedural Analysis
    NJU Static Program Analysis 06: Data Flow Analysis IV
    LianYunGang OI Camp: Contest #2
    NJU Static Program Analysis 05: Data Flow Analysis III
  • 原文地址:https://www.cnblogs.com/bbbblog/p/4755778.html
Copyright © 2020-2023  润新知