Easy
LeetCode 263: Ugly Number
·1 min
LeetCode
Easy
Math
LeetCode 263 note: only positive numbers can be ugly; divide out every factor 2, 3 and 5 and check whether 1 remains
LeetCode 258: Add Digits
·1 min
LeetCode
Easy
Math
Simulation
Number-Theory
LeetCode 258 note: sum the digits repeatedly until one remains; the O(1) answer is the digital root 1 + (n - 1) % 9
LeetCode 257: Binary Tree Paths
·1 min
LeetCode
Easy
String
Backtracking
Tree
Depth-First-Search
LeetCode 257 note: DFS carrying the path string down, adding the whole path at each leaf
LeetCode 242: Valid Anagram
·2 mins
LeetCode
Easy
Hash-Table
String
Sorting
LeetCode 242 note: two strings are anagrams exactly when their character counts match; compare Counters or sorted strings
LeetCode 234: Palindrome Linked List
·2 mins
LeetCode
Easy
Linked-List
Two-Pointers
Stack
Recursion
LeetCode 234 note: find the middle with fast and slow pointers, reverse the second half, and compare from both ends with O(1) extra space
LeetCode 232: Implement Queue using Stacks
·1 min
LeetCode
Easy
Stack
Design
Queue
LeetCode 232 note: two stacks, where in receives pushes and out is refilled from in only when empty, amortized O(1)
LeetCode 231: Power of Two
·2 mins
LeetCode
Easy
Math
Bit-Manipulation
Recursion
LeetCode 231 note: a power of two has exactly one set bit, and n & (n - 1) clears the lowest one, so the result is 0 exactly then; n must be positive
LeetCode 228: Summary Ranges
·2 mins
LeetCode
Easy
Array
LeetCode 228 note: scan once, extending the current range when a number continues the previous one and opening a new range otherwise, then format
LeetCode 226: Invert Binary Tree
·1 min
LeetCode
Easy
Tree
Depth-First-Search
Breadth-First-Search
Binary-Tree
LeetCode 226 note: swap every node’s children, using a simultaneous assignment so the already-changed side is not reused
LeetCode 225: Implement Stack using Queues
·2 mins
LeetCode
Easy
Stack
Design
Queue
LeetCode 225 note: one queue suffices: after a push, rotate all earlier elements to the back so the new element sits at the front
LeetCode 219: Contains Duplicate II
·2 mins
LeetCode
Easy
Array
Hash-Table
Sliding-Window
LeetCode 219 note: a hash map stores each value’s last index; only the most recent occurrence needs a distance check
LeetCode 217: Contains Duplicate
·1 min
LeetCode
Easy
Array
Hash-Table
Sorting
LeetCode 217 note: if converting to a set shortens the array, there is a duplicate