描述

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

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

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

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

样例

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

挑战

Either of the following solutions are acceptable:

O(n) Space, O(nlogn) Time
O(n) Space, O(n) Time
public class Solution {
/**
* @param numbers: An array of Integer
* @param target: target = numbers[index1] + numbers[index2]
* @return: [index1, index2] (index1 < index2)
*/
public int[] twoSum(int[] numbers, int target) {
// write your code here
Map<Integer, Integer> map = new HashMap<>();
for (int i=0; i<numbers.length; i++) {
if (map.containsKey(numbers[i])) {
return new int[] {map.get(numbers[i]), i};
} else {
map.put(target-numbers[i], i);
}
}
return null;
}
}

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. github文档

    Video Guides GitHub Help GitHub.com   Hello World 10 minute read Intro What is GitHub? Create a Repo ...

  2. Brup Suite 渗透测试笔记(五)

    之前章节记到Burp Intruder功能区,接上次笔记 一.首先说再展开说说Brup Intruder功能, 1.标识符枚举Web应用程序经常使用标识符来引用用户账户,资产数据信息. 2.提取有用的 ...

  3. python(2): If/for/函数/try异常/调试/格式输出%

    (一) if  if a1==a2: print('ok') if:  else: if: elif: ... else:  注意缩进 猜数字游戏 from random import randint ...

  4. python之路第二天

    为何要有操作系统 为了让程序员更轻松的完成命令电脑工作而存在的,控制硬件,服务于软件. 操作系统的位置 操作系统位于软件和硬件之间.操作系统由内核(运行于内核态,控制硬件)和系统调用(运行于用户态,为 ...

  5. SSM框架接收处理安卓端的json数据

    最近项目上与安卓端做JSON数据交互,使用的SSM框架,刚开始的时候感觉很简单,想着不就是把安卓端的JSON数据封装为Bean类对象吗? 于是就这样写了 可是这样一直报400,百度原因是因为请求url ...

  6. 阿里云人脸识别测试接口出错 返回Body:{ "errno": 1031, "err_msg": "Invalid Image URL.", "request_id": "cdbe2927-e1bb-4eb1-a603-8fcd4b0b7fc8" }

    错误信息如下 返回Body:{ "errno": 1031, "err_msg": "Invalid Image URL.", " ...

  7. Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state

    For me, the solution (workaround) is to turn off JavaScript debugging on Chrome, which I believe is ...

  8. Java+selenium之WebDriver的cookie,等待等高级操作(五)

    1. 操作cookie // 增加一个 name = "name",value="value" 的 cookie Cookie cookie = new Coo ...

  9. 51 nod 1212 无向图最小生成树(Kruckal算法/Prime算法图解)

    1212 无向图最小生成树 N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树. 收起 输入 第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量.(2 <= N < ...

  10. PyCharm之python书写规范--消去提示波浪线

    强迫症患者面对PyCharm的波浪线是很难受的,针对如下代码去除PyCharm中的波浪线: # _*_coding:utf-8_*_ # /usr/bin/env python3 A_user = & ...