LeetCode——TwoSum

题目:

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

我的解法:

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

系统给出结果:

Time Limit Exceeded
显然系统无法忍受我时间复杂度为O(n^2)的时间复杂度。

在讨论版看到一个复杂度为O(n)的算法:

import java.util.Hashtable;
public class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int [] re=new int[2];
        Hashtable<Integer,Integer> hashtable=new Hashtable<Integer,Integer>();
        int len=numbers.length;
        for(int i=0;i<len;i++){
            Integer n=hashtable.get(numbers[i]);
            if(n==null) hashtable.put(numbers[i],i);
            n=hashtable.get(target-numbers[i]);
            if(n!=null&&n<i){
                    re[0]=n+1;
                    re[1]=i+1;
                    return re;
            }

        }
        return re;
    }
}
时间: 2024-10-27 07:29:18

LeetCode——TwoSum的相关文章

[leetcode]TwoSum系列问题

1.普通数组找两个数,哈希表建立数值和下标的映射,遍历时一边判断一边添加 /* 哇,LeetCode的第一题...啧啧 */ public int [] twoSum(int[] nums, int target) { /* 两个数配合组成target 建立值和下标的映射,遍历数组时一边判断是否有另一半,一边添加新映射到哈希表 */ Map<Integer,Integer> map = new HashMap<>(); for (int i = 0; i < nums.len

leetCode:twoSum 两数之和 【JAVA实现】

LeetCode 两数之和 给定一个整数数组,返回两个数字的索引,使它们相加到特定目标. 您可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 更多文章查看个人博客 个人博客地址:twoSum 两数之和 [JAVA实现] 方法一 使用双重循环两两相加判断是否等于目标值 public List<String> twoSum2(int[] arr, int sum) { if (arr == null || arr.length == 0) { return new ArrayL

LeetCode | TwoSum

def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ dict = {} result = [] for i in range(len(nums)): dict[nums[i]]= i print(dict) for i in range(len(nums)): if (dict.get(target-nu

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

LeetCode 1 # TwoSum

LeetCode 1 # TwoSum 很有意思的问题. Two Sum Total Accepted: 63448 Total Submissions: 350576 My Submissions 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 in

leetcode #1 twoSum问题:简单实用哈希表

因为不是计算机专业的,算法基础有点差,所以最近开始在leetcode上刷刷题补一补. #1 问题链接:twoSum 问题描述: 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

LeetCode 之 TwoSum

题目: 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 t

leetcode之twosum

1 class Solution { 2 public: 3 vector<int> twoSum(vector<int>& nums, int target) { 4 vector<int>result; 5 int i,j,k; 6 map<int,int>h; 7 for(i=0;i<nums.size();i++) { 8 if(!h.count(nums[i])) 9 h[nums[i]]=i+1; 10 k=h[target-num

Leetcode Array 1 twoSum

Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15]