- LeetCode #1 — Two Sum
- LeetCode Problem 1 Solution in Python
- Discussing the approach for an optimal solution to Two Sum problem in LeetCode
- Introduction
- The Two Sum Problem
- A not so..optimal solution
- Π Π°Π·Π±ΠΎΡ Π·Π°Π΄Π°ΡΠΈ 1 Two Sum LeetCode.com
- Π Π΅ΡΠ΅Π½ΠΈΠ΅ Two Sum LeetCode ΠΏΡΠΈΠΌΠ΅Ρ ΠΊΠΎΠ΄Π° Π½Π° Python
- Π Π΅ΡΠ΅Π½ΠΈΠ΅ Two Sum LeetCode ΠΏΡΠΈΠΌΠ΅Ρ ΠΊΠΎΠ΄Π° Π½Π° Java
- Π Π΅ΡΠ΅Π½ΠΈΠ΅ Two Sum LeetCode ΠΏΡΠΈΠΌΠ΅Ρ ΠΊΠΎΠ΄Π° Π½Π° C++
- Π Π΅ΡΠ΅Π½ΠΈΠ΅ Two Sum LeetCode ΠΏΡΠΈΠΌΠ΅Ρ ΠΊΠΎΠ΄Π° Π½Π° JavaScript
- Π Π΅ΡΠ΅Π½ΠΈΠ΅ Two Sum LeetCode ΠΏΡΠΈΠΌΠ΅Ρ ΠΊΠΎΠ΄Π° Π½Π° c#
LeetCode #1 — Two Sum
Hello happy people π! Today we are going to discuss the very first problem on the LeetCode.
Given an array of integers nums and an integer target , return indices of the two numbers such that they add up to target .
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
So, we have to find two numbers among all the numbers in an array such that when they are added, they give us a third number equals to target .
If nums = [2,7,11,15] and target = 9, then we should return 2 and 7 because 2 + 7 = 9
In simpler words, we need to find —
c = a + b where, c => target a and b => elements from the given array
Also, the given constraints suggest that there is only ONE valid answer along with the ranges of the elements in the array and the target.
After we have analyzed our problem, now itβs time to think about the solution.
The first solution that comes to mind is —
- Take one element
- Add this element with every other element
- After adding, compare the sum with the given target
- If the sum is equal to the target, return the indices of these two elements
- If the sum is not equal to the target, we check for the next pair
Pretty simple, eh π? Letβs analyze the time and space complexities.
Letβs say the array has n elements then —
For 1st element - we will check for (n - 1) elements For 2nd element - we will check for (n - 2) elements For 3rd element - we will check for (n - 3) elements and so on. Thus, the total iterations would be - [(n - 1) + (n - 2) + (n - 3) + . + 2 + 1]
If we simplify the above expression we will get —
Thus, our time complexity would be — O(n 2 ).
Since we are not using any extra data structure hence our space complexity would be O(1).
This approach is simple and intuitive but it takes quadratic time which is bad for large inputs. Letβs see if we have any efficient approach to solve this problem. As a matter of fact, we do π!
In the earlier approach, we did what naturally came to us. But letβs try to really think through this problem. Hmmβ¦ π€
When we were analyzing the problem, we came across the following equation
but can we write the above equation as —
Of course we can. We are mathematicians after all π.
The benefit of writing this equation is that now we can iterate through the array only once and calculate the difference of target (c) and the current element (a) and find the other element (b) . The idea is as follows —
- Loop through the array
- Check if we have encountered the current element before
- If we have, we will return the index of this element and the index of the difference of the target and the current element . We will only encounter this element before only if we have saved the difference of target and the element which when added to the current element gives the target itself. This is confusing I know but once it clicks, itβs very obvious.
- If we havenβt, the we will save the difference of the target and current element .
- Repeat the process
Since we need to store our difference , we need a data structure which can store the difference and its corresponding index. So what do you think, which data structure can help us? Of course, we need a Map where we will store the difference as key and the corresponding index as value .
Since we are iterating the array only once, the time complexity would be O(n).
Since we need a Map of the size of the array, the space complexity would be O(n).
Now we have an approach to solve this problem, letβs write some code —
package org.redquark.tutorials.leetcode; import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class TwoSum public int[] twoSum(int[] nums, int target) // Array to store result int[] result = new int[2]; // This map will store the difference and the corresponding index MapInteger, Integer> map = new HashMap>(); // Loop through the entire array for (int i = 0; i nums.length; i++) // If we have seen the current element before // It means we have already encountered the other number of the pair if (map.containsKey(nums[i])) // Index of the current element result[0] = i; // Index of the other element of the pair result[1] = map.get(nums[i]); > // If we have not seen the current before // It means we have not yet encountered any number of the pair else // Save the difference of the target and the current element // with the index of the current element map.put(target - nums[i], i); > > return result; > >
from typing import List def twoSum(nums: List[int], target: int) -> List[int]: # List to store results result = [] # Dictionary to store the difference and its index index_map = > # Loop for each element for i, n in enumerate(nums): # Difference which needs to be checked difference = target - n if difference in index_map: result.append(i) result.append(index_map[difference]) break else: index_map[n] = i return result
var twoSum = function (nums, target) // Array to store the result result = []; // Map to store the difference and its index index_map = new Map(); // Loop for each element in the array for (let i = 0; i nums.length; i++) let difference = target - nums[i]; if (index_map.has(difference)) result[0] = i; result[1] = index_map.get(difference); break; > else index_map.set(nums[i], i); > > return result; >;
package org.redquark.tutorials.leetcode fun twoSum(nums: IntArray, target: Int): IntArray // Array to store result val result = IntArray(2) // This map will store the difference and the corresponding index val map: MutableMapInt, Int> = HashMap() // Loop through the entire array for (i in nums.indices) // If we have seen the current element before // It means we have already encountered the other number of the pair if (map.containsKey(nums[i])) // Index of the current element result[0] = i // Index of the other element of the pair result[1] = map[nums[i]]!! break > else // Save the difference of the target and the current element // with the index of the current element map[target - nums[i]] = i > > return result >
I hope you liked this post. Here, we solved the very first problem on LeetCode in O(n) time and O(n) space.
You can find the complete source code on GitHub. If you find it useful, consider giving it a star β.
- Java
- Python
- JavaScript
- Kotlin Feel free to share your thoughts about this post in commentsβ section. Iβd love to hear your feedback.
Happy coding π and Namaste π!
LeetCode Problem 1 Solution in Python
Discussing the approach for an optimal solution to Two Sum problem in LeetCode
Introduction
LeetCode is a platform that gives access to thousands of programming problems and helps users enhance their skills and get prepared for technical interviews that are usually part of the recruitment process for Engineering and ML positions.
In todayβs short guide we will explore the first problem called Two Sum and attempt to solve it in an optimal way. In technical interviews, itβs not only important to derive a solution for a particular problem but the time complexity is also something you will usually be questioned about.
The Two Sum Problem
Given an array of integers nums and an integer target , return indices of the two numbers such that they add up to target .
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Input: nums = [3,2,4], target = 6
Output: [1,2]
Input: nums = [3,3], target = 6
Output: [0,1]
Constraints:
A not so..optimal solution
The most simple approach would require two nested loops where the outer loop iterates over all the elements of the list and the inner loop iterates from the current index of the outer loop up to the end of the list.
Π Π°Π·Π±ΠΎΡ Π·Π°Π΄Π°ΡΠΈ 1 Two Sum LeetCode.com
Π£ΡΠΈΡΡΠ²Π°Ρ ΠΌΠ°ΡΡΠΈΠ² ΡΠ΅Π»ΡΡ ΡΠΈΡΠ΅Π» nums ΠΈ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ target , Π²Π΅ΡΠ½ΠΈΡΠ΅ ΠΈΠ½Π΄Π΅ΠΊΡΡ Π΄Π²ΡΡ ΡΠΈΡΠ΅Π» ΡΠ°ΠΊ, ΡΡΠΎΠ±Ρ ΠΎΠ½ΠΈ ΡΠΎΡΡΠ°Π²Π»ΡΠ»ΠΈ Π² ΡΡΠΌΠΌΠ΅ target .
ΠΡ ΠΌΠΎΠΆΠ΅ΡΠ΅ ΠΏΡΠ΅Π΄ΠΏΠΎΠ»ΠΎΠΆΠΈΡΡ, ΡΡΠΎ ΠΊΠ°ΠΆΠ΄ΡΠΉ Π²Ρ ΠΎΠ΄ Π±ΡΠ΄Π΅Ρ ΠΈΠΌΠ΅ΡΡ ΡΠΎΠ²Π½ΠΎ ΠΎΠ΄Π½ΠΎ ΡΠ΅ΡΠ΅Π½ΠΈΠ΅ , ΠΈ Π²Ρ Π½Π΅ ΠΌΠΎΠΆΠ΅ΡΠ΅ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΎΠ΄ΠΈΠ½ ΠΈ ΡΠΎΡ ΠΆΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½Ρ Π΄Π²Π°ΠΆΠ΄Ρ.
ΠΡ ΠΌΠΎΠΆΠ΅ΡΠ΅ Π²Π΅ΡΠ½ΡΡΡ ΠΎΡΠ²Π΅Ρ Π² Π»ΡΠ±ΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅.
ΠΠ³ΡΠ°Π½ΠΈΡΠ΅Π½ΠΈΡ:
2 -10 9 -10 9 Π‘ΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ ΡΠΎΠ»ΡΠΊΠΎ ΠΎΠ΄ΠΈΠ½ ΠΏΡΠ°Π²ΠΈΠ»ΡΠ½ΡΠΉ ΠΎΡΠ²Π΅Ρ.
ΠΠ»Ρ ΡΠ΅ΡΠ΅Π½ΠΈΡ ΡΡΠΎΠΉ Π·Π°Π΄Π°ΡΠΈ ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ Ρ ΡΡ-ΡΠ°Π±Π»ΠΈΡΡ, ΡΡΠΎΠ±Ρ Ρ ΡΠ°Π½ΠΈΡΡ ΡΠΆΠ΅ ΠΏΡΠΎΡΠΌΠΎΡΡΠ΅Π½Π½ΡΠ΅ ΡΠΈΡΠ»Π°. ΠΡ ΠΏΡΠΎΡ ΠΎΠ΄ΠΈΠΌ ΠΏΠΎ ΠΌΠ°ΡΡΠΈΠ²Ρ ΡΠΈΡΠ΅Π» ΠΈ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠΈΡΠ»Π° ΠΏΡΠΎΠ²Π΅ΡΡΠ΅ΠΌ, ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ Π»ΠΈ ΡΠΈΡΠ»ΠΎ, ΠΊΠΎΡΠΎΡΠΎΠ΅, Π² ΡΡΠΌΠΌΠ΅ Ρ ΡΠ΅ΠΊΡΡΠΈΠΌ ΡΠΈΡΠ»ΠΎΠΌ, Π΄Π°ΡΡ Π½Π°ΠΌ ΡΠ΅Π»Π΅Π²ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ. ΠΡΠ»ΠΈ ΡΠ°ΠΊΠΎΠ΅ ΡΠΈΡΠ»ΠΎ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ, ΠΌΡ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌ ΠΈΠ½Π΄Π΅ΠΊΡΡ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΡΠΈΡΠ»Π° ΠΈ ΡΠΈΡΠ»Π° ΠΈΠ· Ρ ΡΡ-ΡΠ°Π±Π»ΠΈΡΡ. ΠΡΠ»ΠΈ ΡΠ°ΠΊΠΎΠ΅ ΡΠΈΡΠ»ΠΎ Π½Π΅ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ, ΠΌΡ Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΡΠΈΡΠ»ΠΎ ΠΈ Π΅Π³ΠΎ ΠΈΠ½Π΄Π΅ΠΊΡ Π² Ρ ΡΡ-ΡΠ°Π±Π»ΠΈΡΡ ΠΈ ΠΏΡΠΎΠ΄ΠΎΠ»ΠΆΠ°Π΅ΠΌ ΠΈΡΠ΅ΡΠ°ΡΠΈΡ ΠΏΠΎ ΠΌΠ°ΡΡΠΈΠ²Ρ.
ΠΡΠΎΡ Π°Π»Π³ΠΎΡΠΈΡΠΌ ΠΈΠΌΠ΅Π΅Ρ Π²ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ ΡΠ»ΠΎΠΆΠ½ΠΎΡΡΡ O(n), ΡΠ°ΠΊ ΠΊΠ°ΠΊ ΠΌΡ ΠΏΡΠΎΡ ΠΎΠ΄ΠΈΠΌ ΡΠΎΠ»ΡΠΊΠΎ ΠΎΠ΄ΠΈΠ½ ΡΠ°Π· ΠΏΠΎ ΠΌΠ°ΡΡΠΈΠ²Ρ ΡΠΈΡΠ΅Π», ΠΈ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠΈΡΠ»Π° Π²ΡΠΏΠΎΠ»Π½ΡΠ΅ΠΌ ΠΊΠΎΠ½ΡΡΠ°Π½ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ (ΠΏΡΠΎΠ²Π΅ΡΠΊΠ°, ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ Π»ΠΈ ΡΠΈΡΠ»ΠΎ Π² Ρ ΡΡ-ΡΠ°Π±Π»ΠΈΡΠ΅, Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΡΠΈΡΠ»Π° ΠΈ Π΅Π³ΠΎ ΠΈΠ½Π΄Π΅ΠΊΡΠ° Π² Ρ ΡΡ-ΡΠ°Π±Π»ΠΈΡΡ). ΠΡΠΎ Π·Π½Π°ΡΠΈΡΠ΅Π»ΡΠ½ΠΎ Π±ΡΡΡΡΠ΅Π΅, ΡΠ΅ΠΌ O(n2), ΠΊΠΎΡΠΎΡΠ°Ρ Π±ΡΠ»Π° Π±Ρ Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠ°, Π΅ΡΠ»ΠΈ Π±Ρ ΠΌΡ ΠΏΡΠΎΠ²Π΅ΡΡΠ»ΠΈ Π²ΡΠ΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΡΠ΅ ΠΏΠ°ΡΡ ΡΠΈΡΠ΅Π» Π² ΠΌΠ°ΡΡΠΈΠ²Π΅.
Π Π΅ΡΠ΅Π½ΠΈΠ΅ Two Sum LeetCode ΠΏΡΠΈΠΌΠ΅Ρ ΠΊΠΎΠ΄Π° Π½Π° Python
class Solution: # ΠΌΠ΅ΡΠΎΠ΄ twoSum ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ Π½Π° Π²Ρ ΠΎΠ΄ ΡΠΏΠΈΡΠΎΠΊ nums ΠΈ ΡΠ΅Π»Π΅Π²ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ target # ΠΈ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ ΡΠΏΠΈΡΠΎΠΊ ΠΈΠ· Π΄Π²ΡΡ ΠΈΠ½Π΄Π΅ΠΊΡΠΎΠ² ΡΠΈΡΠ΅Π» Π² nums, ΡΡΠΌΠΌΠ° ΠΊΠΎΡΠΎΡΡΡ ΡΠ°Π²Π½Π° target def twoSum(self, nums: List[int], target: int) -> List[int]: # ΡΠ»ΠΎΠ²Π°ΡΡ Π΄Π»Ρ Ρ ΡΠ°Π½Π΅Π½ΠΈΡ ΡΠΆΠ΅ ΠΏΡΠΎΡΠΌΠΎΡΡΠ΅Π½Π½ΡΡ ΡΠΈΡΠ΅Π» seen = <> # ΠΏΡΠΎΡ ΠΎΠ΄ΠΈΠΌΡΡ ΠΏΠΎ Π²ΡΠ΅ΠΌ ΡΠΈΡΠ»Π°ΠΌ Π² ΡΠΏΠΈΡΠΊΠ΅ nums Ρ ΠΈΠ½Π΄Π΅ΠΊΡΠ°ΠΌΠΈ for i, num in enumerate(nums): # Π²ΡΡΠΈΡΠ»ΡΠ΅ΠΌ "ΠΊΠΎΠΌΠΏΠ»Π΅ΠΌΠ΅Π½Ρ" - Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π±ΡΡΡ Π² ΡΠΏΠΈΡΠΊΠ΅, ΡΡΠΎΠ±Ρ ΠΏΠΎΠ»ΡΡΠΈΡΡ target complement = target - num # Π΅ΡΠ»ΠΈ ΠΊΠΎΠΌΠΏΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠΆΠ΅ Π±ΡΠ» ΠΏΡΠΎΡΠΌΠΎΡΡΠ΅Π½, ΡΠΎ Π½Π°ΡΠ»ΠΈ Π½ΡΠΆΠ½ΡΠ΅ ΠΈΠ½Π΄Π΅ΠΊΡΡ ΡΠΈΡΠ΅Π» if complement in seen: return [seen[complement], i] # ΡΠΎΡ ΡΠ°Π½ΡΠ΅ΠΌ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΡΠΈΡΠ»ΠΎ ΠΈ Π΅Π³ΠΎ ΠΈΠ½Π΄Π΅ΠΊΡ Π² ΡΠ»ΠΎΠ²Π°ΡΡ seen seen[num] = i # Π΅ΡΠ»ΠΈ Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ Π½ΡΠΆΠ½ΡΡ ΠΈΠ½Π΄Π΅ΠΊΡΠΎΠ², Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌ None return None
Π Π΅ΡΠ΅Π½ΠΈΠ΅ Two Sum LeetCode ΠΏΡΠΈΠΌΠ΅Ρ ΠΊΠΎΠ΄Π° Π½Π° Java
class Solution < public int[] twoSum(int[] nums, int target) < // ΡΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Ρ ΡΡ-ΡΠ°Π±Π»ΠΈΡΡ Π΄Π»Ρ Ρ ΡΠ°Π½Π΅Π½ΠΈΡ ΡΠΈΡΠ΅Π» ΠΈ ΠΈΡ ΠΈΠ½Π΄Π΅ΠΊΡΠΎΠ² Mapmap = new HashMap<>(); // ΠΏΠ΅ΡΠ΅Π±ΠΎΡ ΠΌΠ°ΡΡΠΈΠ²Π° ΡΠΈΡΠ΅Π» for (int i = 0; i < nums.length; i++) < // Π½Π°Ρ ΠΎΠΆΠ΄Π΅Π½ΠΈΠ΅ ΡΠ°Π·Π½ΠΎΡΡΠΈ ΠΌΠ΅ΠΆΠ΄Ρ ΡΠ΅Π»Π΅Π²ΡΠΌ ΡΠΈΡΠ»ΠΎΠΌ ΠΈ ΡΠ΅ΠΊΡΡΠΈΠΌ ΡΠΈΡΠ»ΠΎΠΌ int complement = target - nums[i]; // Π΅ΡΠ»ΠΈ ΡΠ°ΠΊΠ°Ρ ΡΠ°Π·Π½ΠΎΡΡΡ ΡΠΆΠ΅ Π΅ΡΡΡ Π² Ρ ΡΡ-ΡΠ°Π±Π»ΠΈΡΠ΅, Π·Π½Π°ΡΠΈΡ ΠΌΡ Π½Π°ΡΠ»ΠΈ ΠΏΠ°ΡΡ ΡΠΈΡΠ΅Π» if (map.containsKey(complement)) < // Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌ ΠΈΠ½Π΄Π΅ΠΊΡΡ Π΄Π²ΡΡ ΡΠΈΡΠ΅Π» return new int[] < map.get(complement), i >; > // Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΡΠΈΡΠ»ΠΎ ΠΈ Π΅Π³ΠΎ ΠΈΠ½Π΄Π΅ΠΊΡ Π² Ρ ΡΡ-ΡΠ°Π±Π»ΠΈΡΡ map.put(nums[i], i); > // Π΅ΡΠ»ΠΈ Π½Π΅ Π½Π°ΡΠ»ΠΈ ΠΏΠ°ΡΡ ΡΠΈΡΠ΅Π», Π²ΡΠ±ΡΠ°ΡΡΠ²Π°Π΅ΠΌ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ throw new IllegalArgumentException("No two sum solution"); > >
Π Π΅ΡΠ΅Π½ΠΈΠ΅ Two Sum LeetCode ΠΏΡΠΈΠΌΠ΅Ρ ΠΊΠΎΠ΄Π° Π½Π° C++
class Solution < public: vectortwoSum(vector& nums, int target) < // ΡΠΎΠ·Π΄Π°Π½ΠΈΠ΅ unordered_map Π΄Π»Ρ Ρ ΡΠ°Π½Π΅Π½ΠΈΡ ΡΠΈΡΠ΅Π» ΠΈ ΠΈΡ ΠΈΠ½Π΄Π΅ΠΊΡΠΎΠ² unordered_mapmp; for(int i = 0; i < nums.size(); i++) < // Π½Π°Ρ ΠΎΠ΄ΠΈΠΌ ΡΠΈΡΠ»ΠΎ-Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ int complement = target - nums[i]; // Π΅ΡΠ»ΠΈ Π½Π°Ρ ΠΎΠ΄ΠΈΠΌ Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ Π² unordered_map if(mp.find(complement) != mp.end()) < // Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌ Π΅Π³ΠΎ ΠΈΠ½Π΄Π΅ΠΊΡ ΠΈ ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΈΠ½Π΄Π΅ΠΊΡ return < mp[complement], i >; > // Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΡΠΈΡΠ»ΠΎ ΠΈ Π΅Π³ΠΎ ΠΈΠ½Π΄Π΅ΠΊΡ Π² unordered_map mp[nums[i]] = i; > // Π΅ΡΠ»ΠΈ Π½Π΅Ρ ΡΠ΅ΡΠ΅Π½ΠΈΡ, Π²ΡΠ±ΡΠ°ΡΡΠ²Π°Π΅ΠΌ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ throw invalid_argument("No two sum solution"); > >;
Π Π΅ΡΠ΅Π½ΠΈΠ΅ Two Sum LeetCode ΠΏΡΠΈΠΌΠ΅Ρ ΠΊΠΎΠ΄Π° Π½Π° JavaScript
/** * Π€ΡΠ½ΠΊΡΠΈΡ twoSum ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ Π½Π° Π²Ρ ΠΎΠ΄ ΠΌΠ°ΡΡΠΈΠ² ΡΠΈΡΠ΅Π» nums ΠΈ ΡΠ΅Π»Π΅Π²ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ target. * ΠΠ½Π° Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ ΠΌΠ°ΡΡΠΈΠ² Ρ Π΄Π²ΡΠΌΡ ΠΈΠ½Π΄Π΅ΠΊΡΠ°ΠΌΠΈ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ², ΡΡΠΌΠΌΠ° ΠΊΠΎΡΠΎΡΡΡ ΡΠ°Π²Π½Π° ΡΠ΅Π»Π΅Π²ΠΎΠΌΡ ΡΠΈΡΠ»Ρ. * @paramnums - ΠΌΠ°ΡΡΠΈΠ² ΡΠΈΡΠ΅Π» * @param target - ΡΠ΅Π»Π΅Π²ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ * @returns - ΠΌΠ°ΡΡΠΈΠ² Ρ Π΄Π²ΡΠΌΡ ΠΈΠ½Π΄Π΅ΠΊΡΠ°ΠΌΠΈ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² * @throws - Π΅ΡΠ»ΠΈ ΡΠ΅ΡΠ΅Π½ΠΈΠ΅ Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ */ const twoSum = function(nums, target) < // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ Ρ ΡΡ-ΡΠ°Π±Π»ΠΈΡΡ Π΄Π»Ρ Ρ ΡΠ°Π½Π΅Π½ΠΈΡ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² ΠΈ ΠΈΡ ΠΈΠ½Π΄Π΅ΠΊΡΠΎΠ² const map = new Map(); // ΠΡΠ΅ΡΠΈΡΡΠ΅ΠΌΡΡ ΠΏΠΎ ΠΌΠ°ΡΡΠΈΠ²Ρ nums for (let i = 0; i < nums.length; i++) < // ΠΡΡΠΈΡΠ»ΡΠ΅ΠΌ ΡΠ°Π·Π½ΠΎΡΡΡ ΡΠ΅Π»Π΅Π²ΠΎΠ³ΠΎ ΡΠΈΡΠ»Π° ΠΈ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° const complement = target - nums[i]; // ΠΡΠ»ΠΈ ΡΠ»Π΅ΠΌΠ΅Π½Ρ-Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡΠ΅Π»ΡΠ½ΠΎΠ΅ ΡΠ»Π°Π³Π°Π΅ΠΌΠΎΠ΅ ΡΠΆΠ΅ Π½Π°Ρ ΠΎΠ΄ΠΈΡΡΡ Π² Ρ ΡΡ-ΡΠ°Π±Π»ΠΈΡΠ΅, // Π·Π½Π°ΡΠΈΡ, ΠΌΡ Π½Π°ΡΠ»ΠΈ ΠΏΠ°ΡΡ, ΡΡΠΌΠΌΠ° ΠΊΠΎΡΠΎΡΠΎΠΉ ΡΠ°Π²Π½Π° ΡΠ΅Π»Π΅Π²ΠΎΠΌΡ ΡΠΈΡΠ»Ρ if (map.has(complement)) < // ΠΠΎΠ·Π²ΡΠ°ΡΠ°Π΅ΠΌ ΠΌΠ°ΡΡΠΈΠ² Ρ ΠΈΠ½Π΄Π΅ΠΊΡΠ°ΠΌΠΈ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² return [map.get(complement), i]; >// ΠΠ½Π°ΡΠ΅, Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΡΠ΅ΠΊΡΡΠΈΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΈ Π΅Π³ΠΎ ΠΈΠ½Π΄Π΅ΠΊΡ Π² Ρ ΡΡ-ΡΠ°Π±Π»ΠΈΡΡ map.set(nums[i], i); > // ΠΡΠ»ΠΈ Π½Π΅ Π±ΡΠ»ΠΎ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ ΠΏΠ°ΡΡ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ², ΡΡΠΌΠΌΠ° ΠΊΠΎΡΠΎΡΡΡ ΡΠ°Π²Π½Π° ΡΠ΅Π»Π΅Π²ΠΎΠΌΡ ΡΠΈΡΠ»Ρ, // Π²ΡΠ±ΡΠ°ΡΡΠ²Π°Π΅ΠΌ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ throw new Error('No two sum solution'); >;
Π Π΅ΡΠ΅Π½ΠΈΠ΅ Two Sum LeetCode ΠΏΡΠΈΠΌΠ΅Ρ ΠΊΠΎΠ΄Π° Π½Π° c#
public class Solution < public int[] TwoSum(int[] nums, int target) < Dictionarymap = new Dictionary(); // ΡΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΡΠ»ΠΎΠ²Π°ΡΡ for (int i = 0; i < nums.Length; i++) < // ΡΠΈΠΊΠ» ΠΏΠΎ ΠΌΠ°ΡΡΠΈΠ²Ρ int complement = target - nums[i]; // Π²ΡΡΠΈΡΠ»Π΅Π½ΠΈΠ΅ ΡΠ°Π·Π½ΠΎΡΡΠΈ ΠΌΠ΅ΠΆΠ΄Ρ ΡΠ΅Π»Π΅Π²ΡΠΌ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ΠΌ ΠΈ ΡΠ΅ΠΊΡΡΠΈΠΌ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠΌ ΠΌΠ°ΡΡΠΈΠ²Π° if (map.ContainsKey(complement)) < // ΠΏΡΠΎΠ²Π΅ΡΠΊΠ° Π½Π°Π»ΠΈΡΠΈΡ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°-Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΡ Π² ΡΠ»ΠΎΠ²Π°ΡΠ΅ return new int[] < map[complement], i >; // Π²ΠΎΠ·Π²ΡΠ°ΡΠ΅Π½ΠΈΠ΅ ΠΏΠ°ΡΡ ΠΈΠ½Π΄Π΅ΠΊΡΠΎΠ², Π΅ΡΠ»ΠΈ ΡΠ»Π΅ΠΌΠ΅Π½Ρ-Π΄ΠΎΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ Π½Π°ΠΉΠ΄Π΅Π½ > map[nums[i]] = i; // Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΡΠ΅ΠΊΡΡΠ΅Π³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° Π² ΡΠ»ΠΎΠ²Π°ΡΡ > throw new ArgumentException("No two sum solution"); // Π²ΡΠ±ΡΠΎΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΡ, Π΅ΡΠ»ΠΈ ΡΠ΅ΡΠ΅Π½ΠΈΠ΅ Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ΠΎ > >