無聊看 Leet Code 覺得網路上好像都沒啥 c# 版本 , 覺得賭爛 , 自己來玩看看順便翻譯
這題直覺就是想到雙迴圈搞出來用傳統的 for
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public int[] TwoSum1(int[] nums, int target) { var result = new int[2]; for (int i = 0; i < nums.Length; i++) { for(int j = i + 1; j < nums.Length; j++) { if(nums[i] + nums[j] == target) { result[0] = i; result[1] = j; return result; } } } return result; }
|
遇到這種東西多半都會要你省時間改成單迴圈 , 算是空間換時間 , 偷看 java 是用 HashMap 其實就是 .net 裡面的 Dictionary
這 Dictionary 內的邏輯有點反 , 要把 target - 目前數值當成 key 放進去 , 把索引位置當成 value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public int[] TwoSum2(int[] nums, int target) { Dictionary<int, int> dict = new Dictionary<int, int>( ); var result = new int[2]; for (int i = 0; i < nums.Length; i++) { var currentValue = nums[i]; if(dict.ContainsKey( target - currentValue )) { result[0] = dict[target - currentValue]; result[1] = i; break; } else { dict.Add(currentValue , i ); } } return result; }
|