字符串转整数
来源:互联网 发布:多组数据的显著性差异 编辑:程序博客网 时间:2024/06/16 20:02
字符串转整数,也是一个经典的问题了。字符串转整数本身的逻辑很简单,难就难在各种特殊输入与特殊情况。所以,这不是一道考察算法的题,而是一道考察细节与编码习惯的题。
我们考虑如下特殊情况:
1.输入为null或者为空字符串。
2.输入开头带空格的字符串。
3.输入带有正负号的数字。
4.数值范围超出最大正整数或者最小负整数。
package edu.bit.algorithm;public class IntergerToString { public static void main(String[] args) { String rawstr = null; int result = atoi(rawstr); System.out.println("The result is: " + result); rawstr = "+123"; result = atoi(rawstr); System.out.println("The result is: " + result); rawstr = " -123"; result = atoi(rawstr); System.out.println("The result is: " + result); rawstr = " +12ab3"; result = atoi(rawstr); System.out.println("The result is: " + result); rawstr = "34567123456"; result = atoi(rawstr); System.out.println("The result is: " + result); } public static int atoi(String rawstr) { //字符串为空 if (rawstr == null || rawstr.length() == 0) { System.out.println("The rawstr is null!"); return 0; } int start = 0; //找不是空格的位置 boolean positive = true; //默认为正数 //找第一个不是空格的位置 if (rawstr.charAt(start) == ' ') { while(rawstr.charAt(start) == ' ') { start++; if(start == rawstr.length()) { System.out.println("The rawstr is all of space!"); return 0; } } } //判断是否带符号 if(rawstr.charAt(start) == '-') { positive = false; start++; } else if(rawstr.charAt(start) == '+') { start++; } else if(rawstr.charAt(start) >= '0' && rawstr.charAt(start) <= '9') { return transfer_num(rawstr,start,true); } else { System.out.println("Invalid string to Int: " + rawstr); return 0; } //此时只有'+'或者'-' if(start == rawstr.length()) { return 0; } //此时为" -abc这种形式的字符串" if(rawstr.charAt(start) <= '0' && rawstr.charAt(start) >= '9') { System.out.println("Invalid string to Int: " + rawstr); return 0; } else { //为" +123"这种形式的字符串 return transfer_num(rawstr,start,positive); } } public static int transfer_num(String rawstr, int start, boolean positive) { long result = 0; for(int i= start; i<rawstr.length(); i++) { if(rawstr.charAt(i) >= '0' && rawstr.charAt(i) <= '9') { result = result * 10 + (rawstr.charAt(i) - '0'); } else { System.out.println("Invalid string to Int: " + rawstr); return 0; } } if(positive) { if(result > Integer.MAX_VALUE) { System.out.println("The result bigger than MAX_VALUE!"); return Integer.MAX_VALUE; } else { return (int) result; } } else { if(result < Integer.MIN_VALUE) { System.out.println("The result less than MIN_VALUE!"); return Integer.MIN_VALUE; } else { return (int) -result; } } }}
将代码run起来
The rawstr is null!The result is: 0The result is: 123The result is: -123Invalid string to Int: +12ab3The result is: 0The result bigger than MAX_VALUE!The result is: 2147483647
再来一个版本
/** * 1.null or empty string * 2.white spaces * 3.+/- sign * 4.calculate real value * 5.handle min and max */public class StringToInt { public static int atoi(String str) { if(str == null || str.length() < 1) { return 0; } str = str.trim(); //去除空白字符 char flag = '+'; int i = 0; if (str.charAt(0) == '-') { flag = '-'; i++; } else if(str.charAt(0) == '+') { i++; } double result = 0; for(int j=i; j<str.length(); j++) { if(str.charAt(j) >= '0' && str.charAt(j) <= '9') { result = result * 10 + (str.charAt(j) - '0'); } else { return 0; } } if(flag == '-') { result = -result; } if(result > Integer.MAX_VALUE) { return Integer.MAX_VALUE; } if(result < Integer.MIN_VALUE) { return Integer.MIN_VALUE; } return (int) result; } public static void main(String[] args) { String str = " 123"; System.out.println(atoi(str)); str = " -1134"; System.out.println(atoi(str)); str = " 123abc6"; System.out.println(atoi(str)); }}
0 0
- 字符串处理------字符串转整数
- 数字字符串转整数
- 整数转字符串
- 字符串转整数
- 字符串转整数
- 字符串转整数
- 字符串转整数
- 字符串转整数
- 20. 字符串转整数
- 字符串转整数
- 字符串转整数
- CSDN-字符串转整数
- 整数转字符串
- c字符串转整数
- 字符串转整数问题
- 字符串转整数(C#)
- c 整数转字符串
- 字符串 转整数
- .Net框架搭建之2、SQL Server MEF依赖注入 MVC Repository框架
- 如何使用fiddler2拦截移动端app的数组请求
- 适配——Drawable适配,占用内存测试
- Unity ScreenSpaceShadowMask Blur
- 第九章上传下载及其他
- 字符串转整数
- Android学习分享-常见控件02-Button详解
- MongoDB和MySQL性能测试及其结果分析
- 极客班STL第二周学习笔记
- Lua的值与类型
- Android动画使用(二)
- (C#)最大公共子串
- iOS开源库Carthage提交自己的开源库
- oc字符串的处理,字符串的截取,匹配字符串,分隔字符串,字符串的大小写转换,转定义字符串