Math

Basic

while (x) {
    int temp = ans * 10 + x % 10;
    if (temp / 10 != ans) return 0;
    ans = temp;
    x /= 10;
}

Plus One

  • Input may contain leading zeros
  • Need to save carry
  • Check for carry after loop

Reverse Integer

  • Watch out for overflow
  • Can divide by 10 to check for overflow

Write your own atoi

  • Carefully consider all possible input cases.
  • Leading zeros
  • Contain optional sing (plus or minus)
  • Convert digit char to integer
  • Check for overflow

Excel Sheet Column Number

  • 26 base
  • Need to check overflow

Excel Sheet Column Title

  • There's no representation of 0
  • Tricky part is how to handle 27
  • We can decrement n by one before any calculation
  • so 27 will be (27-1) % 26 = 0, (27-1)/26 = 1

Roman to Integer

  • Take symbol one by one from starting str.size()-2
  • If current value of symbol is greater than or equal to the value of next symbo, then add this value to the sum
  • Else subtract this value from the sum

Happy Number

  • Similar to check cycle in linked list
  • Use fast pointer and slow pointer

results matching ""

    No results matching ""