Sunday, October 12, 2014

String to Integer (atoi)

Problem

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Idea

  • Return 0 when input is not valid (Empty or wrong format)
  • Return LONG_MAX when input string overflows
  • Return LONG_MIN when input string underflows
  • Input String is allowed to have additional character after a valid substring that can form a long number. (e.g. +123+)
  • Input can have many whitespaces before the first non-whitespace character. (e.g. " 123")

  • Solution


    No comments:

    Post a Comment