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 展开关闭效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. bos项目经验心得(1)

    ---恢复内容开始--- 一.搭建数据库环境: 1.在cmd窗口登录mysql数据库: mysql -uroot -proot   (mysql登录数据库的形式就是 mysql-u用户名 -p密码) ...

  4. 命令行调试smali

    am start -D -n com.agilebits.onepassword/.activity.LoginActivity ps | grep passu0_a126 1107 217 5107 ...

  5. 屏幕的尺寸(厘米)、屏幕分辨率(像素)、PPI它们之间是什么关系

    屏幕的尺寸(厘米).屏幕分辨率(像素).PPI它们之间是什么关系? 添加评论 分享 赞同2反对,不会显示你的姓名 知乎用户,数据ETL,UNITY3D 刘大侠.如果 赞同 以iphone4 为例,分辨 ...

  6. C++:bitset类的使用

    #include <iostream> #include <bitset> using namespace std; int main() { //初始化一个bitmap , ...

  7. PHP开源CRM-推荐几个

    http://www.xinyou88.com/about/xcrm.html 因为医院要同步用户到诊统计的信息.是拿着表单来回送.途中大概有20分钟左右.有些机器和互联网可以搞定的事情.人力来做.在 ...

  8. 第19章 网络通信----TCP程序设计基础

    TCP网络程序设计是指利用Socket类编写通信程序.利用TCP协议进行通信的两个应用程序是有主次之分的,一个称为服务器程序,另一个称为客户机程序,两者的功能和编写方法大不一样. 1.InetAddr ...

  9. 深入理解React、Redux

    深入理解React.ReduReact+Redux非常精炼,良好运用将发挥出极强劲的生产力.但最大的挑战来自于函数式编程(FP)范式.在工程化过程中,架构(顶层)设计将是一个巨大的挑战.要不然做出来的 ...

  10. linux创建vg、lv

    LVM磁盘管理 一.LVM简介... 1 二. LVM基本术语... 2 三. 安装LVM... 3 四. 创建和管理LVM... 4 2. 创建PV.. 6 3. 创建VG.. 7 4. 创建LV. ...