• 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
  • 相关阅读:
    Ajax实现文件下载
    jquery easyui 插件开发
    Chrome谷歌浏览器首页被改为Hao123导航怎么办|附各类解决方法【转】
    查看mysql版本的四种方法
    IntelliJ IDEA 快捷键大全
    Java中判断字符串是否为数字的五种方法
    比数据分析更要命的是:数据质量
    Python绘制六种可视化图表详解,三维图最炫酷!你觉得呢?
    大数据需要好设计
    Python模块学习filecmp文件比较
  • 原文地址:https://www.cnblogs.com/theskulls/p/4755778.html
Copyright © 2020-2023  润新知