LeetCode 8. String to Integer
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 8. String to Integer相关的知识,希望对你有一定的参考价值。
public int MyAtoi(string str) { if (string.IsNullOrEmpty(str)) { return 0; } int sign = 1; int i = 0; for (; i < str.Length; ++i) { if (str[i] != ‘ ‘) { break; } } if (str[i] == ‘+‘) { sign = 1; i++; } else if (str[i] == ‘-‘) { sign = -1; i++; } int digit = 0; for (; i < str.Length; ++i) { if (str[i] < ‘0‘ || str[i] > ‘9‘) { break; } // overflow if (digit * sign > Int32.MaxValue / 10 || (digit * sign == Int32.MaxValue / 10 && (str[i] - ‘0‘) > Int32.MaxValue % 10)) { return Int32.MaxValue; } if (digit * sign < Int32.MinValue / 10 || (digit * sign == Int32.MinValue / 10 && (str[i] - ‘0‘) * sign < Int32.MinValue % 10)) { return Int32.MinValue; } digit = digit * 10 + (str[i] - ‘0‘); } return digit * sign; }
以上是关于LeetCode 8. String to Integer的主要内容,如果未能解决你的问题,请参考以下文章
leetcode:String to Integer (atoi)
8. 字符串转整数(实现atoi函数) [leetcode 8: String to Integer (atoi)]
8. 字符串转整数(实现atoi函数) [leetcode 8: String to Integer (atoi)]
leetcode-8. String to Integer (atoi)