Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the Sum of two numbers by C # algorithm

Shulou Source: shulou.com Published: 2022-06-02 02:33:50 09月28日 Update

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

Tags: Array article hash sum algorithm caching code content integer time goal target value disadvantage run same for reference only subscript not so two element Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi Huawei MySQL macOS