aa Flashcards
Two Sum
Uses a hash map to store seen numbers and their indices; for each element, checks if the complement (target minus current value) exists in the map, returning the pair in a single pass.
Palindrome Number
Converts the integer into its string form and compares it to its reverse to determine if it reads the same forwards and backwards, immediately excluding negative values.
Roman to Integer
Processes the Roman numeral from right to left, adding values but subtracting when a smaller symbol precedes a larger one to handle subtractive notation.
Longest Common Prefix
Starts with the first string as a candidate prefix and iteratively shortens it until all other strings in the array share that starting substring.
Valid parentheses
Uses a stack to push opening brackets and verifies each closing bracket matches the most recent opener, ensuring all brackets are paired correctly and the stack is empty at the end.
Merge Two Sorted Lists
Recursively compares the head nodes of two sorted linked lists, appending the smaller node to the merged result and proceeding until one list is exhausted.
Remove duplicates from sorted array
Uses two pointers—one to build the unique portion and one to scan through the array—overwriting duplicates in place and returning the length of the unique segment.
Remove Element
Iterates through the array with a write pointer that only advances when encountering values not equal to the target, effectively overwriting unwanted elements in place.
Find the index of the First Occurence in a string
Leverages built‑in substring search to return the index of the first occurrence of the needle in the haystack, or –1 if it’s not found.
Search insert position
Applies binary search on a sorted array to find either the exact target or the correct insertion index in O(log n) time.
Length of Last word
Splits the string on whitespace and returns the length of the final nonempty segment, naturally ignoring any trailing spaces.
Plus one
Starts from the least significant digit, increments it while handling carry; if all digits roll over (e.g., 999 → 1000), prepends a new digit.
Add Binary
Simulates manual binary addition by iterating from the end of two binary strings, summing bits and carry, then constructing the result from least to most significant bit.
Sqrt(x)
Uses binary search to find the greatest integer whose square is ≤ x by narrowing down the search interval until convergence.
Climbing stairs
Recognizes the recurrence identical to Fibonacci: the number of ways to reach step n equals the sum of ways to reach steps n–1 and n–2, computed iteratively.