-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSum.cs
More file actions
64 lines (54 loc) · 1.69 KB
/
twoSum.cs
File metadata and controls
64 lines (54 loc) · 1.69 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
namespace treinoObjetos
{
public class Solution
{
public int[] TwoSum(int[] nums, int target)
{
for (int i = 0; i < nums.Length; i++)
{
for (int j = i++; j < nums.Length; j++)
{
if (nums[i] + nums[j] == target)
{
return new int[] { i, j };
}
}
}
return new int[0];
/*
Dictionary<int, int> map = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)
{
int complemento = target - nums[i];
if (map.ContainsKey(complemento))
{
return new int[] { map[complemento], i };
}
map[nums[i]] = i;
}
return new int[0];
*/
}
static void Main(string[] args)
{
Console.Write("\nDigite a quantidade de números: ");
int qtd = int.Parse(Console.ReadLine());
int[] nums = new int[qtd];
for (int i = 0; i < qtd; i++)
{
Console.Write("\nDigite o " + (i + 1) +"º número: ");
nums[i] = int.Parse(Console.ReadLine());
}
Console.Write("\nDigite o alvo da soma: ");
int alvo = int.Parse(Console.ReadLine());
Solution sol = new Solution();
int[] resultado = sol.TwoSum(nums, alvo);
Console.Write("\nResultado: ");
foreach (int i in resultado)
{
Console.Write(i + " ");
}
}
}
}