In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how LeetCode achieves the sum of two numbers. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
one
Topic description
Given an array of integers nums and a target value target, find out in the array the array subscript of the two integers whose sum is the target value. For example, when nums = [2, 7, 11, 15] and target = 9, return [0jue 1] (because 2-7-9).
two
Solve a problem
Idea 1: ergodic matching
Verify all the possibilities one by one through a two-layer for loop to find the correct result. The display time of this method python program in LeetCode exceeds the limit and is not recommended.
Idea 2: hash table
The relationship between each numeric value and location is found through a layer of for loop combined with enumerate (), which is stored in a hash table. Because you want to find the sum of two numbers, in each loop, first determine whether target-item is in the established hash table, if so, return the position of the two values, if not add the value and position relationship obtained this time to the hash table.
Class Solution: def twoSum (self, nums: List [int], target: int)-> List [int]: h_map = {} # or h_map = dict () for iMagne item in enumerate (nums): if target-item in h_map: # or if h_map.get (target-item) is not None: # but slightly slower than return [h _ map [target-item], I] h _ map [item] = I
It should be noted that, similar to the input of nums= [2 for 2] and target=4, the position of the second 2 will override the position of the first 2 when generating the hash table, resulting in the failure to get the result of [0 score 1]. Therefore, it is necessary to add the value and position relationship of this time to the hash table at the end of the cycle, rather than adding the corresponding relationship to the hash table after two layers of for. And this method, when judging whether the target-item is in the list, looks for the qualified value in the value before the item, rather than judging all the elements in the list, so as to further improve the efficiency (don't worry that you will miss the correct answer, because when the two elements are cycled, the result can still be identified).
Thank you for reading! This is the end of the article on "how to achieve the sum of two numbers in LeetCode". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.