This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* class ListNode { | |
* val: number | |
* next: ListNode | null | |
* constructor(val?: number, next?: ListNode | null) { | |
* this.val = (val===undefined ? 0 : val) | |
* this.next = (next===undefined ? null : next) | |
* } | |
* } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int strStr(string haystack, string needle) { | |
// Check if needle is an empty string | |
if (needle.empty()) { | |
return 0; // If needle is an empty string, return 0 (based on problem's description) | |
} | |
// Use find() to get the index of the first occurrence of needle in haystack | |
size_t index = haystack.find(needle); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function removeElement(nums: number[], val: number): number { | |
let k = 0; // Counter for elements not equal to val | |
for (let i = 0; i < nums.length; i++) { | |
if (nums[i] !== val) { | |
nums[k] = nums[i]; // Place the non-val element at index k | |
k++; | |
} | |
} | |
return k; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##What is SOLID? | |
The abbreviation SOLID stands for the five main Object Oriented Programming concepts. Writing readable, flexible and well-structured code is made simpler by following these guidelines. | |
Let us explore each of them. | |
Single Responsibility Principle (SRP) | |
Imagine you have a toolbox. Each tool has a specific job. Similarly, in programming, a class or module should have one main job. It should do one thing and do it well. This makes our code easier to understand and manage. | |
Open-Closed Principle (OCP) | |
Think of a book with blank pages. You can add your own story, but you can’t change the existing text. Similarly, once a class or module is written, it should be open for extension but closed for modification. This prevents unintended side effects and keeps our code stable. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function removeDuplicates(nums: number[]): number { | |
if (nums.length === 0) { | |
return 0; | |
} | |
// Pointer for the position of the last unique element | |
let j = 0; | |
// Iterate through the array starting from the second element | |
for (let i = 1; i < nums.length; i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null { | |
// Create a dummy node to simplify edge cases | |
let dummy = new ListNode(); | |
let current = dummy; | |
// Traverse both lists | |
while (list1 !== null && list2 !== null) { | |
if (list1.val <= list2.val) { | |
current.next = list1; | |
list1 = list1.next; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isValid(s: string): boolean { | |
// Dictionary to hold the mappings of closing to opening brackets | |
const bracketMap: { [key: string]: string } = { | |
')': '(', | |
'}': '{', | |
']': '[' | |
}; | |
// Stack to keep track of opening brackets | |
const stack: string[] = []; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var longestCommonPrefix = function(strs) { | |
if (strs.length === 0) return ""; // If the array is empty, return an empty string | |
// Iterate through the characters of the first string | |
for (let i = 0; i < strs[0].length; i++) { | |
const char = strs[0][i]; // Get the character at position i in the first string | |
// Iterate through the rest of the strings in the array | |
for (let j = 1; j < strs.length; j++) { | |
// If the current character doesn't match or we reached the end of any string, return the prefix found so far |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function romanToInt(s: string): number { | |
// Split the input string into an array of characters and reverse it | |
const reverseString: string[] = s.split("").reverse(); | |
// Define an array of tuples representing Roman numeral symbols and their corresponding values | |
const index: [string, number][] = [["I", 1], ["V", 5], ["X", 10], ["L", 50], ["C", 100], ["D", 500], ["M", 1000]]; | |
// Initialize a variable to store the resulting integer value | |
let result: number = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isPalindrome(x: number): boolean { | |
// Convert the number to a string to manipulate its characters | |
const stringNumber = x.toString(); | |
// Check if the string is equal to its reverse | |
if (stringNumber === stringNumber.split("").reverse().join("")) { | |
// If the string is equal to its reverse, it is a palindrome, so return true | |
return true; |
NewerOlder