1.Tow Sum(两数和)
Level:
Easy
题目描述:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1]
思路分析:
给定一个目标值,要返回数组中两个数之和等于该目标值的两数下标,我们可以借助map来实现,将元素作为键,下标作为值存放进map,我们将目标值记为target,当遍历到数组中某个元素num时,我们查看map中是否存有target-num这个键,如果存在那么我们就找到了满足要求的两个数,如果不存在,继续向下遍历,直到找到答案。
代码:
class Solution {
public int[] twoSum(int[] nums, int target) {
int []res=new int[2];
HashMap<Integer,Integer>map=new HashMap<>();
int temp;
for(int i=0;i<nums.length;i++){
temp=target-nums[i];
if(map.get(temp)!=null){
res[0]=map.get(temp);
res[1]=i;
break;
}else{
map.put(nums[i],i);
}
}
return res;
}
}
1.Tow Sum(两数和)的更多相关文章
- LeetCode刷题 1. Two Sum 两数之和 详解 C++语言实现 java语言实现
1. Two Sum 两数之和 Given an array of integers, return indices of the two numbers such that they add up ...
- 【LeetCode】1. Two Sum 两数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...
- [LeetCode] 1. Two Sum 两数和
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- [LeetCode] Two Sum 两数之和
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- [LeetCode] 1.Two Sum 两数之和分析以及实现 (golang)
题目描述: /* Given an array of integers, return indices of the two numbers such that they add up to a sp ...
- [LeetCode]1.Two Sum 两数之和&&第一次刷题感想
---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...
- [LintCode] Two Sum 两数之和
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- [leetcode]1. Two Sum两数之和
Given an array of integers, return indices of the two numbers such that they add up to a specific t ...
随机推荐
- 应用HTMLTestRunner整合测试报告
为了便于测试脚本的维护,以及更多测试用例的管理,于是根据上次学习的HTMLTestRunner生成的测试报告,今天将对其进行整理.我们之前使用 TestSuite 只是在一个.py 文件里添加多个测试 ...
- system中有空格怎么办
原始路径: C:\\Program Files\\putty\\putty.exe 改为: char *cmd="C:\\\"Program Files\"\\putty ...
- namenode和datanode机制
转自:https://www.cnblogs.com/DarrenChan/p/6416043.html?utm_source=itdadao&utm_medium=referral 首先我们 ...
- 向linux内核增加一个系统调用-2(利用proc打印信息)
添加系统调用,打印/proc中的系统信息 前面关于proc和内核态函数的东西可以对比代码来看. 参考 http://blog.csdn.net/kylin_fire_zeng/article/deta ...
- 【264】◀▶ Windows 批处理(CMD)
参考:Windows Commands 微软官方帮助 参考:DOS命令自学小窍门:巧用help命令 参考:bat批处理的注释语句 打开文件夹: start D:\abc 打开D盘abc文件夹 打开ex ...
- 定时node-schedule 模块的使用
You can install using npm. npm install node-schedule var schedule = require('node-schedule'); var j ...
- vue插件开发与发布
vue插件的规范 / plug.js Toast={}Toast.install=function(){ Vue.prototype.$toast=function(){ }} // 导出这个对象 e ...
- php学习笔记-continue和break
这两个关键字经常被用在循环中,但作用是完全不同的. 在循环中遇到continue这个单词的时候一定要理解为skip,跳过或者略过,啥意思?就是跳过本次循环,后面的循环继续走起来,老铁. break是说 ...
- 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的
1. package algorithms.stacks13; /******************************************************************* ...
- JavaScript中的Array.prototype.slice.call()方法学习
JavaScript中的Array.prototype.slice.call(arguments)能将有length属性的对象转换为数组(特别注意: 这个对象一定要有length属性). 但有一个例外 ...