Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d943434

Browse files
Add two sum to tutorial
1 parent f6714ff commit d943434

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎ArraysAndStrings/Tutorial.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,24 @@ public int[] sum (int[] a, int[] b) {
240240
return c;
241241
}
242242
```
243+
244+
**[Two Sum](https://github.com/ankurjuneja/React-Java-Concepts/blob/master/src/ArraysAndStrings/TwoSum.java)
245+
```
246+
void printTwoSum(int[] array, int k)
247+
{
248+
Set<Integer> set = new HashSet<>(); // O(n) -space
249+
for (int num : array) // O(n) - time
250+
{
251+
if (set.contains(k-num)) // O(1)
252+
{
253+
System.out.println("(" + (k-num) + "," + num + ")");
254+
// remove duplicate
255+
set.remove(k-num); // O(1)
256+
}
257+
else
258+
{
259+
set.add(num); // O(1)
260+
}
261+
}
262+
}
263+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /