Math
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 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 202: Happy Number
·1 min
LeetCode
Easy
Hash-Table
Math
Two-Pointers
Floyds-Cycle-Finding-Algorithm
LeetCode 202 note: repeatedly take the sum of squared digits, which reaches 1 or enters a cycle; detect it with a set of seen numbers or fast and slow pointers
LeetCode 171: Excel Sheet Column Number
·1 min
LeetCode
Easy
Math
String
LeetCode 171 note: scan left to right with res = res × 26 + letter value (A = 1)
LeetCode 168: Excel Sheet Column Title
·2 mins
LeetCode
Easy
Math
String
LeetCode 168 note: Excel columns are base 26 without a zero digit; take (n - 1) % 26 as the letter, then n = (n - 1) // 26
LeetCode 69: Sqrt(x)
·1 min
LeetCode
Easy
Math
Binary-Search
Newtons-Method
LeetCode 69 note: find the largest mid with mid² <= x; when a closed-interval search ends, r is the answer
LeetCode 67: Add Binary
·2 mins
LeetCode
Easy
Math
String
Bit-Manipulation
Simulation
LeetCode 67 note: add from the ends of both strings digit by digit, with carry sum // 2 and digit sum % 2
LeetCode 66: Plus One
·2 mins
LeetCode
Easy
Array
Math
LeetCode 66 note: add one from the last digit, turning 9s into 0s while carrying, and prepend 1 when every digit was 9
LeetCode 2: Add Two Numbers
·2 mins
LeetCode
Medium
Linked-List
Math
Recursion
LeetCode 2 note: digits are stored in reverse, so add from the head digit by digit with a carry, looping until both lists end and no carry remains
LeetCode 13: Roman to Integer
·1 min
LeetCode
Easy
Hash-Table
Math
String
LeetCode 13 note: add each symbol’s value left to right, subtracting instead when it is smaller than the symbol to its right
LeetCode 9: Palindrome Number
·2 mins
LeetCode
Easy
Math
LeetCode 9 note: negatives are never palindromes; without strings, reverse the second half of the digits and compare it with the first half