标签:面试题 图片 int span 题目 pre style pac git
题目:
解答:
1 class Solution { 2 public: 3 int strToInt(string str) 4 { 5 if (str.size() == 0) 6 { 7 return 0; 8 } 9 10 int i = 0; 11 int minus = 1; 12 13 // skip spaces 14 while (str[i] == ‘ ‘) 15 { 16 i++; 17 } 18 19 // get sign 20 if (str[i] == ‘-‘) 21 { 22 minus = -1; 23 i++; 24 } 25 else if (str[i] == ‘+‘) 26 { 27 minus = 1; 28 i++; 29 } 30 31 // convert numbers 32 int num = 0; 33 while (isdigit(str[i])) 34 { 35 // 214748364 = INT_MAX / 10 36 if ((num == 214748364) && ((str[i] - ‘0‘) > 7) || (num > 214748364)) 37 { 38 return (minus > 0) ? INT_MAX : INT_MIN; 39 } 40 num = 10 * num + (str[i] - ‘0‘); 41 i++; 42 } 43 44 return (minus > 0) ? num : -num; 45 } 46 };
标签:面试题 图片 int span 题目 pre style pac git
原文地址:https://www.cnblogs.com/ocpc/p/12826503.html