Leetcode 8. String to Integer (atoi)
来源:互联网 发布:网络兼职 编辑:程序博客网 时间:2024/05/27 00:33
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.
Link:https://leetcode.com/problems/string-to-integer-atoi/
思路:
1 如果出现字符 -> 则忽略
2 如果溢出overflow,返回最大值、最小值
3 符号位symbol
public static int MyAtoi(string str) { if (str == null || str.Length == 0) return 0; bool isNeg = false; str = str.Trim(); char[] charArray = str.ToCharArray(); int curIndex = 0; //获取整数符号 if (charArray[0] == '+') { isNeg = false; curIndex++; } else if (charArray[0] == '-') { isNeg = true; curIndex++; } else isNeg = false; //把字符串转换为整数 Int64 num = 0, absnum = 0; char c; while (curIndex < str.Length) { c = charArray[curIndex]; if (!Char.IsDigit(c)) break; absnum = 10 * absnum + (c - '0');//c是字符串的asscii if (isNeg) num = -absnum; else num = absnum; //判断是否溢出 if (num > Int32.MaxValue) return Int32.MaxValue; if (num < Int32.MinValue) return Int32.MinValue; curIndex++; } return (int)num; }
0 0
- [LeetCode]8. String to Integer (atoi)
- LeetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) Leetcode Python
- LeetCode --- 8. String to Integer (atoi)
- LeetCode 8.String to Integer (atoi)
- [Leetcode] 8. String to Integer (atoi)
- [leetcode] 8.String to Integer (atoi)
- [LeetCode] 8.String to Integer (atoi)
- <LeetCode OJ> 8. String to Integer (atoi)
- leetCode 8. String to Integer (atoi)
- 8. String to Integer (atoi) LeetCode
- leetcode 8. String to Integer (atoi)
- leetcode 8. String to Integer (atoi)
- LeetCode OJ 8.String to Integer (atoi)
- Leetcode ☞ 8. String to Integer (atoi)
- 8. String to Integer (atoi) ---Leetcode
- leetcode 8. String to Integer (atoi)
- Leetcode: 8. String to Integer (atoi)(JAVA)
- 三、Systick定时器
- Java学习总结第四天Java运算符
- HIbernate二级缓存的缓存策略
- 第二章 记住内存和变量
- Naive Bayes 朴素贝叶斯(文本)分类器Java实现
- Leetcode 8. String to Integer (atoi)
- STM32时钟系统学习
- 使用纯CSS实现文字超过一定的长度后自动显示省略号
- 《Java相关文章索引(2)》
- Eclipse中引用依赖库library失败的原因
- nodejs中的fiber库介绍
- Oracle Study 之--Oracle 11g Grid control安装(Agent)
- 安卓monkey随机测试
- USVN1.0.7安装部署(Window)