We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ecd7f5b commit 6e65d41Copy full SHA for 6e65d41
โtwo-sum/YuuuuuuYu.javaโ
@@ -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