当前位置:网站首页>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 .
边栏推荐
- MySQL startup error: several solutions to the server quit without updating PID file
- 深度学习,从一维特性输入到多维特征输入引发的思考
- [function explanation (Part 2)] | [function declaration and definition + function recursion] key analysis + code diagram
- NG Textarea-auto-resize
- JS implements the problem of closing the current child window and refreshing the parent window
- Sorry, this user does not exist!
- Method of finding prime number
- Convolution operation in convolution neural network CNN
- Final review (day3)
- 一起上水硕系列】Day 9
猜你喜欢
![[teacher Zhao Yuqiang] Alibaba cloud big data ACP certified Alibaba big data product system](/img/cc/5509b62756dddc6e5d4facbc6a7c5f.jpg)
[teacher Zhao Yuqiang] Alibaba cloud big data ACP certified Alibaba big data product system

Disruptor learning notes: basic use, core concepts and principles
![[teacher Zhao Yuqiang] index in mongodb (Part 2)](/img/a7/2140744ebad9f1dc0a609254cc618e.jpg)
[teacher Zhao Yuqiang] index in mongodb (Part 2)
![[teacher Zhao Yuqiang] Flink's dataset operator](/img/cc/5509b62756dddc6e5d4facbc6a7c5f.jpg)
[teacher Zhao Yuqiang] Flink's dataset operator
![[function explanation (Part 2)] | [function declaration and definition + function recursion] key analysis + code diagram](/img/29/1644588927226a49d4b8815d8bc196.jpg)
[function explanation (Part 2)] | [function declaration and definition + function recursion] key analysis + code diagram
![[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

Personal outlook | looking forward to the future from Xiaobai's self analysis and future planning

@Import annotation: four ways to import configuration classes & source code analysis

期末复习(Day5)

深度学习,从一维特性输入到多维特征输入引发的思考
随机推荐
[trivia of two-dimensional array application] | [simple version] [detailed steps + code]
[Shangshui Shuo series together] day 10
How does win7 solve the problem that telnet is not an internal or external command
70 shell script interview questions and answers
[branch and cycle] | | super long detailed explanation + code analysis + a trick game
Btrfs and ext4 - features, strengths and weaknesses
伯努利分布,二项分布和泊松分布以及最大似然之间的关系(未完成)
[teacher Zhao Yuqiang] redis's slow query log
Final review (Day6)
【一起上水硕系列】Day 7 内容+Day8
Understand expectations (mean / estimate) and variances
Ensemble, série shuishu] jour 9
Redis cannot connect remotely.
[teacher Zhao Yuqiang] the most detailed introduction to PostgreSQL architecture in history
[advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation
PHP笔记超详细!!!
Sophomore dilemma (resumption)
[function explanation (Part 1)] | | knowledge sorting + code analysis + graphic interpretation
2022.6.30DAY591
Using the ethtool command by example