problem
LeetCode 剑指 Offer 67. 把字符串转换成整数
写一个函数 StrToInt,实现把字符串转换成整数这个功能。不能使用 atoi 或者其他类似的库函数。
尝试一
思路过程
- 如果时-23+45,是输出-23呢?还是22呢?需要我计算吗?不需要
- 如果遇到负号,后面都没有数字,那不就记录错误了,例如-a32,测试返回零,说明符号后一定会跟着数字
- 如果要完成转换,开头一定是要正负号或是数字,是字母就不可以了
- 不用计算,直到一开始到最后截取到有效的数字就行
- 把字符串挨个遍历
- 没有数字,就是无效,返回零,用LinkendList来存储得到的数字
- 遇到不是数字就结束遍历,计算大小
- 用一个标志位来记录正负
- 忘记了判断字符是不是在数字范围的函数,只能手动判断了
代码
class Solution {
public int strToInt(String str) {
boolean positive=true;
char[] chars=str.toCharArray();
if(( chars[0]<'0' || chars[0]>'9') && chars[0]!='-'){
return 0;
}
List<Integer> collection=new LinkedList<>();
int maxValue=Integer.MAX_VALUE;
for(char c:chars){
// 在之前就要判断positive是不是false
if(c=='-'){
positive=false;
continue;
}
if(c<'0' || c>'9'){
break;
}
collection.add(c-'0');
}
long res=0;
for(int num:collection){
res=res*10+num;
if(res>maxValue){
if(positive){
return maxValue;
}
return Integer.MIN_VALUE;
}
}
if(positive){
return (int)res;
}
return -(int)res;
}
}
结果
没有考虑到空字符串,越界了
尝试二
代码
class Solution {
public int strToInt(String str) {
boolean positive=true;
str=str.trim();
char[] chars=str.toCharArray();
if(( chars[0]<'0' || chars[0]>'9') && chars[0]!='-'){
return 0;
}
List<Integer> collection=new LinkedList<>();
int maxValue=Integer.MAX_VALUE;
for(char c:chars){
if(c=='-'){
positive=false;
continue;
}
if(c<'0' || c>'9'){
break;
}
collection.add(c-'0');
}
long res=0;
for(int num:collection){
res=res*10+num;
if(res>maxValue){
if(positive){
return maxValue;
}
return Integer.MIN_VALUE;
}
}
if(positive){
return (int)res;
}
return -(int)res;
}
}
结果
失败,没考虑到加号,整数还需要用加号表示?原来如此,例如+32
尝试三
class Solution {
public int strToInt(String str) {
boolean positive=true;
if(str==null){
return 0;
}
str=str.trim();
char[] chars=str.toCharArray();
if(chars.length==0 || (( chars[0]<'0' || chars[0]>'9') &&(chars[0]!='-'&&chars[0]!='+'))){
return 0;
}
List<Integer> collection=new LinkedList<>();
int maxValue=Integer.MAX_VALUE;
for(char c:chars){
if(c=='-'){
positive=false;
continue;
}
if(c=='+'){
continue;
}
if(c<'0' || c>'9'){
break;
}
collection.add(c-'0');
}
long res=0;
for(int num:collection){
res=res*10+num;
if(res>maxValue){
if(positive){
return maxValue;
}
return Integer.MIN_VALUE;
}
}
if(positive){
return (int)res;
}
return -(int)res;
}
}
结果
还有这样的输入:,怎么解?记录第一个遇到数字的位置,然后后面进行判定
尝试四
class Solution {
public int strToInt(String str) {
boolean positive=true;
if(str==null){
return 0;
}
str=str.trim();
char[] chars=str.toCharArray();
if(chars.length==0){
return 0;
}
if(( chars[0]<'0' || chars[0]>'9') &&(chars[0]!='-'&&chars[0]!='+')){
return 0;
}
if(chars[0]=='-' || chars[0]=='+'){
if(chars.length<=1){
return 0;
}
if(chars[1]<'0'||chars[1]>'9'){
return 0;
}
}
List<Integer> collection=new LinkedList<>();
int maxValue=Integer.MAX_VALUE;
for(char c:chars){
if(c=='-'){
positive=false;
continue;
}
if(c=='+'){
continue;
}
if(c<'0' || c>'9'){
break;
}
collection.add(c-'0');
}
long res=0;
for(int num:collection){
res=res*10+num;
if(res>maxValue){
if(positive){
return maxValue;
}
return Integer.MIN_VALUE;
}
}
if(positive){
return (int)res;
}
return -(int)res;
}
}
结果
还是错了,不做了,看别人解答(╯▔皿▔)╯
尝试五
class Solution {
public int strToInt(String str) {
// 我去,自己想太多了
boolean positive=true;
if(str==null){
return 0;
}
str=str.trim();
char[] chars=str.toCharArray();
if(chars.length==0){
return 0;
}
if(( chars[0]<'0' || chars[0]>'9') &&(chars[0]!='-'&&chars[0]!='+')){
return 0;
}
if(chars[0]=='-'){
positive=false;
}
List<Integer> collection=new LinkedList<>();
int maxValue=Integer.MAX_VALUE;
for(char c:chars){
// 直接应该break掉
if(c<'0' || c>'9'){
break;
}
collection.add(c-'0');
}
long res=0;
for(int num:collection){
res=res*10+num;
if(res>maxValue){
if(positive){
return maxValue;
}
return Integer.MIN_VALUE;
}
}
if(positive){
return (int)res;
}
return -(int)res;
}
}
结果
判断过后没记得把第一个去掉去遍历
尝试六
class Solution {
public int strToInt(String str) {
boolean positive=true;
if(str==null){
return 0;
}
str=str.trim();
char[] chars=str.toCharArray();
if(chars.length==0){
return 0;
}
if(( chars[0]<'0' || chars[0]>'9') &&(chars[0]!='-'&&chars[0]!='+')){
return 0;
}
int i=0;
if(chars[0]=='-'){
positive=false;
i=1;
}
if(chars[0]=='+'){
i=1;
}
List<Integer> collection=new LinkedList<>();
int maxValue=Integer.MAX_VALUE;
for(;i<chars.length;++i){
// 直接应该break掉
if(chars[i]<'0' || chars[i]>'9'){
break;
}
collection.add(chars[i]-'0');
}
long res=0;
for(int num:collection){
res=res*10+num;
if(res>maxValue){
if(positive){
return maxValue;
}
return Integer.MIN_VALUE;
}
}
if(positive){
return (int)res;
}
return -(int)res;
}
}
结果
终于终于成功了~~~~/(ㄒoㄒ)/~~
总结
这自己调试总是得好多次才能成功,边界条件总是没有很全面的考虑到.但就算是让我再做一次,我也大概率不能一次成功.如果面试官给我出这个题目,简直凉凉.但我也觉得这不是在深入考算法,而是考验耐心和分析的方面了.只能说练习一个题目算一个,训练手感,增加刷题量.