题目说明:

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); }

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

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

    题目描述: 把字符串转化为整数值 原文描述: Implement atoi to convert a string to an integer. Hint: Carefully consider al ...

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

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  3. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  4. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  5. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  6. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  7. 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 con ...

  8. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  9. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

随机推荐

  1. 了解到的Web攻击技术

    (1)XSS(Cross-Site Scripting,跨站脚本攻击): 指通过存在安全漏洞的Web网站注册用户的浏览器内运行非法的HTML标签或者JavaScript进行的一种攻击. (2)SQL注 ...

  2. nRF51822EK_PRO

    ARMCC5LIB = C:\Keil\ARM\ARMCC\lib dd if=/dev/zero of=tmp.500M bs=500M count=1 Developer Home

  3. python 进程和线程(代码知识部分)

    二.代码知识部分 一 multiprocessing模块介绍: python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情 ...

  4. Ribbon是什么?

    学而时习之,不亦说乎!                              --<论语> Ribbon使用版本2.2.2 Ribbon是什么? 开始接触Ribbon的时候,网上以及很 ...

  5. javascript中操作元素属性

    1. setAttribute():设置属性的值: getAttribute():得到属性的值: removeAttribute():移除属性: 2.offsetWidth:offsetWidth = ...

  6. JVM-垃圾收集过程的内存管理

    JDK1.7 JVM的垃圾收集算法有 1. 标记-清除算法: 2. 复制算法:在商业虚拟机都是使用这种算法来回收新生代的 3. 标记-整理算法: 4.分代收集算法: JDK1.7 JVM的垃圾收集器有 ...

  7. ubuntu apache2 .htaccess 下配置 反向代理

    安装完apache2后, a2enmod rewrite //启用.htaccess规则 a2enmod proxy a2enmod proxy_http //启用反向代理支持 [P] 配置OK,就可 ...

  8. Golang真言

    Don't communicate by sharing memory, share memory by communicating. Concurrency is not parallelism. ...

  9. Spring.Net---2、IoC/DI基本概念

    ---------------------------------------------------------------------------------- (1)IoC/DI的概念 IoC ...

  10. C#RabbitMQ基础学习笔记

    RabbitMQ基础学习笔记(C#代码示例) 一.定义: MQ是MessageQueue,消息队列的简称(是流行的开源消息队列系统,利用erlang语言开发).MQ是一种应用程序对应用程序的通信方法. ...