当前位置:网站首页>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 an evpn switch?
- apipost接口断言详解
- Disaster recovery series (IV) - disaster recovery construction of business application layer
- Pg-pool-ii read / write separation experience
- 什么是数据中台
- Mini web framework: adding routes in decorator mode | dark horse programmer
- How to enlarge the ECS page? How to select ECS instance specifications?
- Develop a customized music player from scratch, and your girlfriend will have it?
- 2020年Android面试题汇总(初级)
- Find the current index of gbase 8C database?
猜你喜欢

Abnova fluorescence in situ hybridization (FISH) probe solution

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

外网访问svn服务器(外网访问部署在云上的svn服务器)

Idea创建Servlet 后访问报404问题

重新认识WorkPlus,不止IM即时通讯,是企业移动应用管理专家

Introduction to gradient descent method - black horse programmer machine learning handout

『渗透基础』Cobalt Strike基础使用入门_Cobalt Strike联动msfconsole
uni-app进阶之认证【day12】

Final summary of freshman semester (supplement knowledge loopholes)

阿里云混合云首席架构师张晓丹:政企混合云技术架构的演进和发展
随机推荐
What are the advantages of ECS? Is ECS better than VM?
Disaster recovery series (IV) - disaster recovery construction of business application layer
5g and industrial Internet
Qiming cloud sharing: tips on esp32c3 simple IO and serial port
There are many ways to confirm and modify the remote port number
How to add a domain name to ECS? What are the advantages of ECS?
Precautions for online education and training industry filing
Many regulations come into effect today! The main responsibility of network security will be further implemented
Ext4 file system jam caused by MEM CGroup OOM
4G industrial VPN router
让孩子们学习Steam 教育的应用精髓
mini-Web框架:装饰器方式的添加路由 | 黑马程序员
getAttribute 返回值为null
Advanced authentication of uni app [Day12]
When remote, your resolution is lower than a × B. Some items may not be displayed on the screen
How are ECS leased? Can the ECS use VPN?
What does VPS server mean? What is the difference between a VPS server and an ECS?
Weak current engineer, 25g Ethernet and 40g Ethernet: which do you choose?
Idea创建Servlet 后访问报404问题
Naming of tables in MySQL