From dbcd0c269014100b7d4cc421c5ab518f275cca09 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 24 Jun 2021 19:55:26 +0800 Subject: *misc --- Assets/Algorithms/Algorithms_Utils.cs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Assets/Algorithms/Algorithms_Utils.cs (limited to 'Assets/Algorithms/Algorithms_Utils.cs') 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 lut = new Dictionary(); + 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 -- cgit v1.1-26-g67d0