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 4aa3204

Browse files
Chore: All Activites completed for Day-21
1 parent 219ce99 commit 4aa3204

File tree

2 files changed

+161
-6
lines changed

2 files changed

+161
-6
lines changed

‎Day21/Task.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ Welcome to Day 21! Today, we're tackling some classic problems on LeetCode. Thes
55
## Tasks/Activities 📝
66

77
### Activity 1: Two Sum ➕
8-
- [] **Task 1:** Solve the "Two Sum" problem on LeetCode.
8+
- [X] **Task 1:** Solve the "Two Sum" problem on LeetCode.
99
- Write a function that takes an array of numbers and a target number, and returns the indices of the two numbers that add up to the target.
1010
- Log the indices for a few test cases.
1111

1212
### Activity 2: Reverse Integer 🔄
13-
- [] **Task 2:** Solve the "Reverse Integer" problem on LeetCode.
13+
- [X] **Task 2:** Solve the "Reverse Integer" problem on LeetCode.
1414
- Write a function that takes an integer and returns it with its digits reversed.
1515
- Handle edge cases like negative numbers and numbers ending in zero.
1616
- Log the reversed integers for a few test cases.
1717

1818
### Activity 3: Palindrome Number 🔢
19-
- [] **Task 3:** Solve the "Palindrome Number" problem on LeetCode.
19+
- [X] **Task 3:** Solve the "Palindrome Number" problem on LeetCode.
2020
- Write a function that takes an integer and returns `true` if it is a palindrome, and `false` otherwise.
2121
- Log the result for a few test cases, including edge cases like negative numbers.
2222

2323
### Activity 4: Merge Two Sorted Lists 🔗
24-
- [] **Task 4:** Solve the "Merge Two Sorted Lists" problem on LeetCode.
24+
- [X] **Task 4:** Solve the "Merge Two Sorted Lists" problem on LeetCode.
2525
- Write a function that takes two sorted linked lists and returns a new sorted list by merging them.
2626
- Create a few test cases with linked lists and log the merged list.
2727

2828
### Activity 5: Valid Parentheses ✔️❌
29-
- [] **Task 5:** Solve the "Valid Parentheses" problem on LeetCode.
29+
- [X] **Task 5:** Solve the "Valid Parentheses" problem on LeetCode.
3030
- Write a function that takes a string containing just the characters '(', ')', '[', ']', '{', '}', and determines if the input string is valid.
3131
- A string is valid if open brackets are closed in the correct order.
3232
- Log the result for a few test cases.

‎Day21/index.js

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,177 @@
11
console.log("-------------------------------------------------");
22
console.log("Activity 1: ");
33

4+
// Task 1: Solve the "Two Sum" problem on LeetCode.
5+
// Write a function that takes an array of numbers and a target number, and returns the indices of the two numbers that add up to the target.
6+
// Log the indices for a few test cases.
7+
8+
9+
function twoSum(nums, target) {
10+
const map = new Map();
11+
for (let i = 0; i < nums.length; i++) {
12+
const complement = target - nums[i];
13+
if (map.has(complement)) {
14+
return [map.get(complement), i];
15+
}
16+
map.set(nums[i], i);
17+
}
18+
return [];
19+
}
20+
21+
console.log(twoSum([2, 7, 11, 15], 9));
22+
console.log(twoSum([3, 2, 4], 6));
23+
console.log(twoSum([3, 3], 6));
24+
console.log(twoSum([1, 5, 3, 4], 8));
425

526

627
console.log("-------------------------------------------------");
728
console.log("Activity 2: ");
829

30+
// Task 2: Solve the "Reverse Integer" problem on LeetCode.
31+
// Write a function that takes an integer and returns it with its digits reversed.
32+
// Handle edge cases like negative numbers and numbers ending in zero.
33+
// Log the reversed integers for a few test cases.
34+
35+
function reverseInteger(x) {
36+
const isNegative = x < 0;
37+
let reversed = parseInt(Math.abs(x).toString().split('').reverse().join(''));
38+
39+
if (reversed > 2**31 - 1) {
40+
return 0;
41+
}
42+
43+
return isNegative ? -reversed : reversed;
44+
}
45+
46+
console.log(reverseInteger(123));
47+
console.log(reverseInteger(-123));
48+
console.log(reverseInteger(120));
49+
console.log(reverseInteger(0));
50+
console.log(reverseInteger(1534236469));
51+
952

