My Blog List

[Leetcode Solution] Restore IP Addresses

Analysis

  • Because the length of input string is supposed to be short, a straightforward dfs could be used
  • Within the DFS, each time extract next sub string with length of 1 or 2 or 3. If the sub string is a valid number, then search next step.
  • A valid ip address number could be checked 
    • its integer number is no more then 255
    • If its length is bigger then 1, then the first character cannot be '0'

Note

  • str.substr(i,j) cannot be used as  a parameter in a function defined as void func(string& str)
  • The atoi function is used to c string rather then string. So it should be called like atoi(str.c_str())

Code


No comments:

Post a Comment

Enter you comment