当前位置:网站首页>Leetcode solution - 01 Two Sum
Leetcode solution - 01 Two Sum
2022-07-03 05:58:00 【Ashley shot the sun】
Leetcode The first 01. Two Sum topic , questions Easy.
One . Subject requirements
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Two . Their thinking & Code implementation
There are three solutions to this problem
1. Brute force
We can see the pairwise combination of all array elements , Find the index of the combination that meets the condition , Feasible but slow , No discussion here .
2. Array traversal
From 0 Elements start , Add it to the following elements in turn to see whether the conditions are met , If it is satisfied, the corresponding index is returned . In the worst case, we need to start from 0 Traversing n-1, The time complexity is 𝑛(𝑛−1)2, Performance is still very low .
3. HashMap The way
The final solution here is to adopt Space for time The way , The introduction of a HashMap Record the index and value in the array (index,value), If the subsequent traversal Map Included value Satisfy :target - value , Then the conditions are met , Just return the corresponding index , The code is as follows :
class Solution {
public int[] twoSum(int[] nums, int target) {
int length = nums.length;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i< length; i ++) {
int num = nums[i];
if (map.containsKey(target - num)) {
return new int[]{
map.get(target - num), i};
}
map.put(num, i);
}
return new int[]{
0, 0};
}
}
- Running results
Runtime: 1 ms, faster than 99.97% of Java online submissions for Two Sum.
Lao tie saw this and gave a wave of praise 、 Comment on 、 How about paying attention to Sanlian
I am a AhriJ Zou classmate , Front and rear ends 、 Applet 、DevOps Are engaged in the explosion of stack engineers . The blog is constantly updated , If you think it's good , Welcome to the old tiesanlian , If it's not good, you're welcome to correct , learn from each other , Common progress .
边栏推荐
- BeanDefinitionRegistryPostProcessor
- Maximum likelihood estimation, divergence, cross entropy
- 2022.7.2day594
- Error 1045 (28000) occurs when Linux logs in MySQL: access denied for user 'root' @ 'localhost' (using password: yes)
- Understand expectations (mean / estimate) and variances
- [teacher Zhao Yuqiang] the most detailed introduction to PostgreSQL architecture in history
- [explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis
- [branch and cycle] | | super long detailed explanation + code analysis + a trick game
- How does win7 solve the problem that telnet is not an internal or external command
- If function of MySQL
猜你喜欢
![[explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis](/img/df/884313a69fb1e613aec3497800f7ba.jpg)
[explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis

一起上水碩系列】Day 9

pytorch 搭建神经网络最简版

项目总结--04

Redhat7 system root user password cracking
![[teacher Zhao Yuqiang] RDB persistence of redis](/img/cc/5509b62756dddc6e5d4facbc6a7c5f.jpg)
[teacher Zhao Yuqiang] RDB persistence of redis

大二困局(复盘)

为什么网站打开速度慢?

【一起上水硕系列】Day 7 内容+Day8

Exception when introducing redistemplate: noclassdeffounderror: com/fasterxml/jackson/core/jsonprocessingexception
随机推荐
1. 兩數之和
2022.7.2day594
Deep learning, thinking from one dimensional input to multi-dimensional feature input
[minesweeping of two-dimensional array application] | [simple version] [detailed steps + code]
最大似然估计,散度,交叉熵
中职网络子网划分例题解析
Detailed explanation of findloadedclass
redis 无法远程连接问题。
BeanDefinitionRegistryPostProcessor
Use telnet to check whether the port corresponding to the IP is open
[branch and cycle] | | super long detailed explanation + code analysis + a trick game
Final review Day8
[advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation
PHP笔记超详细!!!
Alibaba cloud Alipay sandbox payment
chromedriver对应版本下载
期末复习(Day5)
一起上水碩系列】Day 9
Redhat7系统root用户密码破解
BeanDefinitionRegistryPostProcessor