当前位置:网站首页>力扣-两数之和
力扣-两数之和
2022-07-01 03:02: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]
二、解法1
public int[] twoSum1(int[] nums, int target) {
int[] indexs = new int[2];
// 双重循环 循环极限为(n^2-n)/2
for (int i = 0; i < nums.length; i++) {
for (int j = nums.length - 1; j > i; j--) {
if (nums[i] + nums[j] == target) {
indexs[0] = i;
indexs[1] = j;
System.out.println(Arrays.toString(indexs));
return indexs;
}
}
}
return indexs;
}三、解法2
public int[] twoSum3(int[] nums, int target) {
int[] indexs = new int[2];
// 建立k-v ,一一对应的哈希表
HashMap<Integer, Integer> hash = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (hash.containsKey(nums[i])) {
indexs[0] = i;
indexs[1] = hash.get(nums[i]);
System.out.println(Arrays.toString(indexs));
return indexs;
}
// 将数据存入 key为补数 ,value为下标
hash.put(target - nums[i], i);
}
return indexs;
}边栏推荐
- # 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
- Druid monitoring statistics source
- CX5120控制汇川IS620N伺服报错E15解决方案
- Use ipmitool to configure BMC network and user information of X86 server
- Mnasnet learning notes
- Mouse over effect 10
- [exsi] transfer files between hosts
- Redis分布式锁的8大坑
- Here comes the share creators budding talent training program!
- Restcloud ETL WebService data synchronization to local
猜你喜欢

Const and the secret of pointers

【小程序项目开发-- 京东商城】uni-app之首页商品楼层

通信协议——分类及其特征介绍
![Lavaweb [first understanding the solution of subsequent problems]](/img/8a/08cb2736c2c198d926dbe00c004c3f.png)
Lavaweb [first understanding the solution of subsequent problems]

Xception learning notes

How to verify whether the contents of two files are the same

UE4 rendering pipeline learning notes
![[linear DP] longest common subsequence](/img/47/c3172422e997009facbada929adb1a.jpg)
[linear DP] longest common subsequence

kubernetes资源对象介绍及常用命令(二)

Here comes the share creators budding talent training program!
随机推荐
[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)
If a parent class defines a parameterless constructor, is it necessary to call super ()?
Restcloud ETL WebService data synchronization to local
[exsi] transfer files between hosts
POI导出excel,按照父子节点进行分级显示
Mouse over effect I
Best used trust automation script (shell)
Mouse over effect II
HTB-Lame
How to determine the progress bar loaded in the loading interface when opening the game
访问url 404 的错误
彻底解决Lost connection to MySQL server at ‘reading initial communication packet
lavaweb【初识后续问题的解决】
Mouse over effect III
[applet project development -- Jingdong Mall] classified navigation area of uni app
Use ipmitool to configure BMC network and user information of X86 server
Chapitre 03 Bar _ Gestion des utilisateurs et des droits
Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
Is it safe to open a stock account? Shanghai stock account opening procedures.
第03章_用户与权限管理