atoi问题

atoi问题

leetcode

java

atoi

1.问题描述

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.

题目翻译:

实现一个atoi函数,完成从string到int的转换。要仔细的考虑所有可能输入的情况,才能完成这道题。

2.解题思路

这道题特殊的地方就是必须想清楚所有的可能情况,不然就通不过leetcode的用例测试,这是很麻烦的地方,现将需要注意的情况总结如下:

1.string最开始是空格即‘ ‘,要跳过所有的空格,但是空格可能不存在。

2.在空格之后是表示正负的符号:‘+‘/‘-‘,要正确的读取这些符号。

3.之后是数字,要正确的读取并计算出相应的数值,同时要注意数值的范围问题,因为可能存在数值的溢出,正数溢出输出INT_MAX,负数溢出输出INT_MIN。

4.全是非数字字符

5.数值之中出现非数值字符,只输出当前已经计算得到的数值

是要在程序中解决这5点,程序既能够通过leetcode的用例。

3.代码实现

  1. class Solution {


  2. public static int myAtoi(String str) { 

  3. int len=str.length(); 

  4. if(len<=0) 

  5. return 0; 

  6. char[] ch=str.toCharArray();//把str变成char数组,速度更快 

  7. int pos=0,res=0,sign=1,dit=0; 

  8. int tmp=0; 

  9. while(ch[pos]==‘ ‘&&pos<len) {//跳过所有的空格 

  10. pos++; 



  11. if(ch[pos]==‘+‘||ch[pos]==‘-‘) {//检测是正数还是负数,但是都按照正处理 

  12. sign=(ch[pos]==‘+‘?1:-1); 

  13. pos++; 



  14. for(;pos<len;) { 

  15. dit = ch[pos]-‘0‘; 

  16. if(dit<0||dit>9)//处理数值中间出现非数值,还有全部都不是数值情况 

  17. break; 

  18. //判断是否越界,当res>INT_MAX/10时那么下一步肯定越界, 

  19. //如果等于INT_MAX/10,那就需要比较最后一位,因为INT_MAX的最后一位是7, 

  20. //大于7就肯定会溢出。 

  21. if(Integer.MAX_VALUE/10 < res || Integer.MAX_VALUE/10 == res && Integer.MAX_VALUE %10 < dit) 

  22. return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE; 

  23. tmp = res*10+dit; 

  24. /* 

  25. if(tmp/10 != res) { 

  26. return tmp*sign>Integer.MAX_VALUE?Integer.MAX_VALUE:Integer.MIN_VALUE; 



  27. */ 

  28. res=tmp; 

  29. pos++; 



  30. return res*sign;//返回结算结果 


  31. }  




时间: 2024-08-18 00:14:01

atoi问题的相关文章

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

String to Integer (atoi) leetcode

题目的意思是要将一个字符串转换成数字 这个题目的重点是要处理    各种各样的输入情况 在题目下面有一大段英文: Requirements for atoi: The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an opt

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,

8. String to Integer (atoi)

https://leetcode.com/problems/string-to-integer-atoi/description/ 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 poss

【LeedCode】String to integer(atoi)

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 spe

【C语言】模拟实现atoi函数

atoi(表示 ascii to integer)是把字符串转换成整型数的一个函数. atoi()函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( )函数来检测),直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回.如果 nptr不能转换成 int 或者 nptr为空字符串,那么将返回0 我们在模拟实现atoi函数时,要注意以下几点: 1.字符串之前的空白问题 2.正负号 3.字符串为空时 4.

LeetCode8 String to Integer (atoi)

题意: Implement atoi to convert a string to an integer.  (Easy) 分析: 就是注意各种特殊情况,边界情况的判断,见代码注释. 1 class Solution { 2 public: 3 int myAtoi(string str) { 4 int start = 0; 5 long long result = 0; 6 int flag = 0; 7 //开头多余空格的处理 8 while (str[start] == ' ') { 9

[leetcode]经典算法题- String to Integer (atoi)

题目描述: 把字符串转化为整数值 原文描述: 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

c++ 实现atoi()函数

1. 问题描写叙述 实现c++函数库中atoi()函数,要考虑到各种特殊情况: 空字符串. +和-号. 字符串前中后n个空格. 溢出. 非数字字符. 2. 解决方式 转换过程并不复杂.复杂的是要考虑到众多特殊情况. int myAtoi(string str) { if(str.length() == 0) return 0; //空串 bool isNeg = false; long re = 0; int i=0,cnt; for(;i<str.length(); i++) if(str[i

String to Integer (atoi)

1. Question 将字符串转换为整数,考虑各种输入情况: 空格处理:开头空格省略 有效数字:从第一个非空格字符开始的是+.-或数字,直到下一个非数字字符结束. 加号处理:开头加号省略 空串处理 溢出处理 无效数字处理 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do no