-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
49 lines (43 loc) · 1.44 KB
/
Solution.cs
File metadata and controls
49 lines (43 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace LeetCode.Problem1{
//1. Two Sum
//https://leetcode.com/problems/two-sum/
/*
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
*/
public class Solution {
public int[] TwoSum(int[] nums, int target) {
int[] Out = new int[2];
for (int i=0;i<nums.Length;i++)
{
int second=target-nums[i];
for (int j=i+1;j<nums.Length;j++)
{
if (nums[j]==second)
{
Out[0]=i;
Out[1]=j;
break;
}
}
}
return Out;
}
}
public class Alternative {
public int[] TwoSum(int[] nums, int target) {
Dictionary<int,int> dic = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)
{
int value = nums[i];
int find = target - nums[i];
if (dic.ContainsKey(find))
return new int[] {i,dic[find]};
if (!dic.ContainsKey(value))
dic.Add(value,i);
}
return new int[] {-1,-1};
}
}
}