当前位置:网站首页>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 .
边栏推荐
- 2020年Android面试题汇总(中级)
- 事件
- Tencent cloud audio and video award-winning evaluation | leave online messages or submit evaluation, win Dajiang UAV /iphone/switch and other awards
- How to install software on ECs is it expensive to rent ECS
- How does ECS build websites? Is it troublesome for ECs to build websites?
- Confluence data center version is nearing its lifecycle
- 5g and industrial Internet
- 查找GBase 8c数据库当前索引?
- ribbon
- apipost接口断言详解
猜你喜欢

What is the data center

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

Introduction to C language custom types (structure, enumeration, union, bit segment)

Introduction à la méthode de descente par Gradient - document d'apprentissage automatique pour les programmeurs de chevaux noirs

SAP MTS/ATO/MTO/ETO专题之十:ETO模式 Q+空模式 未估价库存 策略自定义

少儿编程课程改革后的培养方式

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

SAP mts/ato/mto/eto topic 7: ATO mode 1 m+m mode strategy 82 (6892)

Apipost interface assertion details

少儿编程教育在特定场景中的普及作用
随机推荐
4G industrial VPN router
How does ECS publish websites? What software tools are needed?
"Emergency response practice" logparser log analysis practice
Database answers build standard, answer as required
阿里云新一代云计算体系架构 CIPU 到底是啥?
LeetCode 1662. Check whether two string arrays are equal
How does ECS build websites? Is it troublesome for ECs to build websites?
Let children learn the application essence of steam Education
What are the differences between ECs and virtual hosts? Which is better, ECS or VM?
SAP mts/ato/mto/eto topic 10: ETO mode q+ empty mode unvalued inventory policy customization
LeetCode 1791. Find the central node of the star chart
Black horse programmer machine learning handout: preliminary use of linear regression API
Bi-sql where
Use of go testing framework gomock
What is the new generation cloud computing architecture cipu of Alibaba cloud?
Analyzing the superiority of humanoid robot in the post human era
What does VPS server mean? What is the difference between a VPS server and an ECS?
『渗透基础』Cobalt Strike基础使用入门_Cobalt Strike联动msfconsole
Digital transformation practice of Zheshang Bank
阿里云混合云首席架构师张晓丹:政企混合云技术架构的演进和发展