-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path01. Leetcode Easy.js
147 lines (118 loc) · 3.7 KB
/
01. Leetcode Easy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Activity 1: Two Sum ✅
// - Task 1: Solve the "Two Sum" problem on LeetCode.
// Function to solve Two Sum problem
function twoSum(nums, target) {
const numMap = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (numMap.has(complement)) {
return [numMap.get(complement), i];
}
numMap.set(nums[i], i);
}
return [];
}
// Test cases
console.log(twoSum([2, 7, 11, 15], 9)); // Output: [0, 1]
console.log(twoSum([3, 2, 4], 6)); // Output: [1, 2]
console.log(twoSum([3, 3], 6)); // Output: [0, 1]
console.log("***Task 1 Ended Here!✅***");
// ********************************* //
// Activity 2: Reverse Integer ✅
// - Task 2: Solve the "Reverse Integer" problem on LeetCode.
// Function to reverse an integer
function reverseInteger(x) {
const isNegative = x < 0;
let reversed = parseInt(Math.abs(x).toString().split("").reverse().join(""));
if (reversed > 2 ** 31 - 1) return 0; // Handle overflow for 32-bit integer
return isNegative ? -reversed : reversed;
}
// Test cases
console.log(reverseInteger(123)); // Output: 321
console.log(reverseInteger(-456)); // Output: -654
console.log(reverseInteger(130)); // Output: 31
console.log(reverseInteger(0)); // Output: 0
console.log("***Task 2 Ended Here!✅***");
// ********************************* //
// Activity 3: Palindrome Number ✅
// - Task 3: Solve the "Palindrome Number" problem on LeetCode.
// Function to check if an integer is a palindrome
function isPalindrome(x) {
if (x < 0) return false;
const str = x.toString();
return str === str.split("").reverse().join("");
}
// Test cases
console.log(isPalindrome(121)); // Output: true
console.log(isPalindrome(-121)); // Output: false
console.log(isPalindrome(10)); // Output: false
console.log(isPalindrome(0)); // Output: true
console.log("***Task 3 Ended Here!✅***");
// ********************************* //
// Activity 4: Merge Two Sorted Lists ✅
// - Task 4: Solve the "Merge Two Sorted Lists" problem on LeetCode.
// Definition for singly-linked list.
function ListNode(val, next = null) {
this.val = val;
this.next = next;
}
// Function to merge two sorted linked lists
function mergeTwoLists(l1, l2) {
const dummy = new ListNode(0);
let current = dummy;
while (l1 !== null && l2 !== null) {
if (l1.val < l2.val) {
current.next = l1;
l1 = l1.next;
} else {
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
current.next = l1 !== null ? l1 : l2;
return dummy.next;
}
// Test cases
function printList(node) {
const values = [];
while (node) {
values.push(node.val);
node = node.next;
}
console.log(values.join(" -> "));
}
const l1 = new ListNode(1, new ListNode(2, new ListNode(4)));
const l2 = new ListNode(1, new ListNode(3, new ListNode(4)));
const mergedList = mergeTwoLists(l1, l2);
printList(mergedList); // Output: 1 -> 1 -> 2 -> 3 -> 4 -> 4
console.log("***Task 4 Ended Here!✅***");
// ********************************* //
// Activity 5: Valid Parentheses ✅
// - Task 5: Solve the "Valid Parentheses" problem on LeetCode.
// Function to check if a string of parentheses is valid
function isValid(s) {
const stack = [];
const map = {
"(": ")",
"{": "}",
"[": "]",
};
for (let char of s) {
if (map[char]) {
stack.push(map[char]);
} else {
if (stack.pop() !== char) {
return false;
}
}
}
return stack.length === 0;
}
// Test cases
console.log(isValid("()")); // Output: true
console.log(isValid("()[]{}")); // Output: true
console.log(isValid("(]")); // Output: false
console.log(isValid("([)]")); // Output: false
console.log(isValid("{[]}")); // Output: true
console.log("***Task 5 Ended Here!✅***");