Skip to content

Commit 9ecb343

Browse files
authored
add comments
1 parent 340188b commit 9ecb343

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

contains-duplicate/samcho0608.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import java.util.HashSet;
22

3+
// link: https://leetcode.com/problems/contains-duplicate/description/
4+
// difficulty: Easy
35
class Solution {
4-
// return: does any value appear more than once in the array
5-
// Time Complexity: O(n)
6-
// Space Complexity: O(n)
6+
// Problem:
7+
// * return: does any value appear more than once in the array
8+
// Solution:
9+
// * Time Complexity: O(N)
10+
// * Space Complexity: O(N)
711
public boolean containsDuplicate(int[] nums) {
12+
// Space Complexity: O(N)
813
HashSet<Integer> uniqNums = new HashSet<>();
914

15+
// Time Complexity: O(N)
1016
for(int num : nums) {
1117
if(!uniqNums.add(num)) return true;
1218
}

house-robber/samcho0608.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// link: https://leetcode.com/problems/house-robber/description/
2+
// difficulty: Medium
13
class Solution {
24
// Problem:
35
// * can't rob two adj houses in the same night

longest-consecutive-sequence/samcho0608.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import java.util.HashSet;
22
import java.util.Set;
33

4+
// link: https://leetcode.com/problems/longest-consecutive-sequence/
5+
// difficulty: Medium
46
class Solution {
57
// Problem:
68
// * nums is unsorted

top-k-frequent-elements/samcho0608.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import java.util.*;
22

3+
// link: https://leetcode.com/problems/top-k-frequent-elements/description/
4+
// difficulty: Medium
5+
36
// Time complexity: O(Nlogk)
47
// Space complexity: O(N)
58
class Solution1 {

two-sum/samcho0608.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
import java.util.HashMap;
22
import java.util.Map;
33

4+
// link: https://leetcode.com/problems/two-sum/description/
5+
// difficulty: Easy
46
class Solution {
5-
// return: indices of two numbers that add up to `target`
7+
// Problem
68
// * exactly one solution
79
// * must use index only once
8-
9-
// Time Complexity: O(n)
10-
// Space Complexity: O(n)
10+
// * return: indices of two numbers that add up to `target`
11+
// Solution:
12+
// * Time Complexity: O(N)
13+
// * Space Complexity: O(N)
1114
public int[] twoSum(int[] nums, int target) {
15+
// Space Complexity: O(N)
1216
Map<Integer, Integer> indexByNum = new HashMap<>();
1317

18+
// Time Complexity: O(N)
1419
for(int i = 0; i < nums.length; i++) {
1520
int numI = nums[i];
1621
indexByNum.put(numI, i);
1722
}
1823

24+
// Time Complexity: O(N)
1925
for(int i = 0; i < nums.length; i++) {
2026
int numI = nums[i];
2127
Integer compl = indexByNum.getOrDefault(target-numI, null);
22-
if(compl != null && i != compl) return new int[] {i, compl};
28+
29+
if(compl != null && i != compl)
30+
return new int[] {i, compl};
2331
}
2432

2533
return null;

0 commit comments

Comments
 (0)