[leetcode] 8. String to Integer (atoi)
来源:互联网 发布:linux从入门到精通pdf 编辑:程序博客网 时间:2024/06/03 08:01
题目:
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.
题解:
字符串转为整数是很常用的一个函数,由于输入的是字符串,所以需要考虑的情况有很多种。
1. 若字符串开头是空格,则跳过所有空格,到第一个非空格字符,如果没有,则返回0.
2. 若第一个非空格字符是符号+/-,则标记sign的真假,这道题还有个局限性,那就是在c++里面,+-1和-+1都是认可的,都是-1,而在此题里,则会返回0.
3. 若下一个字符不是数字,则返回0. 完全不考虑小数点和自然数的情况,不过这样也好,起码省事了不少。
4. 如果下一个字符是数字,则转为整形存下来,若接下来再有非数字出现,则返回目前的结果。
5. 还需要考虑边界问题,如果超过了整形数的范围,则用边界值替代当前值。
public int myAtoi(String str){ if(str==null) return 0; str=str.trim(); if(str.length()==0) return 0; int mark=0; double result=0; for(int i=0;i<str.length();i++){ if(str.charAt(i)=='+'&&i==0) mark=1; else if(str.charAt(i)=='-'&&i==0) mark=-1; else if(str.charAt(i)>='0'&&str.charAt(i)<='9'){ result=result*10+(str.charAt(i)-'0'); }else break; } if(mark<0) result=-1*result; if(result>Integer.MAX_VALUE) return Integer.MAX_VALUE; if(result<Integer.MIN_VALUE) return Integer.MIN_VALUE; return (int)result; }
注意:如果result定义为long型,会越界
一、基本数据类型的特点,位数,最大值和最小值。
1、
基本类型:short 二进制位数:16
包装类:java.lang.Short
最小值:Short.MIN_VALUE=-32768 (-2的15此方)
最大值:Short.MAX_VALUE=32767 (2的15次方-1)
2、
基本类型:int 二进制位数:32
包装类:java.lang.Integer
最小值:Integer.MIN_VALUE= -2147483648 (-2的31次方)
最大值:Integer.MAX_VALUE= 2147483647 (2的31次方-1)
3、
基本类型:long 二进制位数:64
包装类:java.lang.Long
最小值:Long.MIN_VALUE=-9223372036854775808 (-2的63次方)
最大值:Long.MAX_VALUE=9223372036854775807 (2的63次方-1)
4、
基本类型:float 二进制位数:32
包装类:java.lang.Float
最小值:Float.MIN_VALUE=1.4E-45 (2的-149次方)
最大值:Float.MAX_VALUE=3.4028235E38 (2的128次方-1)
5、
基本类型:double 二进制位数:64
包装类:java.lang.Double
最小值:Double.MIN_VALUE=4.9E-324 (2的-1074次方)
最大值:Double.MAX_VALUE=1.7976931348623157E308 (2的1024次方-1)
基本类型字节数位数最大值最小值byte1byte8bit2^7 - 1-2^7short2byte16bit2^15 - 1-2^15int4byte32bit2^31 - 1-2^31long8byte64bit2^63 - 1-2^63float4byte32bit3.4028235E381.4E - 45double8byte64bit1.7976931348623157E3084.9E - 324char2byte16bit2^16 - 10
- [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)
- 《Qt 实战一二三》
- centos7常见问题总结(一)
- PHP从环境搭建到编写简单接口
- location.href 在JS中是 什么意思 怎么使用它 举例说明谢谢了,大神帮忙啊
- u-boot-2016.09 make编译过程分析(一)
- [leetcode] 8. String to Integer (atoi)
- qt-faststart设关键帧-last atom in file was not a moov atom
- LeetCode 26. Remove Duplicates from Sorted Array
- Android初级开发(四)——补充7、Gallery(画廊)的使用
- 求反射向量
- HDU 6092 Rikka with Subset
- Textbox禁止剪切,复制,粘贴和弹出右键菜单
- Servlet应用程序(3)----验证器
- webrtc中的音频设备 音频采集 AudioDeviceModule