【数据结构和算法】把字符串转换成整数,分3步计算
将字符串转化为整数
http://www.nowcoder.com/questionTerminal/44d8c152c38f43a1b10e168018dcc13f
按照题中的要求做就行了,总共分为3步
- 先去掉字符串两边的空格
- 然后判断符号
- 最后读取数字
public int atoi (String str) { str = str.trim();//去掉前后的空格 //如果为空,直接返回0 if (str.length() == 0) return 0; int index = 0;//遍历字符串中字符的位置 int res = 0;//最终结果 int sign = 1;//符号,1是正数,-1是负数,默认为正数 int length = str.length(); //判断符号 if (str.charAt(index) == '-' || str.charAt(index) == '+') sign = str.charAt(index++) == '+' ? 1 : -1; for (; index < length; ++index) { //取出字符串中字符,然后转化为数字 int digit = str.charAt(index) - '0'; //按照题中的要求,读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。 //字符串的其余部分将被忽略。如果读取了非数字,后面的都要忽略 if (digit < 0 || digit > 9) break; //越界处理 if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && digit > Integer.MAX_VALUE % 10)) return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; else res = res * 10 + digit; } return sign * res; }
我把部分算法题整理成了PDF文档,截止目前总共有900多页,大家可以下载阅读
链接:https://pan.baidu.com/s/1hjwK0ZeRxYGB8lIkbKuQgQ
提取码:6666
如果觉得有用就给个赞吧,还可以关注我的《牛客博客》查看更多的详细题解
数据结构和算法 文章被收录于专栏
专注于算法题的讲解,包含常见数据结构,排序,查找,动态规划,回溯算法,贪心算法,双指针,BFS和DFS等等