当前位置:网站首页>01.两数之和
01.两数之和
2022-07-25 17:08:00 【用户5573316】
#01.两数之和
难度:简单
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
# 暴力法
# 思路
双循环破解
外层循环获取 nums[0] 到 nums[length-2] 的 i 索引值
内存循环获取 nums[1]到nums[length-1] 的 j 索引值
判断 nums[i] + nums[j] == target 则返回 [ i , j ]
# 代码
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] arr = new int[2];
for(int i=0;i<nums.length;i++){
for(int j=(i+1);j<nums.length;j++){
if(nums[i]+nums[j] == target){
arr[0] = i;
arr[1] = j;
return arr;
}
}
}
throw new IllegalArgumentException("No two sum solution");
}
}
# 哈希法
# 思路
遍历 nums
通过判断每一次遍历的 key 是否满足 nums[i] - target
满足则返回结果 {map.get(nums[i]-target),i}
每次遍历都 map.put(nums[i], i)
# 代码
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i] - target)) {
return new int[]{map.get(nums[i] - target), i};
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
}
边栏推荐
- 7. Dependency injection
- 第六章 继承
- Is it safe to open a securities account in Huatai VIP account
- After 20 years of agitation, the chip production capacity has started from zero to surpass that of the United States, which is another great achievement made in China
- [book club issue 13] +ffmpeg video capture function
- GTX1080Ti 光纤HDMI干扰出现闪屏1080Ti 闪屏解决方法
- 【目标检测】YOLOv5跑通VOC2007数据集(修复版)
- Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
- Ilssi certification | the course of Six Sigma DMAIC
- Outlook tutorial, how to search for calendar items in outlook?
猜你喜欢

Outlook tutorial, how to search for calendar items in outlook?

为什么 4EVERLAND 是 Web 3.0 的最佳云计算平台

在华为昇腾Ascend910上复现swin_transformer

基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用

QT listview list display component notes

超越 ConvNeXt、RepLKNet | 看 51×51 卷积核如何破万卷!

异常处理机制专题1

大型仿人机器人的技术难点和应用情况

WPF 实现用户头像选择器
![[target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5](/img/be/5348170fb460cbafbdb848d70fea15.png)
[target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5
随机推荐
What are the free low code development platforms?
MySQL linked table query, common functions, aggregate functions
第五章:流程控制
Go语言系列:Go从哪里来,Go将去哪里?
【redis】redis安装
Rainbond插件扩展:基于Mysql-Exporter监控Mysql
【obs】转载:OBS直播严重延迟和卡顿怎么办?
Test framework unittest command line operation and assertion method
在华为昇腾Ascend910上复现swin_transformer
Solution for win10 device management not recognizing gtx1080ti display device
Roson的Qt之旅#99 QML表格控件-TableView
Birui data joins Alibaba cloud polardb open source database community
虚拟内存管理
Chapter V: process control
What is the monthly salary of 10000 in China? The answer reveals the cruel truth of income
[book club issue 13] +ffmpeg open source project
7.依赖注入
使用Huggingface在矩池云快速加载预训练模型和数据集
Outlook 教程,如何在 Outlook 中搜索日历项?
[knowledge atlas] practice -- Practice of question and answer system based on medical knowledge atlas (Part5 end): information retrieval and result assembly