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. Python作用域

    以下依据Python 3 1.Python变量查找顺序为LEGB(L:Local,E:Enclosing,G:Global,B:Built-in). 2.实际上,在Python中,只有模块,类以及函数 ...

  2. jquery 仿购物车的加减数量

    <p>单价:3.95</p> <input id="min" name="" type="button" va ...

  3. iOS客户端开发与Web前端开发

    转载自:http://blog.cnbang.net/tech/1813/不知不觉做iOS客户端开发已经半年多了,了解到iOS客户端开发与Web前端开发的一些异同,写一下. 版本升级.用户角度上看,客 ...

  4. ViewPager和Fragment组合 v4包下的页面切换

    /* *主页面下 */ //-------------主页面下---------------------- package com.example.viewpagerfragment; import ...

  5. LightOJ 1030 Discovering Gold(期望)

    Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell o ...

  6. 图片处理中的Dithering技术

    话说二战的时候,美国轰炸机每次执行任务,除了满载着威力强大的炸弹以外,还常常要装配一台计算机,飞机飞行方向和投弹的抛物线的计算都离不开这台机器.可是世界上第一台电子计算机在二战结束后才发明,轰炸机上当 ...

  7. .Net多线程编程—Parallel LINQ、线程池

    Parallel LINQ 1 System.Linq.ParallelEnumerable 重要方法概览: 1)public static ParallelQuery<TSource> ...

  8. android:editable is deprecated: Use an <EditText> to make it editable

    问题:android:editable is deprecated: Use an to make it editable   意思:Android的:编辑是反对:使用<</span> ...

  9. Spring注解基本解读

    在一个类中使用Spring对象,办法如下: 使用注解的形式注入 从ApplicationContext中获取. T t = new ApplicationContext.getBean("x ...

  10. Java实现随意切换VPN改变上网地区

    http://www.jb51.net/article/69267.htm 这篇文章主要介绍了Java实现随意切换VPN改变上网地区,本文直接给出实例代码,需要的朋友可以参考下 在很多情况下,有些网络 ...