[leetcode]1. Two Sum两数之和
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].
Solution1: Brute-force
Lock pointer i, move pointer j to check if nums[j] == target - nums[i].



code:
/* Time Complexity: O(n*n)
Space Complexity: O(1)
*/
class Solution {
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] == target - nums[i]) {
return new int[] { i, j };
}
}
}
return null;
}
}
Solution2: HashMap
Use a map to record the index of each element and check if target - nums[i] is in the map. if so, we find a pair whose sum is equal to target.




code:
/* Time Complexity: O(n)
Space Complexity: O(n)
*/
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i< nums.length; i++){
if(map.containsKey(target - nums[i])){
return new int[]{i, map.get(target - nums[i])};
}else{
map.put(nums[i], i);
}
}
return null;
}
}
[leetcode]1. Two Sum两数之和的更多相关文章
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- [LeetCode]1.Two Sum 两数之和&&第一次刷题感想
---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...
- [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】Two Sum(两数之和)
这道题是LeetCode里的第1道题. 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会 ...
- [LeetCode]1.Two Sum 两数之和(Java)
原题地址:two-sum 题目描述: 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每 ...
- 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:0002(两数之和)
LeetCode:0002(两数之和) 题目描述:给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两 ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- autotools
文章目录 原文地址 Autotools上手指南1--autoconf基本思想 Autotools上手指南2--autoscan生成configure.ac Autotools上手指南3--autohe ...
- TableLayoutPanel 动态添加 行 列
//添加行 横排 ++this.tbPnl.RowCount; this.tbPnl.RowStyles.Add(new System.Windows.Forms.RowStyle(System ...
- Firebird 烂笔头(一)
下载非安装版,将文件解压缩到D:\FireBird2.5下面.然后里面有.bat文件,选择自己适合的类型安装后,在服务里面会有一个firebirdserver开头的服务,右键启动. win+R,在命令 ...
- 在Win10 Anaconda中安装Tensorflow
有需要的朋友可以参考一下 1.安装Anaconda 下载:https://www.continuum.io/downloads,我用的是Python 3.5 下载完以后,安装. 安装完以后,打开A ...
- Win7系统安装Centos7.0双系统(一)
项目环境测试,过去在虚拟机安装只要配置好镜像很快就可以轻松安装,但是在真实机中安装就有些略坑.网上有很多说法是把iso文件中安装引导拷出来,试了很久还是觉得U盘镜像直接安装更便捷.靠谱.因为CentO ...
- VMware Workstation 11 搭建windows server 2012 之sql server 2012集群常见问题整理
1.windows server 2012内置支持iSCSI发起程序无需额外安装,iSCSI Software Target 可作为“文件和存储服务”角色下的内置功能使用 2.拷贝虚拟机的文件加入域时 ...
- Linux背背背(4)vim操作
目录 1.打开文件 2.vim的三种模式 3.扩展 (关于vi 和 vim 的区别,它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面.) 1 ...
- python第三方库Requests的基本使用
Requests 是用python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...
- CentOS7 JDK1.8部署详解
一.下载 首先需要将本地下载好的jdk包拷贝到Linux中:scp jdk-8u91-linux-x64.tar.gz hadoop@192.168.***.***:~/software/ //星号处 ...
- lvm管理:扩展lv、删除pv、lv等
从卷组VG里扩展lv.删除pv,并删除物理卷PV 一.扩展LV.缩小LV 1.卸载LV 命令:umount "挂载目录" 2.扩展LV 命令:lvextend -L +500m ...