当前位置:网站首页>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");
}
}
边栏推荐
猜你喜欢

虚拟内存管理

【南京航空航天大学】考研初试复试资料分享

企业直播风起:目睹聚焦产品,微赞拥抱生态

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

Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project

基于redis6.2.4的redis cluster部署

Chapter III data types and variables

How to delete Microsoft Pinyin input method in win10

Customize MVC project login registration and tree menu
![[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part4): problem analysis and retrieval sentence generation combined with problem classification](/img/22/01297d28e5bfb105fc65ee29248a7c.png)
[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part4): problem analysis and retrieval sentence generation combined with problem classification
随机推荐
unity 最好用热更方案卧龙 wolong
[Nanjing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
Rainbow plug-in extension: monitor MySQL based on MySQL exporter
【obs】转载:OBS直播严重延迟和卡顿怎么办?
【redis】redis安装
From digitalization to intelligent operation and maintenance: what are the values and challenges?
备考过程中,这些“谣言”千万不要信!
In the eyes of 100 users, there are 100 QQS
2022年最新北京建筑施工焊工(建筑特种作业)模拟题库及答案解析
2022 latest Beijing Construction welder (construction special operation) simulation question bank and answer analysis
在华为昇腾Ascend910上复现swin_transformer
Use huggingface to quickly load pre training models and datasets in moment pool cloud
如何使用 4EVERLAND CLI 在 IPFS 上部署应用程序
[OBS] frame loss and frame priority before transmission
ReBudget:通过运行时重新分配预算的方法,在基于市场的多核资源分配中权衡效率与公平性
C # introductory basic tutorial
win10如何删除微软拼音输入法
[redis] redis installation
WPF implements user avatar selector
Enumeration classes and magic values