[算法练习]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.

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.

程序代码:

#include <gtest/gtest.h>
using namespace std;

int myAtoi(string str)
{
    int nLength = str.length();
    if (nLength == 0)
    {
        return 0;
    }

    char* pszData = (char*)str.c_str();
    for (int i=0; i<nLength; ++i)
    {
        if (‘ ‘ != *pszData)
        {
            break;
        }

        ++pszData;
    }

    // Positive or negative
    int nFlag = 1;
    if (‘+‘ == *pszData)
    {
        ++pszData;
    }
    else if(‘-‘ == *pszData)
    {
        nFlag = -1;
        ++pszData;
    }

    // ignore base case.
    char cValue;
    long long nResult = 0;
    while( (cValue = *pszData) != ‘\0‘)
    {
        if ( (cValue >= ‘0‘) && (cValue <= ‘9‘))
        {
            nResult = nResult * 10 + cValue - ‘0‘;
        }
        else
        {
            break;
        }

        if (nResult > 0x80000000)
        {
            break;
        }

        ++pszData;
    }

    nResult *= nFlag;
    if (nResult > std::numeric_limits<int>::max())
    {
        nResult = std::numeric_limits<int>::max();
    }
    else if(nResult < std::numeric_limits<int>::min())
    {
        nResult = std::numeric_limits<int>::min();
    }

    return (long)nResult;
}

TEST(Pratices, tMyAtoi)
{
    // 123
    ASSERT_EQ(myAtoi("123"),123);
    ASSERT_EQ(myAtoi("-123123"),-123123);
    ASSERT_EQ(myAtoi("-123123abdf"),-123123);
    ASSERT_EQ(myAtoi("2147483648"),2147483647);
    ASSERT_EQ(myAtoi("2147483647"),2147483647);
    ASSERT_EQ(myAtoi("-2147483649"),-2147483648);
    ASSERT_EQ(myAtoi("9223372036854775809"),2147483647);

}
时间: 2024-10-23 09:30:38

[算法练习]String to Integer (atoi)的相关文章

[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

每日算法之八: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

Kotlin实现LeetCode算法题之String to Integer (atoi)

题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 1 class Solution { 2 fun myAtoi(str: String): Int { 3 val maxInt = "2147483647" 4 val maxIntS = "+2147483647" 5 val minIntS = "-21474836

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】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) --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 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