Skip to content

Commit bafda67

Browse files
committed
Modified 3 solutions
1 parent a32f72b commit bafda67

File tree

3 files changed

+39
-53
lines changed

3 files changed

+39
-53
lines changed

Easy/Jewels and Stones.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
class Solution {
22
public int numJewelsInStones(String J, String S) {
3-
Map<Character, Integer> mapJ = getMap(J);
4-
Map<Character, Integer> mapS = getMap(S);
3+
Set<Character> set = new HashSet<>();
4+
for (char c : J.toCharArray()) {
5+
set.add(c);
6+
}
57
int count = 0;
6-
for (Character key : mapJ.keySet()) {
7-
count += mapS.getOrDefault(key, 0);
8+
for (char c : S.toCharArray()) {
9+
if (set.contains(c)) {
10+
count++;
11+
}
812
}
913
return count;
1014
}
11-
12-
private Map<Character, Integer> getMap(String s) {
13-
Map<Character, Integer> map = new HashMap<>();
14-
for (char c : s.toCharArray()) {
15-
map.put(c, map.getOrDefault(c, 0) + 1);
16-
}
17-
return map;
18-
}
1915
}

Medium/Remove Nth Node From End of List.java

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,31 @@
33
* public class ListNode {
44
* int val;
55
* ListNode next;
6-
* ListNode(int x) { val = x; }
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
79
* }
810
*/
911
class Solution {
10-
public ListNode removeNthFromEnd(ListNode head, int n) {
11-
12-
if (head == null || head.next == null) return null;
13-
14-
ListNode curr = head;
15-
int i = 0;
16-
17-
while (curr != null) {
18-
curr = curr.next;
19-
i++;
20-
}
21-
22-
if (i == n) {
23-
return head.next;
24-
}
25-
26-
n = i - n;
27-
i = 1;
28-
curr = head;
29-
30-
while (i != n) {
31-
curr = curr.next;
32-
i++;
33-
}
34-
curr.next = curr.next.next;
35-
36-
return head;
12+
public ListNode removeNthFromEnd(ListNode head, int n) {
13+
if (head == null || n == 0) {
14+
return head;
3715
}
16+
ListNode slow = head;
17+
int count = 0;
18+
while (count < n) {
19+
slow = slow.next;
20+
count++;
21+
}
22+
if (slow == null) {
23+
return head.next;
24+
}
25+
ListNode fast = head;
26+
while (slow.next != null) {
27+
slow = slow.next;
28+
fast = fast.next;
29+
}
30+
fast.next = fast.next.next;
31+
return head;
32+
}
3833
}

Medium/Subsets.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
class Solution {
22
public List<List<Integer>> subsets(int[] nums) {
33
List<List<Integer>> list = new ArrayList<>();
4-
List<Integer> curr = new ArrayList<>();
5-
helper(list, curr, nums, 0, nums.length, new boolean[nums.length]);
4+
helper(nums, list, new ArrayList<>(), 0);
65
return list;
7-
}
6+
}
87

9-
private void helper(List<List<Integer>> list, List<Integer> curr, int[] nums, int idx, int n, boolean[] seen) {
8+
private void helper(int[] nums, List<List<Integer>> list, List<Integer> curr, int idx) {
109
list.add(new ArrayList<>(curr));
11-
if (idx >= n) {
10+
if (idx >= nums.length) {
1211
return;
1312
}
14-
for (int i = idx; i < n; i++) {
15-
if (!seen[i]) {
16-
seen[i] = true;
17-
curr.add(nums[i]);
18-
helper(list, curr, nums, i + 1, n, seen);
19-
seen[i] = false;
20-
curr.remove(curr.size() - 1);
21-
}
13+
for (int i = idx; i < nums.length; i++) {
14+
curr.add(nums[i]);
15+
helper(nums, list, curr, i + 1);
16+
curr.remove(curr.size() - 1);
2217
}
2318
}
2419
}

0 commit comments

Comments
 (0)