56.两数之和.md
描述
给一个整数数组,找到两个数使得他们的和等于一个给定的数 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/167 两数之和 Two Sum MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
- 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- 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 ...
- 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 ...
- [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 ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- Leetcode(一)两数之和
1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
随机推荐
- 用flask的扩展实现的简单的页面登录
from flask import Flask,render_template,request,redirect,session app = Flask(__name__,template_folde ...
- LeetCode(80):删除排序数组中的重复项 II
Medium! 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额 ...
- eclipse java ee ide 配置
配置WEB服务器 windows->preferences->server->Runtime-Environments->Add->选择对应版本->选择安装路径-& ...
- Java 创建一个窗口,使其启动时位于屏幕中间
import java.awt.Toolkit; import javax.swing.JFrame; public class WindowInTheMiddle extends JFrame { ...
- swagger2常用注解说明
说明: 1.这里使用的版本:springfox-swagger2(2.4)springfox-swagger-ui (2.4) 2.这里是说明常用注解的含义和基本用法(也就是说已经对swagger进行 ...
- last与lastb命令 读取的日志文件
在linux系统中,last与lastb命令用来列出目前与过去登录系统的用户相关信息.指令英文原义: last, lastb - show listing of last logged in user ...
- install memcached for ubuntu
Memcached安装 1.先下载安装libevent 安装 libevent# tar zxvf libevent-1.4.9-stable.tar.gz# cd libevent-1.4.9-st ...
- Django实现注册页面_头像上传
Django实现注册页面_头像上传 Django实现注册页面_头像上传 1.urls.py 配置路由 from django.conf.urls import url from django.cont ...
- 运维基础——Zabbix 设置Redis监控
https://blog.csdn.net/xundh/article/details/77604357
- JavaScript中this 详解
涵义 this 关键字是一个非常重要的语法点.毫不夸张地说,不理解它的含义,大部分开发任务都无法完成. 首先, this 总是返回一个对象,简单说,就是返回属性或方法“当前”所在的对象. this.p ...