当前位置:网站首页>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
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
边栏推荐
- 如何评价科大讯飞AI翻译笔P20系列,值得买吗?
- Deploy web using gunicorn Py application
- String类的常用方法
- C#_ Convert camera images to bitmap format and draw Crosshairs
- Analysis of the factors affecting the transmission signal of the conductive slip ring
- 深入Go底层原理,重写Redis中间件实战无密
- 量子机器学习的基础和应用:一个简明文献综述
- 2021 CCPC 哈尔滨 E. Power and Modulo (思维题)
- 18. `bs object Node name next_ sibling` previous_ Sibling get sibling node
- 期末复习【微机原理】
猜你喜欢

String字符串的存储原理

fastadmin后台设置单选按钮

A Japanese Cherry sold at a sky high price of 1980 yuan. Netizen: I feel cheated after eating it

LSF bsub command

. NETCORE unified authentication authorization learning - run (1)

Alibaba cloud released the atlas of China's robot industry (2022), 122 Pages pdf
![Navigation [microcomputer principle]](/img/79/8311a409113331e72f650a83351b46.png)
Navigation [microcomputer principle]
[cloud native practice] kubesphere practice - Multi tenant system practice

Stm32cubemx learning (6) external interrupt experiment
【云原生实战】KubeSphere实战——多租户系统实战
随机推荐
CORDIC based Signal Processor desgn
Cmake development - Multi Directory Project
Lexin interview process
跳转打开新窗口
Selection of materials for space conductive disc slip ring
Special training of C language array
"Xiaodeng" ad domain delegation for operation and maintenance
Coreldraw2022 new version v24.1.0.360 update
C#_ Convert camera images to bitmap format and draw Crosshairs
每周招聘|DBA数据工程师,年薪35+ ,梦起九州,星河灿烂!
How to evaluate iFLYTEK AI translation pen P20 series? Is it worth buying?
利用积分商城游戏进行营销需要避免哪些问题出现?
leetcode:724. 寻找数组的中心下标
How to call RFC function of ABAP on premises system directly in SAP BTP ABAP programming environment
Three. JS development: drawing of thick lines
时钟树综合(CTS)
「运维有小邓」Active Directory 密码过期通知功能
Cantata version 9.5 has officially passed the sgs-t Ü V certification and conforms to all major software safety standards
Practical guide to GStreamer application development (V)
Codeforces Global Round 21 C D E