当前位置:网站首页>LeetCode 1. 两数之和
LeetCode 1. 两数之和
2022-06-29 20:54:00 【碳烤小肥羊。。。】
题目描述:给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:
输入:nums = [3,2,4], target = 6
输出:[1,2]
示例 3:
输入:nums = [3,3], target = 6
输出:[0,1]
提示:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
只会存在一个有效答案
进阶:你可以想出一个时间复杂度小于 O(n2) 的算法吗?
方法一:暴力破解
public static int[] twoSum1(int[] nums, int target){
int[] rs = new int[2];
for(int i = 0; i < nums.length-1; i++){
for(int j = i+1; j < nums.length; j++){
if(nums[i] + nums[j] == target){
rs[0] = i;
rs[1] = j;
return rs;
}
}
}
return rs;
}
方法一:使用Map集合
- 首先声明一个Map集合,其中key存储nums[i]的值, value存储nums[i]的下标i。
- 遍历nums数组,判断target - nums[i]是否存在map集合中:
如果不在map集合中,就把<nums[i], i>存入map集合中;
如果在map集合中,那么map.get(target - nums[i]) 和 i 就是本题所需的一组下标。
public static int[] twoSum(int[] nums, int target){
int[] rs = new int[2];
if(nums == null || nums.length == 0){
return rs;
}
// 声明一个map集合
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
int temp = nums[i];
if(map.containsKey(target - temp)){
rs[0] = map.get(target - temp);
rs[1] = i;
return rs;
}
map.put(temp, i);
}
return rs;
}
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/two-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
边栏推荐
- Basic qualities of management personnel
- 0/1 score planning topic
- 空间导电盘式滑环材料的选择
- 阿里云发布《中国机器人产业图谱(2022)》,122页pdf
- Set up your own website (12)
- "Operation and maintenance department has Xiao Deng" to review and analyze file and folder access rights
- STL教程6-deque、stack、queue、list容器
- Practical guide to GStreamer application development (V)
- 2021 CCPC Harbin J. local minimum (thinking question)
- 导航【微机原理】
猜你喜欢
![[a must for reptiles - > scrapy framework from black iron to king] first chapter - detailed explanation of 10000 character blog posts (recommended Collection)](/img/f3/a45fc96054e42725e491c3599c527f.jpg)
[a must for reptiles - > scrapy framework from black iron to king] first chapter - detailed explanation of 10000 character blog posts (recommended Collection)

. NETCORE unified authentication authorization learning - run (1)

Win7 easy connect 提示:选路连接失败,可能当前连接网络异常,请稍后重试

数字密码锁verilog设计+仿真+上板验证

Stm32cubemx learning (6) external interrupt experiment

「运维有小邓」实时监控用户登录操作

一颗新的北极星已经升起!

"Xiaodeng" active directory password expiration notification function is available for operation and maintenance

Win10 sets automatic dial-up networking task to realize automatic reconnection after startup and disconnection

双目立体视觉摄像头的标定、矫正、世界坐标计算(opencv)
随机推荐
Calibration, correction and world coordinate calculation of binocular stereo vision camera (openCV)
Lexin interview process
一次 Keepalived 高可用的事故,让我重学了一遍它!
. NETCORE unified authentication authorization learning - first authorization (2)
高校如何基于云原生构建面向未来的智慧校园?全栈云原生VS传统技术架构
0/1 score planning topic
ads131a04 ADC verilog实现及仿真
WIN10设置自动拨号联网任务,实现开机、断网自动重连
How to call RFC function of ABAP on premises system directly in SAP BTP ABAP programming environment
Final review [microcomputer principle]
Coreldraw2022 new version v24.1.0.360 update
Analysis on the true topic of "cost management" by Guangdong second-class cost engineer
What are the mainstream brands of smart door locks? What characteristics should we pay attention to when purchasing door locks?
时钟树综合(CTS)
2021 CCPC Harbin E. power and modulo (thinking questions)
Analysis of the underlying architecture of spark storage system - spark business environment practice
verilog实现串口通信发送到数码管
"Xiaodeng" ad domain delegation for operation and maintenance
Enter the year and month to find the total number of days in the month
「运维有小邓」实时监控用户登录操作