当前位置:网站首页>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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
边栏推荐
- Logical structure and physical structure
- Information system project manager -- Chapter VII examination questions of project cost management over the years
- Gstreamer应用开发实战指南(五)
- At least 3 years for learning amplifier?
- [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 - first authorization (2)
- Calibration, correction and world coordinate calculation of binocular stereo vision camera (openCV)
- 60天远程办公经验分享 | 社区征文
- Flutter BottomNavigationBar带有页面切换示例
- Exit operation in project
猜你喜欢

广东二级造价工程师《造价管理》真题解析

LSF-bsub命令

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

"Operation and maintenance department has Xiao Deng" to review and analyze file and folder access rights

《强化学习周刊》第51期:PAC、ILQL、RRL&无模型强化学习集成于微电网络格控制:综述与启示

mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总

STL tutorial 6-deque, stack, queue, list container

Advances in computational imaging

PostgreSQL每周新闻—6月22日

Three. JS development: drawing of thick lines
随机推荐
Go: how to write a correct UDP server
利用积分商城游戏进行营销需要避免哪些问题出现?
Sentinel's quick start takes you through flow control in three minutes
0/1 score planning topic
阿里云发布《中国机器人产业图谱(2022)》,122页pdf
Defense cornerstone in attack and defense drill -- all-round monitoring
leetcode:238. 除自身以外数组的乘积
Calibration, correction and world coordinate calculation of binocular stereo vision camera (openCV)
VoIP Push 在海外音视频业务中的应用
Basic qualities of management personnel
计算成像前沿进展
Go deep into the underlying principles of go and rewrite redis middleware without secret
THREEJS基础入门
WPF measurement string display size
Uncover the secret! Pay attention to those machines under the membership system!
What problems should be avoided when using the points mall games for marketing?
VHDL电话计费系统设计
Bigder:自动化测试工程师
Exit operation in project
TMUX settings