描述

给一个整数数组,找到两个数使得他们的和等于一个给定的数 target。

你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标。注意这里下标的范围是 0 到 n-1。

你可以假设只有一组答案。

您在真实的面试中是否遇到过这个题?

样例

给出 numbers = [2, 7, 11, 15], target = 9, 返回 [0, 1].

挑战

Either of the following solutions are acceptable:

  1. O(n) Space, O(nlogn) Time
  2. O(n) Space, O(n) Time
  1. public class Solution {
  2. /**
  3. * @param numbers: An array of Integer
  4. * @param target: target = numbers[index1] + numbers[index2]
  5. * @return: [index1, index2] (index1 < index2)
  6. */
  7. public int[] twoSum(int[] numbers, int target) {
  8. // write your code here
  9. Map<Integer, Integer> map = new HashMap<>();
  10. for (int i=0; i<numbers.length; i++) {
  11. if (map.containsKey(numbers[i])) {
  12. return new int[] {map.get(numbers[i]), i};
  13. } else {
  14. map.put(target-numbers[i], i);
  15. }
  16. }
  17. return null;
  18. }
  19. }

56.两数之和.md的更多相关文章

  1. 领扣-1/167 两数之和 Two Sum MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

  3. 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X

    题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...

  4. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  5. LeetCode 371. Sum of Two Integers (两数之和)

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  6. LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  7. [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  8. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  9. Leetcode(一)两数之和

    1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...

随机推荐

  1. 用flask的扩展实现的简单的页面登录

    from flask import Flask,render_template,request,redirect,session app = Flask(__name__,template_folde ...

  2. LeetCode(80):删除排序数组中的重复项 II

    Medium! 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额 ...

  3. eclipse java ee ide 配置

    配置WEB服务器 windows->preferences->server->Runtime-Environments->Add->选择对应版本->选择安装路径-& ...

  4. Java 创建一个窗口,使其启动时位于屏幕中间

    import java.awt.Toolkit; import javax.swing.JFrame; public class WindowInTheMiddle extends JFrame { ...

  5. swagger2常用注解说明

    说明: 1.这里使用的版本:springfox-swagger2(2.4)springfox-swagger-ui (2.4) 2.这里是说明常用注解的含义和基本用法(也就是说已经对swagger进行 ...

  6. last与lastb命令 读取的日志文件

    在linux系统中,last与lastb命令用来列出目前与过去登录系统的用户相关信息.指令英文原义: last, lastb - show listing of last logged in user ...

  7. install memcached for ubuntu

    Memcached安装 1.先下载安装libevent 安装 libevent# tar zxvf libevent-1.4.9-stable.tar.gz# cd libevent-1.4.9-st ...

  8. Django实现注册页面_头像上传

    Django实现注册页面_头像上传 Django实现注册页面_头像上传 1.urls.py 配置路由 from django.conf.urls import url from django.cont ...

  9. 运维基础——Zabbix 设置Redis监控

    https://blog.csdn.net/xundh/article/details/77604357

  10. JavaScript中this 详解

    涵义 this 关键字是一个非常重要的语法点.毫不夸张地说,不理解它的含义,大部分开发任务都无法完成. 首先, this 总是返回一个对象,简单说,就是返回属性或方法“当前”所在的对象. this.p ...