当前位置:网站首页>1. Sum of two numbers: given an integer array num and an integer target value, please find the two integers whose sum is the target value target in the array and return their array subscripts
1. Sum of two numbers: given an integer array num and an integer target value, please find the two integers whose sum is the target value target in the array and return their array subscripts
2022-07-01 13:21:00 【? abc!】
subject
Given an array of integers nums And an integer target value target, Please find... In the array And is the target value target the Two Integers , And return their array subscripts .
You can assume that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer .
You can return the answers in any order .
Example 1:
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
explain : because nums[0] + nums[1] == 9 , return [0, 1] .
Example 2:
Input :nums = [3,2,4], target = 6
Output :[1,2]
Example 3:
Input :nums = [3,3], target = 6
Output :[0,1]
Tips :
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
There will only be one valid answer
Ideas
Standard can use double for loop , But the time complexity is relatively high ; So you can Use a tag value , When and are the target content , You can return directly to .
Code
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(target - nums[i])){
return new int[]{
map.get(target - nums[i]),i};
}
map.put(nums[i],i);
}
return new int[0];
}
}
Code instructions :
leetcode The standard answer inside is to use hashTable, such map It's thread safe , But relatively speaking, the performance is poor ;
Map About : The data in the array and the position in the array where the data is located , Preservation , Set the data to key, The position is set to value; This can ensure that the data in map Do not repeat in
Use one layer for loop effect : Traverse all the data in the array for In the cycle
if The role of : Use containsKey Judge the target content (target) Subtract the current loop to data , Whether it already exists map Collection , If it exists, return to the current for Cycle to the coordinates and map The corresponding in the set value, This value is the evaluated value
边栏推荐
- Flinkcdc should extract Oracle in real time. What should be configured for oracle?
- Nexus builds NPM dependent private database
- Flow management technology
- 9. Use of better scroll and ref
- leetcode 322. Coin change (medium)
- 北斗通信模块 北斗gps模块 北斗通信终端DTU
- PG basics -- Logical Structure Management (trigger)
- Fiori applications are shared through the enhancement of adaptation project
- Vs code setting Click to open a new file window without overwriting the previous window
- Google Earth engine (GEE) - Global Human Settlements grid data 1975-1990-2000-2014 (p2016)
猜你喜欢

A Fletter version of Notepad

启动solr报错The stack size specified is too small,Specify at least 328k

9. Use of better scroll and ref

逆向调试入门-PE结构-输入表输出表05/07

Nexus builds NPM dependent private database

【机器学习】VAE变分自编码器学习笔记

Vs code setting Click to open a new file window without overwriting the previous window

Qtdeisgner, pyuic detailed use tutorial interface and function logic separation (nanny teaching)

The stack size specified is too small, specify at least 328k

Professor Li Zexiang, Hong Kong University of science and technology: I'm wrong. Why is engineering consciousness more important than the best university?
随机推荐
Global and Chinese silicone defoamer production and marketing demand and investment forecast analysis report Ⓨ 2022 ~ 2027
A Fletter version of Notepad
Yarn重启applications记录恢复
Qtdeisgner, pyuic detailed use tutorial interface and function logic separation (nanny teaching)
Analysis report on production and marketing demand and investment forecast of global and Chinese diamond powder industry Ⓤ 2022 ~ 2027
【开发大杀器】之Idea
spark源码(五)DAGScheduler TaskScheduler如何配合提交任务,application、job、stage、taskset、task对应关系是什么?
6. Wiper part
Investment analysis and prospect prediction report of global and Chinese dimethyl sulfoxide industry Ⓦ 2022 ~ 2028
PG basics -- Logical Structure Management (trigger)
Mysql间隙锁
Ustime wrote a bug
CS5268优势替代AG9321MCQ Typec多合一扩展坞方案
MySQL六十六问,两万字+五十图详解!复习必备
Flow management technology
2.15 summary
Analysis report on the development pattern of China's smart emergency industry and the 14th five year plan Ⓠ 2022 ~ 2028
Three questions about scientific entrepreneurship: timing, pain points and important decisions
codeforces -- 4B. Before an Exam
Fiori applications are shared through the enhancement of adaptation project