diff options
author | chai <chaifix@163.com> | 2021-06-24 19:55:26 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-06-24 19:55:26 +0800 |
commit | dbcd0c269014100b7d4cc421c5ab518f275cca09 (patch) | |
tree | 6564ca8a58b6921d4782bba3feae0ea787f9e1f4 /Assets/Algorithms/Algorithms_Utils.cs | |
parent | 2749e059084b99737d79aadd92521023b0f65f56 (diff) |
Diffstat (limited to 'Assets/Algorithms/Algorithms_Utils.cs')
-rw-r--r-- | Assets/Algorithms/Algorithms_Utils.cs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Assets/Algorithms/Algorithms_Utils.cs b/Assets/Algorithms/Algorithms_Utils.cs new file mode 100644 index 0000000..6eee42f --- /dev/null +++ b/Assets/Algorithms/Algorithms_Utils.cs @@ -0,0 +1,31 @@ +using System;
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace AlgorithmCollection
+{
+ public static partial class Algorithms
+ {
+ // 两数之和,在nums中找到和为target的两个数
+ public static int[] TwoSum(int[] nums, int target)
+ {
+ int a = 0, b = 0;
+ Dictionary<int, int> lut = new Dictionary<int, int>();
+ for (int i = 0; i < nums.Length; ++i)
+ {
+ if (lut.ContainsKey(target - nums[i]))
+ {
+ a = i;
+ b = lut[target - nums[i]];
+ break;
+ }
+ lut.Add(nums[i], i);
+ }
+ return new int[] { a, b };
+ }
+
+ //
+
+ }
+}
\ No newline at end of file |