Skip to content

Commit 8b99a89

Browse files
committedApr 6, 2024·
Improved java
1 parent 139ea57 commit 8b99a89

File tree

103 files changed

+3557
-1575
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+3557
-1575
lines changed
 

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

+52-11
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,64 @@ You can return the answer in any order.
4040

4141
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code> time complexity?
4242

43-
## Solution
43+
To solve the Two Sum problem in Java using a `Solution` class, we'll follow these steps:
44+
45+
1. Define a `Solution` class with a method named `twoSum`.
46+
2. Inside the `twoSum` method, create a hashmap to store elements and their indices.
47+
3. Iterate through the array:
48+
- For each element, calculate the complement required to reach the target sum.
49+
- Check if the complement exists in the hashmap.
50+
- If found, return the indices of the current element and the complement.
51+
- If not found, add the current element and its index to the hashmap.
52+
4. Handle edge cases:
53+
- If no solution is found, return an empty array or null (depending on the problem requirements).
54+
55+
Here's the implementation:
4456

4557
```java
4658
import java.util.HashMap;
47-
import java.util.Map;
4859

4960
public class Solution {
50-
public int[] twoSum(int[] numbers, int target) {
51-
Map<Integer, Integer> indexMap = new HashMap<>();
52-
for (int i = 0; i < numbers.length; i++) {
53-
Integer requiredNum = target - numbers[i];
54-
if (indexMap.containsKey(requiredNum)) {
55-
return new int[] {indexMap.get(requiredNum), i};
61+
62+
public int[] twoSum(int[] nums, int target) {
63+
// Create a hashmap to store elements and their indices
64+
HashMap<Integer, Integer> map = new HashMap<>();
65+
66+
// Iterate through the array
67+
for (int i = 0; i < nums.length; i++) {
68+
int complement = target - nums[i];
69+
// Check if the complement exists in the hashmap
70+
if (map.containsKey(complement)) {
71+
// Return the indices of the current element and the complement
72+
return new int[]{map.get(complement), i};
5673
}
57-
indexMap.put(numbers[i], i);
74+
// Add the current element and its index to the hashmap
75+
map.put(nums[i], i);
5876
}
59-
return new int[] {-1, -1};
77+
// If no solution is found, return an empty array or null
78+
return new int[]{};
79+
}
80+
81+
public static void main(String[] args) {
82+
Solution solution = new Solution();
83+
84+
// Test cases
85+
int[] nums1 = {2, 7, 11, 15};
86+
int target1 = 9;
87+
int[] result1 = solution.twoSum(nums1, target1);
88+
System.out.println("Example 1 Output: [" + result1[0] + ", " + result1[1] + "]");
89+
90+
int[] nums2 = {3, 2, 4};
91+
int target2 = 6;
92+
int[] result2 = solution.twoSum(nums2, target2);
93+
System.out.println("Example 2 Output: [" + result2[0] + ", " + result2[1] + "]");
94+
95+
int[] nums3 = {3, 3};
96+
int target3 = 6;
97+
int[] result3 = solution.twoSum(nums3, target3);
98+
System.out.println("Example 3 Output: [" + result3[0] + ", " + result3[1] + "]");
6099
}
61100
}
62-
```
101+
```
102+
103+
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.

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

+82-26
Original file line numberDiff line numberDiff line change
@@ -37,44 +37,100 @@ You may assume the two numbers do not contain any leading zero, except the numbe
3737
* `0 <= Node.val <= 9`
3838
* It is guaranteed that the list represents a number that does not have leading zeros.
3939

40-
## Solution
40+
To solve the Add Two Numbers problem in Java using a `Solution` class, we'll follow these steps:
41+
42+
1. Define a `ListNode` class to represent nodes in a linked list.
43+
2. Define a `Solution` class with a method named `addTwoNumbers`.
44+
3. Inside the `addTwoNumbers` method, traverse both input linked lists simultaneously:
45+
- Keep track of a carry variable to handle cases where the sum of two digits exceeds 9.
46+
- Calculate the sum of the current nodes' values along with the carry.
47+
- Update the carry for the next iteration.
48+
- Create a new node with the sum % 10 and attach it to the result linked list.
49+
- Move to the next nodes in both input lists.
50+
4. After finishing the traversal, check if there is any remaining carry. If so, add a new node with the carry to the result.
51+
5. Return the head of the result linked list.
52+
53+
Here's the implementation:
4154

4255
```java
43-
import com_github_leetcode.ListNode;
44-
45-
/*
46-
* Definition for singly-linked list.
47-
* public class ListNode {
48-
* int val;
49-
* ListNode next;
50-
* ListNode(int x) { val = x; }
51-
* }
52-
*/
56+
class ListNode {
57+
int val;
58+
ListNode next;
59+
60+
ListNode() {}
61+
62+
ListNode(int val) {
63+
this.val = val;
64+
}
65+
66+
ListNode(int val, ListNode next) {
67+
this.val = val;
68+
this.next = next;
69+
}
70+
}
71+
5372
public class Solution {
73+
5474
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
55-
ListNode dummyHead = new ListNode(0);
56-
ListNode p = l1;
57-
ListNode q = l2;
75+
ListNode dummyHead = new ListNode();
5876
ListNode curr = dummyHead;
5977
int carry = 0;
60-
while (p != null || q != null) {
61-
int x = (p != null) ? p.val : 0;
62-
int y = (q != null) ? q.val : 0;
63-
int sum = carry + x + y;
64-
carry = sum / 10;
65-
curr.next = new ListNode(sum % 10);
66-
curr = curr.next;
67-
if (p != null) {
68-
p = p.next;
78+
79+
while (l1 != null || l2 != null) {
80+
int sum = carry;
81+
if (l1 != null) {
82+
sum += l1.val;
83+
l1 = l1.next;
6984
}
70-
if (q != null) {
71-
q = q.next;
85+
if (l2 != null) {
86+
sum += l2.val;
87+
l2 = l2.next;
7288
}
89+
curr.next = new ListNode(sum % 10);
90+
curr = curr.next;
91+
carry = sum / 10;
7392
}
93+
7494
if (carry > 0) {
7595
curr.next = new ListNode(carry);
7696
}
97+
7798
return dummyHead.next;
7899
}
100+
101+
// Helper method to print a linked list
102+
public void printList(ListNode head) {
103+
ListNode curr = head;
104+
while (curr != null) {
105+
System.out.print(curr.val + " ");
106+
curr = curr.next;
107+
}
108+
System.out.println();
109+
}
110+
111+
public static void main(String[] args) {
112+
Solution solution = new Solution();
113+
114+
// Test cases
115+
ListNode l1 = new ListNode(2, new ListNode(4, new ListNode(3)));
116+
ListNode l2 = new ListNode(5, new ListNode(6, new ListNode(4)));
117+
ListNode result1 = solution.addTwoNumbers(l1, l2);
118+
System.out.print("Example 1 Output: ");
119+
solution.printList(result1);
120+
121+
ListNode l3 = new ListNode(0);
122+
ListNode l4 = new ListNode(0);
123+
ListNode result2 = solution.addTwoNumbers(l3, l4);
124+
System.out.print("Example 2 Output: ");
125+
solution.printList(result2);
126+
127+
ListNode l5 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9)))))));
128+
ListNode l6 = new ListNode(9, new ListNode(9, new ListNode(9, new ListNode(9)))));
129+
ListNode result3 = solution.addTwoNumbers(l5, l6);
130+
System.out.print("Example 3 Output: ");
131+
solution.printList(result3);
132+
}
79133
}
80-
```
134+
```
135+
136+
This implementation provides a solution to the Add Two Numbers problem using linked lists in Java.

0 commit comments

Comments
 (0)
Please sign in to comment.