当前位置:网站首页>1. Sum of two numbers (hash table)
1. Sum of two numbers (hash table)
2022-04-23 10:15:00 【Popuessing's Jersey】
subject :
Given an array of integers nums And a target value target, Please find and as the target value in the array Two Integers , And return their array subscripts .
You can assume that each input corresponds to only one answer . however , The same element in an array cannot be used twice .
Example :
Given nums = [2, 7, 11, 15], target = 9
because nums[0] + nums[1] = 2 + 7 = 9
So back [0, 1]
Method : Hash table method
Ideas : Hash table is a method of exchanging space for time , If violent solutions are used , Then the time complexity is O(n^2), If the complexity of the hash table is reduced to time O(n), One traversal completes the task
import java.util.HashMap;
import java.util.Map;
public class Liangshuxiangjia {
public int[] twoSum(int [] nums,int target){
int [] res = new int[2];
// If the array is empty , Return an empty array
if(nums == null || nums.length ==0){
return res;
}
// New hash table
Map<Integer,Integer> map = new HashMap<>();
// Traversal array , Judge map Whether there is a value equal to target-num[i]
// If there is , Returns the value of the current element , as well as target-num[i]
// without , take target-nums[i] The difference is stored in the set
for (int i = 0; i <nums.length; i++) {
int temp = target-nums[i];
if(map.containsKey(temp)){
res[1] = i;
res[0] = map.get(temp);
}
map.put(nums[i],i);
}
return res;
}
public static void main(String[] args) {
int[] nums = {2,5,6,7};
Liangshuxiangjia liangshuxiangjia = new Liangshuxiangjia();
int [] res = liangshuxiangjia.twoSum(nums,9);
for (int x:res) {
System.out.print(x+" ");
}
}
}
Output results :
0 3
Time complexity :O(n)
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231011140735.html
边栏推荐
猜你喜欢

Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022

第120章 SQL函数 ROUND

Six practices of Windows operating system security attack and defense
Detailed explanation of MapReduce calculation process

《Redis设计与实现》

基于PyQt5实现弹出任务进度条功能示例

MapReduce compression

2022茶艺师(初级)考试试题模拟考试平台操作

JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer

Custom login failure handling
随机推荐
2022茶艺师(初级)考试试题模拟考试平台操作
[untitled]
转:毛姆:阅读是一座随身携带的避难所
利用多线程按顺序连续输出abc10次
Yarn资源调度器
0704、ansible----01
杰理之有时候定位到对应地址的函数不准确怎么办?【篇】
杰理之通常程序异常情况有哪些?【篇】
定义链表(链表)
MapReduce核心和基础Demo
Jerry's users how to handle events in the simplest way [chapter]
Zhengda international explains what the Dow Jones industrial index is?
Sim Api User Guide(4)
Configuration of LNMP
ARM调试(1):两种在keil中实现printf重定向到串口的方法
209、长度最小的子数组(数组)
Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
349、两个数组的交集
Realize data value through streaming data integration (3) - real-time continuous data collection
Sim Api User Guide(6)