当前位置:网站首页>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 .
边栏推荐
- How to install software on ECs is it expensive to rent ECS
- Mini web framework: adding routes in decorator mode | dark horse programmer
- Verifying data models in golang
- Are you ready for the exam preparation strategy of level II cost engineer in 2022?
- 提pr,push 的时候网络超时配置方法
- Before creating an image, it is recommended to execute the following code to purify the image as an administrator
- Facebook internal announcement: instant messaging will be re integrated
- Naming of tables in MySQL
- apipost接口断言详解
- Bi-sql basic cognition
猜你喜欢

C语言自定义类型的介绍(结构体,枚举,联合体,位段)

Abnova membrane protein lipoprotein solution

2022年二级造价工程师备考攻略,你准备好了吗?

解析后人类时代类人机器人的优越性

什么是数据中台

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

少儿编程教育在特定场景中的普及作用
Summary of Android interview questions in 2020 (intermediate)

TCPIP协议详解

『渗透基础』Cobalt Strike基础使用入门_Cobalt Strike联动msfconsole
随机推荐
When remote, your resolution is lower than a × B. Some items may not be displayed on the screen
Apipost interface assertion details
Bi-sql - Select
什么是数据中台
线性回归的损失和优化,机器学习预测房价
RedHat 8 time synchronization and time zone modification
ribbon
MySQL - SQL execution process
Naming of tables in MySQL
LeetCode 1290. Binary linked list to integer
Find the current index of gbase 8C database?
Deep learning common optimizer summary
Problem: SQL create stored procedure
Before creating an image, it is recommended to execute the following code to purify the image as an administrator
Analyze the actual user groups and demand positioning of distributed database products from the market and demand
重新认识WorkPlus,不止IM即时通讯,是企业移动应用管理专家
Bi-sql and & or & in
What does VPS server mean? What is the difference between a VPS server and an ECS?
Digital transformation practice of Zheshang Bank
梯度下降法介紹-黑馬程序員機器學習講義