【LeetCode】String to Integer (atoi)(java)
来源:互联网 发布:剪切视频在线软件 编辑:程序博客网 时间:2024/05/29 08:02
Question:
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.
Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
spoilers alert… click to show requirements for atoi.
题目是为了解决字符串转化为整数的问题。
解题思路:
题目本身不难,但是考虑总是会欠缺。
23333,我也是看了别人的理解,再加上自己debug的错误,才最终完成的。概括一下我所理解的判断方面。
1、丢弃前面的空格字符,直到找到第一个字符
2、选择一个可选的正负号(若出现“+-”则返回0)
3、遇到非数字字符,取之前的数字
4、超过整型范围,返回边界值
5、若不执行转换,则返回0
6、若最终无数字,返回0,若只能返回一个正负号,返回0
Java源代码:
public class Solution { public int myAtoi(String str) { String str2 = null; for(int i=0;i<str.length();i++){ if(str.charAt(i)==' '&&str2==null){ continue; } else if((str.charAt(i)=='+'||str.charAt(i)=='-')&&str2==null) str2=String.valueOf(str.charAt(i)); else if(48<=str.charAt(i)&&str.charAt(i)<=57){ if(str2==null) str2=String.valueOf(str.charAt(i)); else str2+=String.valueOf(str.charAt(i)); if(Long.valueOf(str2)>Integer.MAX_VALUE) return Integer.MAX_VALUE; if(Long.valueOf(str2)<Integer.MIN_VALUE) return Integer.MIN_VALUE; } else if((str.charAt(i)<48||str.charAt(i)>57)&&str2!=null){ if(str2.length()==1&&str2.charAt(0)!='+' &&str2.charAt(0)!='-') return Integer.valueOf(str2); else if(str2.length()==1&&(str2.charAt(0)=='+' ||str2.charAt(0)=='-')) return 0; else return Integer.valueOf(str2); } else return 0; } if(str2==null) return 0; else if(str2.length()==1&&(str2.charAt(0)=='+'||str2.charAt(0)=='-')) return 0; else return Integer.valueOf(str2); }
- [Leetcode] String to Integer (atoi) (Java)
- LeetCode – String to Integer (atoi) (Java)
- 【Leetcode】String to Integer(atoi) in JAVA
- [LeetCode Java] 8 String to Integer (atoi)
- [LeetCode][Java] String to Integer (atoi)
- [leetcode-8]String to Integer (atoi)(java)
- LeetCode : String to Integer (atoi) [java]
- Leetcode: 8. String to Integer (atoi)(JAVA)
- LeetCode(java)8. String to Integer (atoi)
- [LeetCode-Java]8. String to Integer (atoi)
- [LeetCode] 8. String to Integer (atoi) java
- leetcode String to Integer (atoi)(Java)
- String to Integer (atoi) leetcode java
- [leetcode]8. String to Integer (atoi)(Java)
- 【LeetCode】String to Integer (atoi)(java)
- LeetCode:String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- LeetCode String to Integer(atoi)
- 大型企业所需掌握技术
- 支持单选,多选,还可以限制选择的数量的android流式布局
- 水晶灯火灵
- com.alibaba.fastjson.JSONException: syntax error, expect {, actual int, pos 0
- 车厢重组( carry. cpp)
- 【LeetCode】String to Integer (atoi)(java)
- HTTP和servlet初学
- Android 动画简述
- 大数据试验——配置环境1
- BZOJ 1568 [JSOI2008]Blue Mary开公司
- 最长递增子序列
- 长短信发送
- 二级列表SimpleExpandableListAdapter ,ExpandableListAdapter
- go语言开发必备