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,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
随机推荐
- kali访问宿主机Web页面解决方案
1.首先安装好PHPDVWA测试平台,将等级设置成low,kali中自带了python2.7.为了不再宿主机中修改python3.6,所以要利用kali来模访问宿主机中的Web页面.如果不进行配置修改 ...
- cf1076d 贪心最短路
#include<bits/stdc++.h> #include<queue> using namespace std; #define maxn 300005 #define ...
- Docker快速部署gitlab
环境: Centos7.5 安装Docker 1.移除旧版本: $ sudo yum remove docker \ docker-client \ docker-client-latest \ do ...
- easyui组件window拖动时超过浏览器顶部则无法拖回
项目研发过程中遇到一个问题,easyui的window可以随意拖动或者放大缩小,但是鼠标只有放在“header”上面时鼠标箭头才会变成四个方向的箭头,也就是只有在这时才能拖动窗口:但是当拖动的窗口超过 ...
- SSM 三大框架---事务处理
SSM 三大框架---事务处理 原创 2016年05月12日 20:57:03 标签: spring / J2EE / java / 框架 / 事务 7010 在学习三大框架的时候,老师说事务处理是最 ...
- iOS 测试之非代码获取 iPhone 型号及其他信息
首先 安装libimobiledevice和ideviceinstaller $ brew uninstall ideviceinstaller $ brew uninstall libimobile ...
- 关于Mybaits10种通用的写法
用来循环容器的标签forEach,查看例子 foreach元素的属性主要有item,index,collection,open,separator,close. item:集合中元素迭代时的别名, i ...
- 论文阅读笔记三十一:YOLO 9000: Better,Faster,Stronger(CVPR2016)
论文源址:https://arxiv.org/abs/1612.08242 代码:https://github.com/longcw/yolo2-pytorch 摘要 本文提出YOLO9000可以检测 ...
- FCN 项目部分代码学习
下面代码由搭档注释,保存下来用作参考. github项目地址:https://github.com/shekkizh/FCN.tensorflowfrom __future__ import prin ...
- maven如果正常配置不成功,就按照我的就可以配置成功了
常规的配置如下 非常规的配置 如果上面都没有问题,还是不能创建成功,就是maven配路径的问题,如图所示 如下图所示即是正确配置