In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how the C# algorithm to achieve the sum of two numbers, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Title
Given an array of integers nums and a target value target, please find the two integers in the array that are the target values and return their array subscript.
You can assume that each input corresponds to only one answer. However, you cannot reuse the same elements in this array.
Example:
Given nums = [2,7,11,15], target = 9
Because nums [0] + nums [1] = 2 + 7 = 9
So return [0,1]
Tip: you can't add it up by yourself.
Test case
[2,7,11,15]
nine
Expected result
[0,1]
Format template
Public class Solution {public int [] TwoSum (int [] nums, int target) {/ * Code * /}} author's code, for reference only
Use brute force method, run time 700ms-1100ms
Public class Solution {public int [] TwoSum (int [] nums, int target) {int [] a = new int [2]; for (int I = 0; I < nums.Length-1; iTunes +) {for (int j = I + 1; j < nums.Length) Nums +) {if (nums [I] + nums [j] = = target) {a [0] = I; a [1] = j;} return a;}}
Run time 400ms-600ms
Because you are using a hash table, the disadvantage is that the keys cannot be the same.
Public class Solution {public int [] TwoSum (int [] nums, int target) {int [] a = new int [2]; System.Collections.Hashtable hashtable = new System.Collections.Hashtable (); for (int I = 0; I < nums.Length; I +) {hashtable.Add (nums [I], I) } for (int I = 0; I < nums.Length; ionization +) {int complement = target-nums [I]; if (hashtable.ContainsKey (complement) & & int.Parse (hashtable [hashtable]. ToString ())! = I) {a [0] = I A [1] = int.Parse (hashtable.ToString ());}} return a;}}
It is also a hash table, but the disadvantage is that the type of hash table is object, which needs to be converted when getting the value.
Public int [] TwoSum (int [] nums, int target) {int [] a = new int [2]; System.Collections.Hashtable h = new System.Collections.Hashtable (); for (int I = 0; I < nums.Length; iTunes +) {int c = target-nums [I] If (h.ContainsKey (c)) {a [0] = int.Parse (h [c] .ToString ()) nums [I]? Int.Parse (h [c] .ToString ()): I;} else if (! h.ContainsKey (nums [I])) {h.Add (nums [I], I);}} return a } copy someone else's public class Solution {public int [] TwoSum (int [] nums, int target) {int [] res = {0,0}; int len = nums.Length; Dictionary dict = new Dictionary (); for (int I = 0; I < len; iTunes +) {int query = target-nums [I] If (dict.ContainsKey (query)) {int min = (I
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.