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

import java.util.HashMap;
import java.util.Vector;

public class Solution {
    public  int[] twoSum(int[] numbers, int target)
    {
        int result[]= new int[2];
        HashMap<Integer,Vector<Integer>> aaa = new HashMap<Integer,Vector<Integer>>();
        for(int i=0;i<numbers.length;i++)
        {
            if( aaa.containsKey(numbers[i]) )
            {
                Vector<Integer> index = aaa.get(numbers[i]);
                index.add(i+1);
            }
            else
            {
                Vector<Integer> index = new Vector<Integer>();
                index.add(i+1);
                aaa.put(numbers[i], index);
            }
        }
        for(int i=0;i<numbers.length;i++)
        {
            int subTarget = target - numbers[i];
            if(aaa.containsKey(subTarget))
            {
                Vector<Integer> index = aaa.get(subTarget);
                for(int j = 0;j<index.size();j++)
                {
                    if(index.get(j)!=i+1)
                    {
                        result[0] = i+1;
                        result[1] = index.get(j);
                        return result;
                    }
                }
            }
        }
        return result;
    }
}

上面是个很笨的解法,下面是Discuss里面一个很酷的解法:


import java.util.HashMap;


public class Solution {


public int[] twoSum(int[] numbers, int target) {


int[] result = new int[2];
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();


for(int i = 0; i < numbers.length; i++){
if(map.containsKey(numbers[i])){
result[0] = map.get(numbers[i]) + 1;
result[1] = i + 1;
}
map.put(target - numbers[i], i);
}
return result;
}
}

 

但是这个解法我有一点疑虑:

对于输入:1 4 2 2 6 5 8。   target:8

该程序会输出 4 5

但是实际上应该输出 3 5

但是上述代码在leetcode中是通过的,不知道是不是因为leetcode上的测试用例没有检测这种情况。

Leetcode题1的更多相关文章

  1. leetcode题库

    leetcode题库 #题名题解通过率难度出现频率  1 两数之和     46.5%简单2 两数相加     35.5%中等3 无重复字符的最长子串     31.1%中等4 寻找两个有序数组的中位 ...

  2. 简单的leetcode题

    简单的leetcode题 环绕字符串中唯一的子字符串 把字符串 s 看作是\("abcdefghijklmnopqrstuvwxyz"\)的无限环绕字符串,所以 s 看起来是这样的 ...

  3. 这道LeetCode题究竟有什么坑点,让它的反对是点赞的9倍?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第38篇文章,我们一起来看看第65题,Valid Number. 曾经我们聊到过算法当中的一个类别--模拟题.所 ...

  4. C++/Java小白解Leetcode题,发现了知识盲区……

    一.初见LeetCode 大一时候学习C++,根据课程一直在PTA平台做题目,数据结构和算法的作业题目也是在PTA.后来发现牛客网学习资源也很丰富,孤陋寡闻,前几个月在知道LeetCode这个平台,跟 ...

  5. leetcode题库解答源码(python3)

    下面和大家分享本人在leetcode上已经ace的题目源码(python3): 本人会持续更新!- class Leetcode_Solution(object): def twoSum_1(self ...

  6. LeetCode题库整理(自学整理)

    1. Two Sum 两数之和       来源:力扣(LeetCode) 题目:给定一个整数数组和一个目标值,找出数组中和为目标值的两个数.你可以假设每个输入只对应一种答案,且同样的元素不能被重复利 ...

  7. LeetCode 题目的 Python 实现(持续更新中)

    Python-LeetCode 是一个使用 Python 语言解决 LeetCode 问题的代码库,库有以下几个方面需要注意: 所有题目都是 AC 的: 按照题目顺序,每 50 个放在一个目录下,方便 ...

  8. LeetCode题:旋转链表

    原题: 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2输出: ...

  9. leetcode题库练习_数组中重复的数字

    题目:数组中重复的数字 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次 ...

随机推荐

  1. c++运行时类型识别(rtti)

    一个简单运行时类型识别 namespace rtti_ex { /* * 类型信息基类 */ class i_type_info { public: // 判断是否是指定类型 bool is(cons ...

  2. HDU/5499/模拟

    题目链接 模拟题,直接看代码. £:分数的计算方法,要用double; #include <set> #include <map> #include <cmath> ...

  3. jquery 中 $('div','li')

    要搞清楚$('div','li') 和 $('div , li') 和 $('div li') 区别$('div','li')是$(子,父),是从父节点里找子,而不是找li外面的div $('div ...

  4. 留言本,keyCode

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  5. ajax2016/4/15 post与get

    post方式,数据放在send()里面作为参数传递 xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded'); ...

  6. This compilation unit is not on the build path SVN

    This compilation unit is not on the build path of a Java project 解决办法​ 把项目导入STS(基于Eclipse)时,项目出现问题, ...

  7. linux 命令实现原理

    我们知道有些Linux的命令涉及到一些高效率的算法,在此做出一个积累吧,不是系统的. 1.tail命令打印一个文件的最后num行 2.grep命令从文本中匹配字符串 基于正则表达式的匹配很快. it ...

  8. startActivityForResult相关的

    在Fragment里调用startActivityForResult,界面回来后,会先走其宿主Activity的onActivityResult方法,再走Fragment的. 在Fragment里面嵌 ...

  9. js的阻塞特性

    JS具有阻塞特性,当浏览器在执行js代码时,不能同时做其它事情,即<script>每次出现都会让页面等待脚本的解析和执行(不论JS是内嵌的还是外链的),JS代码执行完成后,才继续渲染页面. ...

  10. elasticsearch 配置说明

    elasticsearch的config文件夹里面有两个配置文件:elasticsearch.yml和logging.yml,第一个是es的基本 配置文件,第二个是日志配置文件,es也是使用log4j ...