当前位置:网站首页>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 .
边栏推荐
- 期末复习(Day5)
- [teacher Zhao Yuqiang] MySQL flashback
- [together Shangshui Shuo series] day 7 content +day8
- [teacher Zhao Yuqiang] Alibaba cloud big data ACP certified Alibaba big data product system
- Introduction to redis using Lua script
- 期末复习(DAY6)
- [teacher Zhao Yuqiang] index in mongodb (Part 1)
- Skywalking8.7 source code analysis (I): agent startup process, agent configuration loading process, custom class loader agentclassloader, plug-in definition system, plug-in loading
- 1. 两数之和
- Exception when introducing redistemplate: noclassdeffounderror: com/fasterxml/jackson/core/jsonprocessingexception
猜你喜欢
![[teacher Zhao Yuqiang] the most detailed introduction to PostgreSQL architecture in history](/img/18/f91d3d21a39743231d01f2e4015ef8.jpg)
[teacher Zhao Yuqiang] the most detailed introduction to PostgreSQL architecture in history

Sophomore dilemma (resumption)

pytorch DataLoader实现miniBatch(未完成)

pytorch 多分类中的损失函数

伯努利分布,二项分布和泊松分布以及最大似然之间的关系(未完成)

Linux登录MySQL出现ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
![[Shangshui Shuo series together] day 10](/img/a3/e8b9df588bef67ead925813a75c8c0.png)
[Shangshui Shuo series together] day 10
![[advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation](/img/9b/a309607c037b0a18ff6b234a866f9f.jpg)
[advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation

How to create and configure ZABBIX

Skywalking8.7 source code analysis (II): Custom agent, service loading, witness component version identification, transform workflow
随机推荐
2022.7.2 simulation match
Ensemble, série shuishu] jour 9
一起上水硕系列】Day 9
2022.DAY592
Final review (Day7)
[function explanation (Part 2)] | [function declaration and definition + function recursion] key analysis + code diagram
[video of Teacher Zhao Yuqiang's speech on wot] redis high performance cache and persistence
Configure DTD of XML file
There is no one of the necessary magic skills PXE for old drivers to install!!!
Personal outlook | looking forward to the future from Xiaobai's self analysis and future planning
[teacher Zhao Yuqiang] Cassandra foundation of NoSQL database
Analysis of the example of network subnet division in secondary vocational school
2022.7.2day594
[teacher Zhao Yuqiang] Flink's dataset operator
Final review (Day6)
【无标题】
1. Sum of two numbers
Why should there be a firewall? This time xiaowai has something to say!!!
How to create your own repository for software packages on Debian
Using the ethtool command by example