当前位置:网站首页>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
边栏推荐
- Mysql间隙锁
- [Niu Ke's questions -sql big factory interview real questions] no2 User growth scenario (a certain degree of information flow)
- spark源码(五)DAGScheduler TaskScheduler如何配合提交任务,application、job、stage、taskset、task对应关系是什么?
- 不同的测试技术区分
- The future of game guild in decentralized games
- Redis exploration: cache breakdown, cache avalanche, cache penetration
- Analysis report on the development prospect and investment strategic planning of China's wafer manufacturing Ⓔ 2022 ~ 2028
- 硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件
- Project deployment is not difficult at all!
- MySQL Replication中的并行复制示例详解
猜你喜欢
Reasons for MySQL reporting 1040too many connections and Solutions

nexus搭建npm依赖私库

Wave animation color five pointed star loader loading JS special effects

Look at the sky at dawn and the clouds at dusk, and enjoy the beautiful pictures

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

游戏公会在去中心化游戏中的未来

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

Fiori applications are shared through the enhancement of adaptation project

Nexus builds NPM dependent private database

mysql统计账单信息(下):数据导入及查询
随机推荐
Analysis report on the development trend and Prospect of new ceramic materials in the world and China Ⓐ 2022 ~ 2027
spark源码阅读总纲
【牛客刷题-SQL大厂面试真题】NO2.用户增长场景(某度信息流)
Zabbix 6.0 源码安装以及 HA 配置
Introduction to reverse debugging PE structure input table output table 05/07
Meta enlarge again! VR new model posted on CVPR oral: read and understand voice like a human
Mysql间隙锁
软件测试中功能测试流程
内容审计技术
Svg diamond style code
The popular major I chose became "Tiankeng" four years later
Introduction to topological sorting
Project deployment is not difficult at all!
Detailed explanation of OSPF LSA of routing Foundation
SSO and JWT good article sorting
Asp. NETCORE uses dynamic to simplify database access
Analysis report on production and marketing demand and investment forecast of global and Chinese diamond powder industry Ⓤ 2022 ~ 2027
Sharing with the best paper winner of CV Summit: how is a good paper refined?
CV顶会最佳论文得主分享:好论文是怎么炼成的?
Content Audit Technology