Skip to content

Commit 6e65d41

Browse files
committed
two sum solution
1 parent ecd7f5b commit 6e65d41

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

โ€Žtwo-sum/YuuuuuuYu.javaโ€Ž

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Runtime: 3ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 46.97MB
6+
* Space Complexity: O(n)
7+
*
8+
* Approach: HashMap์„ ์‚ฌ์šฉํ•˜์—ฌ ์ง์„ ์ด๋ฃจ๋Š” ๊ฐ’(pair) ๊ฒ€์‚ฌ
9+
* - ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ์›์†Œ์˜ ์ง์„ ๊ณ„์‚ฐ
10+
* - ์ง์ด HashMap์— ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์‚ฌ
11+
*/
12+
class Solution {
13+
public int[] twoSum(int[] nums, int target) {
14+
Map <Integer, Integer> map = new HashMap<>();
15+
for (int i=0; i<nums.length; i++) {
16+
int pair = target-nums[i];
17+
if (map.containsKey(pair) && map.get(pair) != i) {
18+
return new int[]{i, map.get(pair)};
19+
}
20+
map.put(nums[i], i);
21+
}
22+
23+
return new int[]{};
24+
}
25+
}

0 commit comments

Comments
ย (0)