diff options
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 | 
