Skip to content

Commit 9e1ff1a

Browse files
committed
updated tasks 1-22
1 parent b3a8167 commit 9e1ff1a

File tree

20 files changed

+138
-1396
lines changed

20 files changed

+138
-1396
lines changed

src/main/java/g0001_0100/s0001_two_sum/readme.md

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -35,66 +35,4 @@ You can return the answer in any order.
3535
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
3636
* **Only one valid answer exists.**
3737

38-
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code> time complexity?
39-
40-
To solve the Two Sum problem in Java using a `Solution` class, we'll follow these steps:
41-
42-
1. Define a `Solution` class with a method named `twoSum`.
43-
2. Inside the `twoSum` method, create a hashmap to store elements and their indices.
44-
3. Iterate through the array:
45-
- For each element, calculate the complement required to reach the target sum.
46-
- Check if the complement exists in the hashmap.
47-
- If found, return the indices of the current element and the complement.
48-
- If not found, add the current element and its index to the hashmap.
49-
4. Handle edge cases:
50-
- If no solution is found, return an empty array or null (depending on the problem requirements).
51-
52-
Here's the implementation:
53-
54-
```java
55-
import java.util.HashMap;
56-
57-
public class Solution {
58-
59-
public int[] twoSum(int[] nums, int target) {
60-
// Create a hashmap to store elements and their indices
61-
HashMap<Integer, Integer> map = new HashMap<>();
62-
63-
// Iterate through the array
64-
for (int i = 0; i < nums.length; i++) {
65-
int complement = target - nums[i];
66-
// Check if the complement exists in the hashmap
67-
if (map.containsKey(complement)) {
68-
// Return the indices of the current element and the complement
69-
return new int[]{map.get(complement), i};
70-
}
71-
// Add the current element and its index to the hashmap
72-
map.put(nums[i], i);
73-
}
74-
// If no solution is found, return an empty array or null
75-
return new int[]{};
76-
}
77-
78-
public static void main(String[] args) {
79-
Solution solution = new Solution();
80-
81-
// Test cases
82-
int[] nums1 = {2, 7, 11, 15};
83-
int target1 = 9;
84-
int[] result1 = solution.twoSum(nums1, target1);
85-
System.out.println("Example 1 Output: [" + result1[0] + ", " + result1[1] + "]");
86-
87-
int[] nums2 = {3, 2, 4};
88-
int target2 = 6;
89-
int[] result2 = solution.twoSum(nums2, target2);
90-
System.out.println("Example 2 Output: [" + result2[0] + ", " + result2[1] + "]");
91-
92-
int[] nums3 = {3, 3};
93-
int target3 = 6;
94-
int[] result3 = solution.twoSum(nums3, target3);
95-
System.out.println("Example 3 Output: [" + result3[0] + ", " + result3[1] + "]");
96-
}
97-
}
98-
```
99-
100-
This implementation provides a solution to the Two Sum problem with a time complexity of O(n), where n is the number of elements in the input array.
38+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code> time complexity?

src/main/java/g0001_0100/s0002_add_two_numbers/readme.md

Lines changed: 1 addition & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -32,102 +32,4 @@ You may assume the two numbers do not contain any leading zero, except the numbe
3232

