-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSum.java
More file actions
51 lines (41 loc) · 1.68 KB
/
twoSum.java
File metadata and controls
51 lines (41 loc) · 1.68 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
/*Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].*/
/*
1st : Brute force
T=O(n^2)
S=O(1)
~~~~~~~~~~~~~~~~~~~
class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i = 0; i < nums.length; i++) {
for(int j = i + 1; j < nums.length; j++) {
if (nums[j] == target - nums[i]) {
return new int[] {i, j};
}
}
}
throw new IllegalArgumentException("NO Solution Found");
}
}
/*When Arguments out of range. For example, the percentage should lie between 1 to 100. If the user entered 101 then an IllegalArugmentExcpetion will be thrown.
When arguments format is invalid. For example, if our method requires date format like YYYY/MM/DD but if the user is passing YYYY-MM-DD. Then our method can’t understand then IllegalArugmentExcpetion will be thrown.
When a method needs non-empty string as a parameter but the null string is passed.
*/
class twoSum {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> numbers = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int j = target - nums[i];
if (numbers.containsKey(j)){
return new int[] {numbers.get(j), i};
}
numbers.put(nums[i],i);
}
//System.out.println("numbers " + numbers);
throw new IllegalArgumentException("No Solution Found");
}
}