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 cd16480 commit 19e2142Copy full SHA for 19e2142
Easy/0001-two-sum.java
@@ -1 +1,27 @@
1
+/**
2
+ * Problem: 1. Two Sum
3
+ * Difficulty: Easy
4
+ * URL: https://leetcode.com/problems/two-sum/
5
+ *
6
+ * Approach:
7
+ * - Use a HashMap to store visited numbers and their indices.
8
+ * - Check if target - current exists in map.
9
10
+ * Time Complexity: O(n)
11
+ * Space Complexity: O(n)
12
+ */
13
+
14
+class Solution {
15
+ public int[] twoSum(int[] nums, int target) {
16
+ Map<Integer, Integer> map = new HashMap<>();
17
+ for (int i = 0; i < nums.length; i++) {
18
+ int complement = target - nums[i];
19
+ if (map.containsKey(complement)) {
20
+ return new int[] { map.get(complement), i };
21
+ }
22
+ map.put(nums[i], i);
23
24
+ return new int[] {};
25
26
+}
27
0 commit comments