3333
* The number of nodes in each linked list is in the range `[1, 100]`.
3434
* `0 <= Node.val <= 9`
35-
* It is guaranteed that the list represents a number that does not have leading zeros.
36-
37-
To solve the Add Two Numbers problem in Java using a `Solution` class, we'll follow these steps:
38-
39-
1. Define a `ListNode` class to represent nodes in a linked list.
40-
2. Define a `Solution` class with a method named `addTwoNumbers`.
41-
3. Inside the `addTwoNumbers` method, traverse both input linked lists simultaneously:
42-
- Keep track of a carry variable to handle cases where the sum of two digits exceeds 9.
43-
- Calculate the sum of the current nodes' values along with the carry.
44-
- Update the carry for the next iteration.
45-
- Create a new node with the sum % 10 and attach it to the result linked list.
46-
- Move to the next nodes in both input lists.
47-
4. After finishing the traversal, check if there is any remaining carry. If so, add a new node with the carry to the result.
48-
5. Return the head of the result linked list.
49-
50-
Here's the implementation:
51-
52-
```java
53-
class ListNode {
54-
int val;
55-
ListNode next;
56-
57-
ListNode() {}
58-
59-
ListNode(int val) {
60-
this.val = val;
61-
}
62-
63-
ListNode(int val, ListNode next) {
64-
this.val = val;
65-
this.next = next;
66-
}
67-
}
68-
69-
public class Solution {
70-
71-
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
72-
ListNode dummyHead = new ListNode();
73-
ListNode curr = dummyHead;
74-
int carry = 0;
75-
76-
while (l1 != null || l2 != null) {
77-
int sum = carry;
78-
if (l1 != null) {
79-
sum += l1.val;
80-
l1 = l1.next;
81-
}
82-
if (l2 != null) {
83-
sum += l2.val;
84-
l2 = l2.next;
85-
}
86-
curr.next = new ListNode(sum % 10);
87-
curr = curr.next;
88-
carry = sum / 10;
89-
}
90-
91-
if (carry > 0) {
92-
curr.next = new ListNode(carry);
93-
}
94-
95-
return dummyHead.next;
96-
}
97-
98-
// Helper method to print a linked list
99-
public void printList(ListNode head) {
100-
ListNode curr = head;
101-
while (curr != null) {
102-
System.out.print(curr.val + " ");
103-
curr = curr.next;
104-
}
105-
System.out.println();
106-
}
107-
108-
public static void main(String[] args) {
109-
Solution solution = new Solution();
110-
111-
// Test cases
112-
ListNode l1 = new ListNode(2, new ListNode(4, new ListNode(3)));
113-
ListNode l2 = new ListNode(5, new ListNode(6, new ListNode(4)));
114-
ListNode result1 = solution.addTwoNumbers(l1, l2);
115-
System.out.print("Example 1 Output: ");
116-
solution.printList(result1);
117-
118-
ListNode l3 = new ListNode(0);
119-
ListNode l4 = new ListNode(0);
120-
ListNode result2 = solution.addTwoNumbers(l3, l4);
121-
System.out.print("Example 2 Output: ");
122-
solution.printList(result2);
123-
124-
ListNode l5 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9)))))));
125-
ListNode l6 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9))));
126-
ListNode result3 = solution.addTwoNumbers(l5, l6);
127-
System.out.print("Example 3 Output: ");
128-
solution.printList(result3);
129-
}
130-
}
131-
```
132-
133-
This implementation provides a solution to the Add Two Numbers problem using linked lists in Java.
35+
* It is guaranteed that the list represents a number that does not have leading zeros.

src/main/java/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
Medium
44

5-
Given a string `s`, find the length of the **longest substring** without repeating characters.
5+
Given a string `s`, find the length of the **longest** **substring** without duplicate characters.
66

77
**Example 1:**
88

99
**Input:** s = "abcabcbb"
1010

1111
**Output:** 3
1212

13-
**Explanation:** The answer is "abc", with the length of 3.
13+
**Explanation:** The answer is "abc", with the length of 3. Note that `"bca"` and `"cab"` are also correct answers.
1414

1515
**Example 2:**
1616

@@ -28,76 +28,7 @@ Given a string `s`, find the length of the **longest substring** without repeati
2828

2929
**Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
3030

31-
**Example 4:**
32-
33-
**Input:** s = ""
34-
35-
**Output:** 0
36-
3731
**Constraints:**
3832

3933
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
40-
* `s` consists of English letters, digits, symbols and spaces.
41-
42-
To solve the Longest Substring Without Repeating Characters problem in Java using a `Solution` class, we'll follow these steps:
43-
44-
1. Define a `Solution` class with a method named `lengthOfLongestSubstring`.
45-
2. Initialize variables to keep track of the starting index of the substring (`start`), the maximum length (`maxLen`), and a hashmap to store characters and their indices.
46-
3. Iterate through the string `s`, and for each character:
47-
- Check if the character exists in the hashmap and its index is greater than or equal to the `start` index.
48-
- If found, update the `start` index to the index after the last occurrence of the character.
49-
- Update the maximum length if necessary.
50-
- Update the index of the current character in the hashmap.
51-
4. Return the maximum length found.
52-
5. Handle the edge case where the input string is empty.
53-
54-
Here's the implementation:
55-
56-
```java
57-
import java.util.HashMap;
58-
59-
public class Solution {
60-
61-
public int lengthOfLongestSubstring(String s) {
62-
// Initialize variables
63-
int start = 0;
64-
int maxLen = 0;
65-
HashMap<Character, Integer> map = new HashMap<>();
66-
67-
// Iterate through the string
68-
for (int end = 0; end < s.length(); end++) {
69-
char ch = s.charAt(end);
70-
// If the character exists in the hashmap and its index is greater than or equal to the start index
71-
if (map.containsKey(ch) && map.get(ch) >= start) {
72-
// Update the start index to the index after the last occurrence of the character
73-
start = map.get(ch) + 1;
74-
}
75-
// Update the maximum length if necessary
76-
maxLen = Math.max(maxLen, end - start + 1);
77-
// Update the index of the current character in the hashmap
78-
map.put(ch, end);
79-
}
80-
81-
return maxLen;
82-
}
83-
84-
public static void main(String[] args) {
85-
Solution solution = new Solution();
86-
87-
// Test cases
88-
String s1 = "abcabcbb";
89-
System.out.println("Example 1 Output: " + solution.lengthOfLongestSubstring(s1));
90-
91-
String s2 = "bbbbb";
92-
System.out.println("Example 2 Output: " + solution.lengthOfLongestSubstring(s2));
93-
94-
String s3 = "pwwkew";
95-
System.out.println("Example 3 Output: " + solution.lengthOfLongestSubstring(s3));
96-
97-
String s4 = "";
98-
System.out.println("Example 4 Output: " + solution.lengthOfLongestSubstring(s4));
99-
}
100-
}
101-
```
102-
103-
This implementation provides a solution to the Longest Substring Without Repeating Characters problem in Java.
34+
* `s` consists of English letters, digits, symbols and spaces.

