当前位置:网站首页>Leetcode (question 1) - sum of two numbers
Leetcode (question 1) - sum of two numbers
2022-06-24 04:56:00 【Xiaobai, a vegetable seller】
One 、 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 .
Two 、 Example
// 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]
3、 ... and 、 Their thinking
The idea of solving this problem is to do double traversal , Pass during traversal if Judge whether the sum of two numbers is equal to target equal . If equal, it returns directly , If they are not equal, they will traverse again .
/** * @param {number[]} nums * @param {number} target * @return {number[]} */
var twoSum = function(nums, target) {
let length = nums.length
for(let i = 0; i < length; i++) {
for(let j = i + 1; j < length; j++) {
if(nums[i] + nums[j] === target) {
return [i, j]
}
}
}
};

At this point, we can see that the time complexity is relatively high , by O(n ^ 2), The space complexity is O(1).
Four 、 expand
We can increase the time complexity to O(n ^ 2) within , At this point, we can use the hash table to solve , You can traverse every element nums[i], And then determine target - nums[i] Exists in the hash table , If there is , Then the array is returned directly , If it doesn't exist , Then put the element and its subscript into the hash table .
/** * @param {number[]} nums * @param {number} target * @return {number[]} */
var twoSum = function(nums, target) {
let map = new Map()
let length = nums.length
for(let i = 0; i < length; i++) {
if(map.has(target - nums[i])) {
return [map.get(target - nums[i]), i]
}
map.set(nums[i], i)
}
};

At this point, we can see that the time complexity is O(n), The space complexity is O(n), The space complexity may be high , However, it does not violate the idea of exchanging space for time .
边栏推荐
- What is Ping? How can the server disable Ping?
- Elfk service setup
- Critical service failed
- Spirit breath development log (16)
- When remote, your resolution is lower than a × B. Some items may not be displayed on the screen
- Precautions for online education and training industry filing
- 『应急响应实践』LogParser日志分析实践
- How to build an ECS and how to control the server through the local host
- Inventory of common tools used by dry goods | data journalists
- How to restart the ECS? What are the differences between ECS restart and normal computers?
猜你喜欢

一款支持内网脱机分享文档的接口测试软件

SAP MTS/ATO/MTO/ETO专题之八:ATO模式2 D+空模式策略用85

Facebook internal announcement: instant messaging will be re integrated

『渗透基础』Cobalt Strike基础使用入门_Cobalt Strike联动msfconsole

SAP MTS/ATO/MTO/ETO专题之十:ETO模式 Q+空模式 未估价库存 策略自定义
Summary of Android interview questions in 2020 (intermediate)

MySQL - SQL execution process

External network access SVN server (external network access SVN server deployed on the cloud)

Detailed explanation of tcpip protocol

Abnova membrane protein lipoprotein solution
随机推荐
SAP mts/ato/mto/eto topic 10: ETO mode q+ empty mode unvalued inventory policy customization
How to file ECS? What should be paid attention to when selecting ECS
Use of golang testing framework goshub
Bi-sql distinct
How does the VPS server upload data? Is the VPS server free to use?
What is an ECS? What is the difference between ECs and traditional servers?
Loss and optimization of linear regression, machine learning to predict house prices
解析90后创客教育的主观积极性
Jimureport building block report - expression introduction
LeetCode 1662. Check whether two string arrays are equal
An interface testing software that supports offline document sharing in the Intranet
What does VPS server mean? What is the difference between a VPS server and an ECS?
Functional advantages of industrial wireless router
少儿编程课程改革后的培养方式
LeetCode 1791. Find the central node of the star chart
Spirit breath development log (15)
apipost接口断言详解
External network access SVN server (external network access SVN server deployed on the cloud)
How to select a suitable optical fiber tester
黑马程序员机器学习讲义:线性回归api初步使用