当前位置:网站首页>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
边栏推荐
- SSO and JWT good article sorting
- Router.use() requires a middleware function but got a Object
- Analysis report on the development trend and prospect scale of silicon intermediary industry in the world and China Ⓩ 2022 ~ 2027
- 硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件
- Vs code set code auto save
- Yarn重启applications记录恢复
- 启动solr报错The stack size specified is too small,Specify at least 328k
- 【开发大杀器】之Idea
- Global and Chinese polypropylene industry prospect analysis and market demand forecast report Ⓝ 2022 ~ 2027
- 路由基础之OSPF LSA详细讲解
猜你喜欢

Function test process in software testing

学历、长相、家境普通的人,未来的发展方向是什么?00后的职业规划都已经整得明明白白......

8 popular recommended style layout

Simple two ball loading

VM虚拟机配置动态ip和静态ip访问

一款Flutter版的记事本

华为HMS Core携手超图为三维GIS注入新动能

不同的测试技术区分

Fiori applications are shared through the enhancement of adaptation project

5. Use of ly tab plug-in of header component
随机推荐
新手准备多少钱可以玩期货?农产品可以吗?
Jenkins+webhooks-多分支参数化构建-
shell脚本导入存储过程到数据库
Wave animation color five pointed star loader loading JS special effects
Function test process in software testing
Asp. NETCORE uses dynamic to simplify database access
SVG钻石样式代码
商汤科技崩盘 :IPO时已写好的剧本
During Oracle CDC data transmission, the CLOB type field will lose its value during update. There is a value before update, but
Machine learning - performance metrics
Flow management technology
Operator-1 first acquaintance with operator
8 popular recommended style layout
Project deployment is not difficult at all!
JS变色的乐高积木
北斗通信模块 北斗gps模块 北斗通信终端DTU
Three questions about scientific entrepreneurship: timing, pain points and important decisions
Shell script imports stored procedures into the database
Different test techniques
Wang Xing's infinite game ushers in the "ultimate" battle