题目:
mplement 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.先判断string是否指向空或者本身为空(str==null||str.length()==0);
2.去除空格str=str.trim;
3.检查正负号,同时设定i计数,下一步计算数值可以直接从i开始(我总是想砍掉,或者建立数组存储,不必要!)
4.最后,计数,因为只要一次就好,不必存储,所以直接遍历并记录即可(因为粗心没有将代码写在括号里!)
学到的知识点:
1.字符串的遍历,不必存为数组
char c=str.charAt(i);//把第i个数存成字符类型
int digit=c-'0';//把字符类型转化成整数类型
Accepted代码如下:
1 public class Solution { 2 public int myAtoi(String str) { 3 4 double res=0; 5 int i=0; 6 char f='0'; 7 //1.没有指向或者字符串为空 8 int length=str.length(); 9 if (length==0||str==null) { 10 return 0; 11 } 12 //2.去除两边的空格 13 str=str.trim(); 14 //3.检查正负号,并存到f中,同时用i记录访问位置 15 if (str.charAt(0)=='+') { 16 f='+'; 17 i++; 18 } 19 else if (str.charAt(0)=='-'){ 20 f='-'; 21 i++; 22 } 23 //4.遍历数。此时遇到不是数的,退出即可,同时每次要乘10,计算出位数 24 for (; i< str.length(); i++) { 25 char c=str.charAt(i);//把第i个数存成字符类型 26 int digit=c-'0';//把字符类型转化成整数类型 27 if (digit>9||digit<0) { 28 break; 29 } 30 res=res*10+digit;//此时已经得到数的绝对值 31 } 32 //5.最后判断是否越界,根据符号确定输出 33 if (f=='+'||f=='0') { 34 if (res>Integer.MAX_VALUE) {res=Integer.MAX_VALUE;} 35 } 36 else{ 37 res=-res; 38 System.out.print(res); 39 if (res<Integer.MIN_VALUE ) { 40 res=Integer.MIN_VALUE; 41 } 42 } 43 /** 44 if (f=='+'||f=='0') { 45 46 if (res > Integer.MAX_VALUE) return Integer.MAX_VALUE; 47 } else { 48 res=-res; 49 if (res < Integer.MIN_VALUE) return Integer.MIN_VALUE; 50 } 51 */ 52 53 return (int)res; 54 55 //以上对于符号和越界的同时判断,因为我直接在if和elseif中return,就可能造成逻辑不严谨,从而没有返回值 56 57 58 59 60 61 62 /** 63 int []m=new int[length]; 64 int []a=new int[length]; 65 char []n=new char[length]; 66 */ 67 //基本思路正确,即先判断正负号,再判断从第二位开始的,出现一个非数字就停止 68 //我建立了char数组,又建立了int数组,其实是不必要的。 69 // char c = str.charAt(i);表示把str的第i个char值赋值给c,类似于数组的挨个查找 70 //int digit=c-'0;即将该数位转化为10 71 //还有不必砍掉符号部分,只需要用i记住,从第二位开始遍历即可 72 //3.连续两个正负号就返回0.一个正负号就记录下来f 73 /**f='0'; 74 if (str.substring(0,1).equals("+")) { 75 if (str.substring(1,2).equals("+")||str.substring(1,2).equals("-")) { 76 return 0; 77 }//重复的正负号 78 else { 79 f='+'; 80 str=str.substring(1,str.length()); 81 n=str.toCharArray(); 82 //如果有合理的正负号,就剪掉 83 } 84 } 85 else if (str.substring(0,1).equals("-")) { 86 if (str.substring(1,2).equals("+")||str.substring(1,2).equals("-")) { 87 return 0; 88 }//重复的正负号 89 else { 90 f='-';//正负号单独提取出来,牢记数组n是一个字符数组 91 str=str.substring(1,str.length()); 92 n=str.toCharArray(); 93 94 } 95 } 96 else {//开始将字符串搬到字符数组里面,目前已经除掉了前后的空格,除掉了开始的正负号 97 n=str.toCharArray(); 98 for (int i = 0; i < n.length; i++) { 99 m[i]=n[i]-'0';//m中存的是转化成整数的字符串(含正负号) 100 } 101 } 102 //检查字符到整数的转换是否存好 103 for (int j = 0; j < m.length; j++) { 104 System.out.println(m[j]); 105 } 106 107 for (int j = 0; j < m.length; j++) { 108 if (m[j]>=0&&m[j]<=9) { 109 a[j]=m[j]; 110 z=j+1; 111 } 112 else { 113 z=j;//出现不是数组的字符,就不要了,标记位置并弹出循环 114 //当纯数字时出现错误是因为根本就没有进到这个else里面 115 break; 116 } 117 118 } 119 120 System.out.println(z); 121 System.out.println(f); 122 for (int i = z-1; i >=0; i--) { 123 res=res+Math.pow(10, (z-1-i))*a[i]; 124 //System.out.println(z-1-i); 125 System.out.print(a[i]); 126 } 127 128 if (f=='+'||f=='0') { 129 return (int)res; 130 } 131 else { 132 return -(int)res; 133 }*/ 134 135 136 } 137 }