当前位置:网站首页>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
边栏推荐
- Research Report on China's software outsourcing industry investment strategy and the 14th five year plan Ⓡ 2022 ~ 2028
- 图灵奖得主Judea Pearl:最近值得一读的19篇因果推断论文
- 机器学习—性能度量
- Yarn重启applications记录恢复
- nexus搭建npm依赖私库
- The future of game guild in decentralized games
- A Fletter version of Notepad
- 我选的热门专业,四年后成了“天坑”
- Sharing with the best paper winner of CV Summit: how is a good paper refined?
- Operator-1 first acquaintance with operator
猜你喜欢

内容审计技术

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

A Fletter version of Notepad

【开发大杀器】之Idea

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

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

5. Use of ly tab plug-in of header component

nexus搭建npm依赖私库

Colorful five pointed star SVG dynamic web page background JS special effect
Reasons for MySQL reporting 1040too many connections and Solutions
随机推荐
Shangtang technology crash: a script written at the time of IPO
Report on the 14th five year plan and future development trend of China's integrated circuit packaging industry Ⓓ 2022 ~ 2028
Different test techniques
硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件
Wang Xing's infinite game ushers in the "ultimate" battle
Google Earth engine (GEE) - Global Human Settlements grid data 1975-1990-2000-2014 (p2016)
PG基础篇--逻辑结构管理(触发器)
Global and Chinese styrene acrylic lotion polymer development trend and prospect scale prediction report Ⓒ 2022 ~ 2028
MySQL gap lock
股票开户要认识谁?实际上网上开户安全么?
Report on the "14th five year plan" and scale prospect prediction of China's laser processing equipment manufacturing industry Ⓢ 2022 ~ 2028
Analysis report on the development prospect and investment strategy of the global and Chinese laser chip industry Ⓑ 2022 ~ 2027
minimum spanning tree
China NdYAG crystal market research conclusion and development strategy proposal report Ⓥ 2022 ~ 2028
Jenkins+webhooks-多分支参数化构建-
SSO and JWT good article sorting
Wave animation color five pointed star loader loading JS special effects
Idea of [developing killer]
啟動solr報錯The stack size specified is too small,Specify at least 328k
Global and Chinese n-butanol acetic acid market development trend and prospect forecast report Ⓧ 2022 ~ 2028