当前位置:网站首页>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
边栏推荐
- codeforces -- 4B. Before an Exam
- Router. use() requires a middleware function but got a Object
- Analysis report on the development trend and Prospect of new ceramic materials in the world and China Ⓐ 2022 ~ 2027
- Idea of [developing killer]
- Investment analysis and prospect prediction report of global and Chinese p-nitrotoluene industry Ⓙ 2022 ~ 2027
- Camp division of common PLC programming software
- Meta再放大招!VR新模型登CVPR Oral:像人一样「读」懂语音
- PG basics -- Logical Structure Management (trigger)
- VM虚拟机配置动态ip和静态ip访问
- Jenkins+webhooks-多分支参数化构建-
猜你喜欢
Redis explores cache consistency
Meta再放大招!VR新模型登CVPR Oral:像人一样「读」懂语音
codeforces -- 4B. Before an Exam
MySQL statistical bill information (Part 2): data import and query
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
简单的两个圆球loading加载
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
minimum spanning tree
JS变色的乐高积木
我花上万学带货:3天赚3元,成交靠刷单
随机推荐
Mysql间隙锁
Different test techniques
oracle cdc 数据传输时,clob类型字段,在update时值会丢失,update前有值,但
5G工业网关的科技治超应用 超限超重超速非现场联合执法
mysql统计账单信息(下):数据导入及查询
C language learning
网络中的listen
【大型电商项目开发】性能压测-压力测试基本概念&JMeter-38
Will it affect the original MySQL database to read the data of a MySQL table in full by flick MySQL CDC
软件测试中功能测试流程
leetcode 322. Coin Change 零钱兑换(中等)
spark源码(五)DAGScheduler TaskScheduler如何配合提交任务,application、job、stage、taskset、task对应关系是什么?
有没有大佬 遇到过flink监控postgresql数据库, 检查点无法使用的问题
Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices
1553B环境搭建
Global and Chinese styrene acrylic lotion polymer development trend and prospect scale prediction report Ⓒ 2022 ~ 2028
Use of shutter SQLite
Class initialization and instantiation
Investment analysis and prospect prediction report of global and Chinese dimethyl sulfoxide industry Ⓦ 2022 ~ 2028
Colorful five pointed star SVG dynamic web page background JS special effect