LeetCode Study Notes - How to Start Using LeetCode to Brush Questions - First Experience of Brushing LeetCode - LeetCode Question 1 - Two Sum Solution
Github link
1. How to get started on the LeetCode website?
Step 1: Open the LeetCode website and log in
Step 2: Click on Problems
Step 3: Fill in the topics and choose the category topics you want
Step 4: Select a good question, click in, and you can start to brush the question (Two Sum as an example)
2. Two Sum implementation
topic:
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.
Given a nums array of integers and a target integer, the program will return the indices of the two numbers in the array, that is, the two numbers will add up to the target integer
You can assume that there is only one solution per input and the same element will not be reused, the answer can be returned in any order
Solution 1: Violent solution
Description: First take a fixed element from the list, and then start pairing one by one from its next element to see if the addition is equal to the target value, if not, replace the next element, and so on
def two_sum(nums, target): for i in range(len(nums)): for j in range(i+1, len(nums)): if nums[i] + nums[j] == target: return i, j nums = [1, 2, 3, 4, 5, 6] target = 10 two_sum(nums, target)
Results of the
(3, 5)
Reminder: Although the violent solution is easy to understand, it is a very bad choice for the program execution performance. Unless it is not possible, I hope everyone can use the violent solution less.
Solution 2: Hash Table Method - Hash Method
Description: Create a dictionary to load the value and the index, the value is placed in the key, and the index is placed in the value. When the program starts to execute, because no data has been added to the previous hash_table, it must be empty, and then the data in front of nums is slowly filled. After entering, to the next element, hash_table also has a lot of data, it may be added to the previous element to equal the target value, and then it can be returned
method one
class Solution: def two_sum(self, nums, target): hash_table = {} for i, num in enumerate(nums): t = target - num if t in hash_table: return [hash_table[t], i] ## Add data to hash table (key: value, value: index) hash_table[num] = i ## Return [-1, -1] when no answer is found return [-1, -1] # print(hash_table) nums = [1, 2, 3, 4, 5, 6] target = 10 Solution().two_sum(nums, target)
Results of the
[3, 5]
Method Two
class Solution: def two_sum(self, nums, target): hash_map = {} for i in range(len(nums)): if target - nums[i] in hash_map: return hash_map.get(target - nums[i]), i hash_map[nums[i]] = i nums = [1, 2, 3, 4, 5, 6] target = 10 Solution().two_sum(nums, target)
I tested the execution time on LeetCode, the method will be slightly faster!!
Like my work? Don't forget to support and clap, let me know that you are with me on the road of creation. Keep this enthusiasm together!
- Author
- More