String to Integer (atoi)

矫情吗;* 2022-08-06 11:28 202阅读 0赞

String to Integer (atoi)

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.

spoilers alert… click to show requirements for atoi.

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

  1. class Solution {
  2. public:
  3. int atoi(const char *str) {
  4. int len = strlen(str);
  5. //cout<<len<<endl;
  6. int i=0;
  7. if(*str == '\0'){ // empty
  8. return 0;
  9. }
  10. while(i<len && str[i] == ' ') // !((str[i]=='-' && str[i+1]>='0' && str[i]<='9') || (str[i]>='0' && str[i]<='9'))
  11. {i++;}
  12. //cout<<"i: "<<i<<endl;
  13. if(i==len){ // all are blank
  14. return 0;
  15. }
  16. else{
  17. bool flag=true;
  18. if(str[i] == '-' && (str[i+1]>='0' && str[i+1]<='9')){ //negative
  19. flag=false;
  20. i++;
  21. }
  22. else if(str[i] == '+' && (str[i+1]>='0' && str[i+1]<='9'))
  23. {
  24. flag=true;
  25. i++;
  26. }
  27. else if ((str[i]>='0' && str[i]<='9'))
  28. {
  29. }
  30. else
  31. {
  32. return 0;
  33. }
  34. //positive
  35. string s="";
  36. while(i < len)
  37. {
  38. if(str[i]>='0' && str[i]<='9'){
  39. s += str[i]; //contecate
  40. }
  41. else
  42. {
  43. break;
  44. }
  45. i++;
  46. }
  47. //cout<<s<<endl;
  48. string max="2147483647";
  49. string min="2147483648";
  50. if(flag && (s.length() > 10 || (s.length() == 10 && s>max)))
  51. {
  52. return INT_MAX;
  53. }
  54. if(!flag && (s.length() > 10 || (s.length() == 10 && s>min))){
  55. return INT_MIN;
  56. }
  57. int result=0;
  58. for(i = 0; i < s.length(); i++){
  59. result = result*10 + s[i]-'0';
  60. }
  61. if(!flag)
  62. {
  63. result = -1*result;
  64. }
  65. return result;
  66. }
  67. }
  68. };

题目不难,但是测试案例特别多,情况复杂,我给出几个:

  1. char str[100] = " -11919730356x"; // "", " -0012a42"=-12, "+-2"=0, "+1"=1, " -11919730356x"

发表评论

表情:
评论列表 (有 0 条评论,202人围观)

还没有评论,来说两句吧...

相关阅读