LeetCode题解:String to Integer (atoi)
来源:互联网 发布:怎么找m2c 厂商 知乎 编辑:程序博客网 时间:2024/05/16 09:16
Implement atoi to convert a string to an integer.
题意:把字符串转换为整数
解决思路:按照整数的限制一个个转换字符串的每一个字符
代码:
public class Solution { public int myAtoi(String str) { int sign = 1; int total = 0; int index = 0; if(str.length() == 0){ return 0; } while(str.charAt(index) == ' ' && index < str.length()){ index++; } if(str.charAt(index) == '+' || str.charAt(index) == '-'){ sign = str.charAt(index) == '+'? 1 : -1; index++; } while(index < str.length()){ int digit = str.charAt(index) - '0'; if(digit < 0 || digit > 9){ break; } if(Integer.MAX_VALUE / 10 < total || Integer.MAX_VALUE / 10 == total && Integer.MAX_VALUE % 10 < digit){ return sign == 1? Integer.MAX_VALUE : Integer.MIN_VALUE; } total = total * 10 + digit; ++index; } return total * sign; }}
0 0
- LeetCode题解:String to Integer (atoi)
- LeetCode - String to Integer (atoi) 题解
- LeetCode题解:String to Integer (atoi)
- LeetCode题解--8. String to Integer (atoi)
- Leetcode题解 8. String to Integer (atoi)
- LeetCode题解-8-String to Integer (atoi)
- 8. String to Integer (atoi) LeetCode题解
- leetcode题解-8. String to Integer (atoi)
- 题解:String to Integer (atoi)
- String to Integer (atoi)---题解
- LeetCode 题解(18): String to Integer (atoi)
- LeetCode题解——8 String to Integer (atoi)
- LEETCODE 8 String to Integer (atoi) (JAVA题解)
- Leetcode题解(Python): 8. String to Integer (atoi)
- LeetCode Algorithms 8. String to Integer (atoi) 题解
- LeetCode:String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- LeetCode String to Integer(atoi)
- LeetCode题解:Palindrome Number
- empty isset is_null
- asp.net图片分布式存储
- UVA 156 Ananagrams
- 黑马程序员--抽象类与接口
- LeetCode题解:String to Integer (atoi)
- CDMA系统的远近效应问题
- MFC中CSplitter的应用以及在View中添加按钮
- 46家公司程序题答案,均通过上机验证
- POJ 题目1105 S-Trees(二叉树模拟)
- LeetCode题解:Ugly Number
- Spring注解@Component、@Repository、@Service、@Controller区别
- hdu 2096 小明A+B 刷题
- Windows下配置的环境变量未生效