LeetCode【8】. String to Integer (atoi) --java实现

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 specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Update (2015-02-10):

The signature of the C++ function had been updated. If you still see your function signature accepts a const
char *
 argument, please click the reload button  to reset your code definition.

本文简单点说就是把字符串里的数字转成整形,但是题目有几点要求:

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 optional initial plus or minus sign followed by as many numerical
digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists
because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

翻译大概意思是:

1、先丢弃字符串前面的空白字符知道扫到第一个非空白字符;然后将一个合法的数值的字符串以及其前面的一个正负号一同装换为int;

2、该合法数值子字符串中或后,可能包含其他非数值的特殊字符,忽略这些子字符串;

3、若第一个非空子字符串非护法的整型数值,或根本就没有,甚至该字符串可能都是特殊字符或空字符,则不作转换0;

4、若没有有效地转换,则返回0,若有返回数值超出边界,则返回边界值;

一、思路:

本题虽然属于比较简单的级别,但是在没考虑好全部可能的情况下,总是会出现遗漏一些细节的结果,导致提交后总是有新的意外情况出现。如:“     +-15651”、“    *    6644”这些首次出现的字符串不能构成数字的都是属于不合法的情况,直接返回0。思路如下:先扫描到第一个非空字符,再考虑两种情况,第一种是第一个非空字符是‘+‘或‘-‘号,若是则继续判断接下来的字符是否为数字,不是则不合法;另外一种是第一个非空字符是数字,那可继续以扫描数字的方式进行转换。除了以上两种情况,其他都为不合法。

二、Java程序

public class Solution {
    public int myAtoi(String str) {
        int sl = str.length();
        int index=0;
        char tempc;            //获取字符
        boolean flag = false;  //负数标志
        int firstI=-1;
        long re=0;

        if(sl<=0)
            return 0;
        //1. 开始至第一个非空字符
        while(str.charAt(index)==' ')
        {
            index++;
            if(index>=sl) //超过字符串长度
                return 0;
         }

        //2. 扫到第一个非空字符,判断是否合法
        tempc = str.charAt(index);
        //2.1 若是+-或数值则进行,否则则返回0
        if(tempc=='-'||tempc=='+'||(tempc<='9'&&tempc>='0'))
        {
            //2.2 若是数值则跳出判断
            if((tempc<='9'&&tempc>='0'))
            {
            }else                        //2.3 若是+-号,则判断接下来的是否为数值
            {
              if(tempc=='-') flag = true;//2.3.1 若是-号,则设置标志位

              index++;                   //2.4 若是+-号,则判断下一位是不是数值,是则合法,不是则返回0
              tempc = str.charAt(index);
              if((tempc<='9'&&tempc>='0'))
              {}else
               {
                   return 0;
               }
            }

        }else    //2.1 若是+-或数值则进行,否则则跳出
        {
            return 0;
        }

        //3. 开始扫描有效数字
        while(index<sl)
         {
            tempc = str.charAt(index);
            if(tempc<='9'&&tempc>='0')
            {
             re = re*10 + (str.charAt(index)-'0');
             if(re>=Integer.MAX_VALUE||re<=Integer.MIN_VALUE)
                break;
            }else
            {
                break;
            }
            index++;
         }

        if(flag==true)
        { re=-re;
          re = re<Integer.MIN_VALUE?Integer.MIN_VALUE:re;
        }else
        {
          re = re>Integer.MAX_VALUE?Integer.MAX_VALUE:re;
        }
        return (int)re;
    }
}
时间: 2024-12-09 19:53:24

LeetCode【8】. String to Integer (atoi) --java实现的相关文章

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函数需要满足以下特征 忽略字符串第一个非空格字符前的所

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,

LeetCode 008 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 b

leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

1.  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 inte

【leetcode】String to Integer (atoi)

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 f

[LeetCode][JavaScript]String to Integer (atoi)

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 f

leetCode 8. String to Integer (atoi) 字符串

8. 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 intende

[LeetCode][Algorithms]String to Integer (atoi)

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/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

【LeetCode】String to Integer (atoi) 解题报告 (Java)

这道题在LeetCode OJ上难道属于Easy,但是通过率却比较低,究其原因是需要考虑的情况比较低,很少有人一遍过吧. [题目] 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