【leetcode刷题笔记】Two Sum
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
利用一个hashmap,把数组中的元素和它们的索引+1作为键-值对,然后对于每个元素numbers[i]寻找target-numbers[i],如果找到了就把i+1和map.get(target-numbers[i])返回。
代码如下:
- public class Solution {
- public int[] twoSum(int[] numbers, int target) {
- int[] answer = new int[2];
- if(numbers == null || numbers.length == 0)
- return answer;
- HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
- for(int i = 0;i < numbers.length;i++)
- map.put(numbers[i], i+1);
- for(int i = 0;i < numbers.length;i++){
- if(map.containsKey(target-numbers[i])){
- if(i+1 == map.get(target-numbers[i]))
- continue;
- answer[0] = i+1;
- answer[1] = map.get(target-numbers[i]);
- break;
- }
- }
- return answer;
- }
- }
【leetcode刷题笔记】Two Sum的更多相关文章
- 【leetcode刷题笔记】Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- LeetCode 刷题笔记 1. 两数之和(Two Sum)
tag: 栈(stack) 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案. ...
- (python)leetcode刷题笔记 01 TWO SUM
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- 【leetcode刷题笔记】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【leetcode刷题笔记】Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- linux程序设计——取消一个线程(第十二章)
12.7 取消一个线程 有时,想让一个线程能够要求还有一个线程终止,就像给它发送一个信号一样. 线程有方法能够做到这一点,与与信号处理一样.线程能够被要求终止时改变其行为. pthread_ca ...
- mysql 主从切换
4)提升slave为master Stop slave: Reset master; Reset slave all; 在5.6.3版本之后 Reset slave; 在5.6.3版本之前 查看sla ...
- mysql设置主从同步
1.主从同步定义 主从同步使得数据可以从一个数据库服务器复制到其他服务器上,在复制数据时,一个服务器充当主服务器(master),其余的服务器充当从服务器(slave).因为复制是异步进行的,所以从服 ...
- zoj 3827 Information Entropy 【水题】
Information Entropy Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Information ...
- ubuntu16.04 安装系统之后的开发必备-sourcelist--idk-sublime--opencv
设置sourcelist.txt # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释deb https://mirrors.tuna.tsinghua.edu.cn/ub ...
- [转载]Axure RP 7.0下载地址及安装说明
Axure RP是产品经理必备的原型制作工具,因为很多同学是新手,在这里整理一下axure7.0的下载.安装和汉化流程,希望能够帮到大家. Axure RP是美国Axure Software Solu ...
- Failed to read artifact descriptor for org.apache.httpcomponents:httpmime:jar
额,在Stackoverflow上找到了一个答案: I had this in eclipse and did this which fixed it(even though my command l ...
- Ubuntu配置apache2.4配置虚拟主机遇到的问题
update: 偶然看到了 apache的更新说明,直接贴个地址过来吧. http://httpd.apache.org/docs/2.4/upgrading.html 最近想把web开发目录从/va ...
- python学习 01 变量
1.变量不是‘盒子’. 1.1 不同的值,变量名没变, 变量地址也会变. 1.2 相同的值,不同的变量名,变量地址是相同的
- 多媒体开发之rtp 打包发流---udp 丢包问题
http://blog.csdn.net/acs713/article/details/19339707