当前位置:网站首页>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
边栏推荐
- Shell script imports stored procedures into the database
- Yarn重启applications记录恢复
- Wave animation color five pointed star loader loading JS special effects
- Qtdeisgner, pyuic detailed use tutorial interface and function logic separation (nanny teaching)
- 6. Wiper part
- 北斗通信模块 北斗gps模块 北斗通信终端DTU
- China NdYAG crystal market research conclusion and development strategy proposal report Ⓥ 2022 ~ 2028
- Who should I know when opening a stock account? Is it actually safe to open an account online?
- mysql统计账单信息(下):数据导入及查询
- ROS2 Foxy depthai_ ROS tutorial
猜你喜欢
![[Niu Ke's questions -sql big factory interview real questions] no2 User growth scenario (a certain degree of information flow)](/img/a0/e9e7506c9c34986dc73562539c8410.png)
[Niu Ke's questions -sql big factory interview real questions] no2 User growth scenario (a certain degree of information flow)

Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS

I spent tens of thousands of dollars to learn and bring goods: I earned 3 yuan in three days, and the transaction depends on the bill

Computer network interview knowledge points

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

shell脚本导入存储过程到数据库

Fiori 应用通过 Adaptation Project 的增强方式分享

Flow management technology

我选的热门专业,四年后成了“天坑”

Vs code set code auto save
随机推荐
Investment analysis and prospect prediction report of global and Chinese p-nitrotoluene industry Ⓙ 2022 ~ 2027
软件测试中功能测试流程
Terminal identification technology and management technology
5. Use of ly tab plug-in of header component
Will it affect the original MySQL database to read the data of a MySQL table in full by flick MySQL CDC
【机器学习】VAE变分自编码器学习笔记
Google Earth engine (GEE) - Global Human Settlements grid data 1975-1990-2000-2014 (p2016)
MySQL Replication中的并行复制示例详解
A Fletter version of Notepad
Reasons for MySQL reporting 1040too many connections and Solutions
Function test process in software testing
Judea pearl, Turing prize winner: 19 causal inference papers worth reading recently
Localtime can't re-enter. It's a pit
MySQL报错1040Too many connections的原因以及解决方案
VM virtual machine configuration dynamic IP and static IP access
Idea of [developing killer]
Meta再放大招!VR新模型登CVPR Oral:像人一样「读」懂语音
ZABBIX 6.0 source code installation and ha configuration
Router. use() requires a middleware function but got a Object
Wang Xing's infinite game ushers in the "ultimate" battle