1053

1154
console.log("-------------------------------------------------");
1255
console.log("Activity 3: ");
1356

57+
// Task 3: Solve the "Palindrome Number" problem on LeetCode.
58+
// Write a function that takes an integer and returns true if it is a palindrome, and false otherwise.
59+
// Log the result for a few test cases, including edge cases like negative numbers.
60+
61+
function isPalindrome(x) {
62+
if (x < 0) return false;
63+
const str = x.toString();
64+
return str === str.split('').reverse().join('');
65+
}
66+
67+
console.log(isPalindrome(101));
68+
console.log(isPalindrome(1012));
69+
console.log(isPalindrome(10070010));
70+
console.log(isPalindrome(23456543));
1471

1572

1673
console.log("-------------------------------------------------");
1774
console.log("Activity 4: ");
1875

76+
// Task 4: Solve the "Merge Two Sorted Lists" problem on LeetCode.
77+
// Write a function that takes two sorted linked lists and returns a new sorted list by merging them.
78+
// Create a few test cases with linked lists and log the merged list.
79+
80+
class ListNode {
81+
constructor(val = 0, next = null) {
82+
this.val = val;
83+
this.next = next;
84+
}
85+
}
86+
87+
function mergeTwoLists(l1, l2) {
88+
const dummy = new ListNode();
89+
let current = dummy;
90+
91+
while (l1 !== null && l2 !== null) {
92+
if (l1.val < l2.val) {
93+
current.next = l1;
94+
l1 = l1.next;
95+
} else {
96+
current.next = l2;
97+
l2 = l2.next;
98+
}
99+
current = current.next;
100+
}
101+
// Attach the remaining nodes
102+
current.next = l1 !== null ? l1 : l2;
103+
return dummy.next;
104+
}
105+
106+
// function to create a linked list from an array
107+
function createLinkedList(arr) {
108+
let head = new ListNode(arr[0]);
109+
let current = head;
110+
for (let i = 1; i < arr.length; i++) {
111+
current.next = new ListNode(arr[i]);
112+
current = current.next;
113+
}
114+
return head;
115+
}
116+
117+
// Helper function to print linked list as an array
118+
function printLinkedList(head) {
119+
const result = [];
120+
while (head !== null) {
121+
result.push(head.val);
122+
head = head.next;
123+
}
124+
return result;
125+
}
126+
const list1 = createLinkedList([1, 2, 4]);
127+
const list2 = createLinkedList([1, 3, 4]);
128+
const mergedList = mergeTwoLists(list1, list2);
129+
console.log(printLinkedList(mergedList));
130+
131+
const list3 = createLinkedList([5, 6, 7]);
132+
const list4 = createLinkedList([1, 2, 8]);
133+
const mergedList2 = mergeTwoLists(list3, list4);
134+
console.log(printLinkedList(mergedList2));
135+
136+
const list5 = createLinkedList([]);
137+
const list6 = createLinkedList([0]);
138+
const mergedList3 = mergeTwoLists(list5, list6);
139+
console.log(printLinkedList(mergedList3));
19140

20141

21142
console.log("-------------------------------------------------");
22-
console.log("Activity 5: ");
143+
console.log("Activity 5: ");
144+
145+
// Task 5: Solve the "Valid Parentheses" problem on LeetCode.
146+
// Write a function that takes a string containing just the characters '(', ')', '[', ']', '{', '}', and determines if the input string is valid.
147+
// A string is valid if open brackets are closed in the correct order.
148+
// Log the result for a few test cases.
149+
150+
function isValid(s) {
151+
const stack = [];
152+
const map = {
153+
')': '(',
154+
']': '[',
155+
'}': '{'
156+
};
157+
158+
for (let char of s) {
159+
if (char === '(' || char === '[' || char === '{') {
160+
stack.push(char);
161+
} else if (stack.length > 0 && stack[stack.length - 1] === map[char]) {
162+
stack.pop();
163+
} else {
164+
return false;
165+
}
166+
}
167+
168+
return stack.length === 0;
169+
}
170+
171+
console.log(isValid("()"));
172+
console.log(isValid("()[]{}"));
173+
console.log(isValid("(]"));
174+
console.log(isValid("([)]"));
175+
console.log(isValid("{[]}"));
176+
177+
console.log("-------------------------------------------------");

0 commit comments

Comments
(0)

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