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

这一题打该意思是将一个字符串转换成一个整型数,大概就是类似于atoi()这个函数的功能,要考虑很多的特例而已。

函数名: atoi

功 能: 将字符串转换成整型数;atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串时(’\0’)才结束转化,并将结果返回(返回转换后的整型数)。

这是atoi函数的功能,照着这个功能实现既可。但要考虑溢出啊,空白字符串啊这一大片的问题。

在这里,我处理溢出没有使用常规的方法,而是把res定义为long,然后返回时强转成int了,不知道算不算作弊啊。

public class Solution {

    private static final int INT_MAX = 2147483647;
    private static final int INT_MIN = -2147483648;
    public static int myAtoi(String str) {
        int i = 0;
        int flag = 0;
        long res = 0;
        if (str.equals("")) {
            return 0;
        }
//      跳过字符串前的空白字符
        while(str.charAt(i)==‘ ‘){
            i++;
            if (i>str.length()-1) {
                return 0;
            }
        }
//      遇到+ - 符合开始的数字
        if (str.charAt(i)==‘+‘ || str.charAt(i)==‘-‘) {
//          如果是-,则标记为1
            if (str.charAt(i)==‘-‘) {
                flag = 1;
            }
            i++;
        }
//      将字符转换为数字
        while (str.charAt(i)>=‘0‘&&str.charAt(i)<=‘9‘) {
//          处理溢出的情况
            if (res*10 + str.charAt(i) - ‘0‘>INT_MAX && flag == 0) {
                return INT_MAX;
            }
            if (res*10 + str.charAt(i) - ‘0‘-1>INT_MAX && flag == 1) {
                return INT_MIN;
            }
            res = res*10 + str.charAt(i) - ‘0‘;
            i++;
            if (i == str.length()) {
                break;
            }
        }
        if (flag==1) {
            res = res*-1;
        }
        return (int)res;
    }
}
时间: 2024-10-18 04:32:15

(atoi)Leetcode第八题_String to Integer (atoi)的相关文章

leetcode第八题--String to Integer (atoi)

Problem: 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

[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

[leetcode]_String to Integer (atoi)

非常考虑思维全面性的一道题,考验是否能够考虑本问题的方方面面. 题目:将一个string转换为int.实现函数atoi()的功能. 先应该明确atoi()有哪些特殊功能:(正常的正负数情况我就不列了) input output "+1" 1 "   +   1"  0(error了) "       1" 1(前头只有空格是合法的) "12b45" 12(取前面的数字) 溢出 : "2147483648"(

乘风破浪:LeetCode真题_008_String to Integer (atoi)

乘风破浪:LeetCode真题_008_String to Integer (atoi) 一.前言 将整型转换成字符串,或者将字符串转换成整型,是经常出现的,也是必要的,因此我们需要熟练的掌握,当然也有很多工具来实现了,但是在这个基础上加入一些其他的因素就是考点的所在了. 二.String to Integer (atoi) 2.1 问题理解 2.2 问题分析和解决     看到这个问题,我们就需要遍历字符串,然后判断开始的时候是不是空格,+,-,或者数字,如果不是的话就是不合理的,如果是,则继

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 f

LeetCode【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 intended for this problem to be spe

LeetCode(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 intended for this problem to be

leetcode第13题--Roman to Integer

Problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 遍历一次输入的字符串,如果不满足类似4或者9的就直接加相应的数,否则减去相应的数.其中对应如下”IVXLCDM“对应{1,5,10,50,100,500,1000} class Solution { public: int romanToInt(string s)

【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 this problem to be spe