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 ...
随机推荐
- Celery-4.1 用户指南: Extensions and Bootsteps (扩展和Bootsteps)
自定义消息消费者 你可能想要嵌入自定义的 Kombu 消费者来手动处理你的消息. 为了达到这个目的,celery 提供了一个 ConsumerStep bootstep 类,你只需要定义 get_co ...
- pycharm中 unittests in xxxx 运行模式
pycham中 当你运行时 ,使用的 是 Run "unittests in xxxx" 模式时候,if __name__ == '__main__': 后面的代码是不执行的 ...
- 第五章 JVM调优(待续)
Java虚拟机内存模型 JVM内存分配参数 垃圾收集基础 常用调优案列和方法 实用JVM参数 实战JVM调优
- ASP.NET——配置文件——连接字符串
一.连接字符串 1.通过<connectionStrings>方式: 方式一:SqlServer身份验证 <connectionStrings> <add name=&q ...
- NoSQL概述
- 使用Post进行Http通信
--------------siwuxie095 有道翻译官网:http://fanyi.youdao.com/ 找到官网页面下方的 有 ...
- ES6中变量的解析赋值的用途
变量的解构赋值用途很多. (1)交换变量的值 let x = 1; let y = 2; [x, y] = [y, x]; 上面代码交换变量x和y的值,这样的写法不仅简洁,而且易读,语义非常清晰. ( ...
- ROS Learning-006 beginner_Tutorials 编译ROS程序包
ROS Indigo beginner_Tutorials-05 编译 ROS 程序包 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04 ...
- spoj1716 Can you answer these queries III
传送门 (分析见正睿2018.10.1笔记) 代码 #include<iostream> #include<cstdio> #include<cstring> #i ...
- DesertWind TopCoder - 1570
传送门 分析 首先我们要知道为了情况最坏,无论你到哪,一定会在你前往绿洲的最短路上的那片沙子上刮风,所以这个点到绿洲的最短路即为他相连的点中到绿洲距离最短的距离+3和次短的距离+1的最小值,具体实现见 ...