Leetcode#1.Two Sum(两数之和)
题目描述
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
思路
思路一:
暴力
思路二:
用 HashMap 存储数组元素和索引的映射,在访问到 nums[i] 时,判断 HashMap 中是否存在 target - nums[i] ,如果存在说明 target - nums[i] 所在的索引和 i 就是要找的两个数。该方法的时间复杂度为 O(N),空间复杂度为 O(N),使用空间来换取时间。
代码实现
package Array;
import java.util.Arrays;
import java.util.HashMap;
/**
* 两数之和
* 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
* 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
*/
public class Solution1 {
public static void main(String[] args) {
Solution1 solution1 = new Solution1();
int[] nums = {2, 7, 11, 15};
int target = 9;
int[] result = solution1.twoSum(nums, target);
System.out.println(Arrays.toString(result));
}
/**
* 用 HashMap 存储数组元素和索引的映射,在访问到 nums[i] 时,判断 HashMap 中是否存在 target - nums[i]
* 如果存在说明 target - nums[i] 所在的索引和 i 就是要找的两个数。
* 该方法的时间复杂度为 O(N),空间复杂度为 O(N),使用空间来换取时间。
*
* @param nums
* @param target
* @return
*/
public int[] twoSum_2(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
return new int[]{map.get(target - nums[i]), i};
} else {
map.put(nums[i], i);
}
}
return null;
}
/**
* 暴力,时间复杂度为 O(N^2)
*
* @param nums
* @param target
* @return
*/
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[i] + nums[j] == target) {
return new int[]{i, j};
}
}
}
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两数之和
Given an array of integers, return indices of the two numbers such that they add up to a specific t ...
- [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,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- vcftools报错:Writing PLINK PED and MAP files ... Error: Could not open temporary file.解决方案
一般来说有两种解决方案. 第一种:添加“--plink-tped”参数: 用vcftools的“--plink”参数生成plink格式文件时,小样本量测试可以正常生成plink格式,用大样本量时产生W ...
- Dubbo新版管控台
地址:https://github.com/apache/incubator-dubbo-ops 下载下来,解压 打开cmd 注意:它的前端用到了Vue.js,打包需要npm,所以你要有node.js ...
- ElasticSearch6.3.2------入门
先去官网下载,方便测试用的Windows版本的 都解压了 --- 启动ElasticSearch和Kibana [E:\elasticsearch-]$ .\bin\elasticsearch.bat ...
- Gym 101911E "Painting the Fence"(线段树区间更新+双端队列)
传送门 题意: 庭院中有 n 个围栏,每个围栏上都被涂上了不同的颜色(数字表示): 有 m 条指令,每条指令给出一个整数 x ,你要做的就是将区间[ x第一次出现的位置 , x最后出现的位置 ]中的围 ...
- (基础 输入方法 栈)P1427 小鱼的数字游戏 洛谷
题目描述 小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字(长度不一定,以0结束,最多不超过100个,数字不超过2^32-1),记住了然后反着念出来(表示结束的数字0就不要念出来了).这对小鱼的 ...
- [USACO13FEB] Tractor
题目链接 大家的 Blog 里面都是做过的题目,只有我的里面什么都没有 那我也开始写一点吧 刷着刷着 DP 不知怎的就出来一道这个题……用时 2 hours 后怒而删两个文件重构…… 然后过了……过了 ...
- Altium Designer 18 ------ 原理图和PCB元器件互相查找
方法一:单击选中原理图中元器件,然后单击tools>Seclect PCBcomponents,即可在PCB中看到该器件的高亮显示: 方法二:单击Tools>Cross Select Mo ...
- MySQL数据库优化_索引
1.添加索引后减少查询需要的行数,提高查询性能 (1) 建表 CREATE TABLE `site_user` ( `id` ) NOT NULL AUTO_INCREMENT COMMENT '自增 ...
- thinkpad yoga 12 / thinkpad s1 yoga / WS860
s 序号 IP地址 MAC地址 主机名 设备商 1 192.168.3.6 34-02-86-29-46-8B Intel(R) Dual Band Wireless-AC 7265 Intel公司/ ...
- Python的基础详情
Python的基础信息 Python是一种动态解释性高级语言 Python即可面向对象,也可以面向过程 解释行语言 无需编译 程序以'行'为单位进行执行 执行速度慢 开发效率快 可跨平台 编译型语言 ...