/
iRumba
/
LeetCode
Обзор
Документация
Войти
/
iRumba
/
LeetCode
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
TwoSum1/Solution.cs
45 строк
1 KB
iRumba
1 Solution
18 ноя 2025, 07:23
18 ноя 2025, 07:23
8d256c6
Код
Авторство
О чём код?
using System; using System.Collections.Generic; using System.Text; namespace TwoSum1; public class Solution { public int[] TwoSum(int[] nums, int target) { var expectedDictionary = new Dictionary<int, int>(); for(var currentIndex = 0; currentIndex < nums.Length; currentIndex++) { var currentValue = nums[currentIndex]; if (expectedDictionary.TryGetValue(currentValue, out var firstElementIndex)) { return new[] { firstElementIndex, currentIndex }; } expectedDictionary[target - currentValue] = currentIndex; } return new int[0]; //var lastIndex = nums.Length - 1; //var preLastIndex = lastIndex - 1; //for(var i = 0; i <= preLastIndex; i++) //{ // var currentI = nums[i]; // for(var j = i + 1; j <= lastIndex; j++) // { // var currentJ = nums[j]; // if (currentI + currentJ == target) // { // return new[] { i, j }; // } // } //} //return new int[0]; } }