zoukankan      html  css  js  c++  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.

    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) != '')
        {
            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);
        
    }
  • 相关阅读:
    C#注意事项
    组件化开发
    kettle导数删除并插入更新数据_20161130
    MySQL_各城市在线产品天订单数据20161130
    MySQL_杭州11月1-29号在线产品在线天数、销售天数_20161129
    kettle每天自动发送邮件总结_20161128
    MYSQL_与excel结合在excel中用&连接符快速创建表头_20161125
    MySQL_杭州拱墅区、西湖区近9-11月销售过的产品_20161125
    MySQL_产品昨日库存与历史入库历史出库成本_20161124
    MySQL11月16-11月21日活动赠送的优惠券使用率_20161124
  • 原文地址:https://www.cnblogs.com/Quincy/p/5293185.html
Copyright © 2011-2022 走看看