Skip to content

Instantly share code, notes, and snippets.

@suca19
suca19 / LeetCode
Created May 7, 2025 22:38
Add Two Numbers
/**
* 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)
* }
* }
@suca19
suca19 / LeetCode
Created January 24, 2025 21:45
Find the Index of the First Occurrence in a String
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);
@suca19
suca19 / LeetCode
Created November 30, 2024 23:53
Remove Element
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;
}
@suca19
suca19 / OOP
Created November 25, 2024 23:13
SOLID Principles
##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.
@suca19
suca19 / LeetCode
Last active November 21, 2024 22:17
Remove Duplicates from Sorted Array
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++) {
@suca19
suca19 / LeetCode
Created November 15, 2024 23:01
Merge two sorted list
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;
@suca19
suca19 / LeetCode
Created November 13, 2024 17:24
Valid Parentheses
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[] = [];
@suca19
suca19 / LeetCode
Created May 20, 2024 20:30
Longest Common Prefix
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
@suca19
suca19 / LeetCode
Last active March 30, 2024 16:24
Roman to Integer
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;
@suca19
suca19 / LeetCode
Last active March 27, 2024 17:52
Palindrome Number
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;