src/main/java/g0001_0100/s0004_median_of_two_sorted_arrays/readme.md

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -22,97 +22,11 @@ The overall run time complexity should be `O(log (m+n))`.
2222

2323
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
2424

25-
**Example 3:**
26-
27-
**Input:** nums1 = [0,0], nums2 = [0,0]
28-
29-
**Output:** 0.00000
30-
31-
**Example 4:**
32-
33-
**Input:** nums1 = [], nums2 = [1]
34-
35-
**Output:** 1.00000
36-
37-
**Example 5:**
38-
39-
**Input:** nums1 = [2], nums2 = []
40-
41-
**Output:** 2.00000
42-
4325
**Constraints:**
4426

4527
* `nums1.length == m`
4628
* `nums2.length == n`
4729
* `0 <= m <= 1000`
4830
* `0 <= n <= 1000`
4931
* `1 <= m + n <= 2000`
50-
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
51-
52-
To solve the Median of Two Sorted Arrays problem in Java using a `Solution` class, we'll follow these steps:
53-
54-
1. Define a `Solution` class with a method named `findMedianSortedArrays`.
55-
2. Calculate the total length of the combined array (m + n).
56-
3. Determine the middle index or indices of the combined array based on its length (for both even and odd lengths).
57-
4. Implement a binary search algorithm to find the correct position for partitioning the two arrays such that elements to the left are less than or equal to elements on the right.
58-
5. Calculate the median based on the partitioned arrays.
59-
6. Handle edge cases where one or both arrays are empty or where the combined length is odd or even.
60-
7. Return the calculated median.
61-
62-
Here's the implementation:
63-
64-
```java
65-
public class Solution {
66-
67-
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
68-
int m = nums1.length;
69-
int n = nums2.length;
70-
int totalLength = m + n;
71-
int left = (totalLength + 1) / 2;
72-
int right = (totalLength + 2) / 2;
73-
return (findKth(nums1, 0, nums2, 0, left) + findKth(nums1, 0, nums2, 0, right)) / 2.0;
74-
}
75-
76-
private int findKth(int[] nums1, int i, int[] nums2, int j, int k) {
77-
if (i >= nums1.length) return nums2[j + k - 1];
78-
if (j >= nums2.length) return nums1[i + k - 1];
79-
if (k == 1) return Math.min(nums1[i], nums2[j]);
80-
81-
int midVal1 = (i + k / 2 - 1 < nums1.length) ? nums1[i + k / 2 - 1] : Integer.MAX_VALUE;
82-
int midVal2 = (j + k / 2 - 1 < nums2.length) ? nums2[j + k / 2 - 1] : Integer.MAX_VALUE;
83-
84-
if (midVal1 < midVal2) {
85-
return findKth(nums1, i + k / 2, nums2, j, k - k / 2);
86-
} else {
87-
return findKth(nums1, i, nums2, j + k / 2, k - k / 2);
88-
}
89-
}
90-
91-
public static void main(String[] args) {
92-
Solution solution = new Solution();
93-
94-
// Test cases
95-
int[] nums1_1 = {1, 3};
96-
int[] nums2_1 = {2};
97-
System.out.println("Example 1 Output: " + solution.findMedianSortedArrays(nums1_1, nums2_1));
98-
99-
int[] nums1_2 = {1, 2};
100-
int[] nums2_2 = {3, 4};
101-
System.out.println("Example 2 Output: " + solution.findMedianSortedArrays(nums1_2, nums2_2));
102-
103-
int[] nums1_3 = {0, 0};
104-
int[] nums2_3 = {0, 0};
105-
System.out.println("Example 3 Output: " + solution.findMedianSortedArrays(nums1_3, nums2_3));
106-
107-
int[] nums1_4 = {};
108-
int[] nums2_4 = {1};
109-
System.out.println("Example 4 Output: " + solution.findMedianSortedArrays(nums1_4, nums2_4));
110-
111-
int[] nums1_5 = {2};
112-
int[] nums2_5 = {};
113-
System.out.println("Example 5 Output: " + solution.findMedianSortedArrays(nums1_5, nums2_5));
114-
}
115-
}
116-
```
117-
118-
This implementation provides a solution to the Median of Two Sorted Arrays problem in Java with a runtime complexity of O(log(min(m, n))).
32+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>

0 commit comments

Comments
 (0)