题目说明:

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. Github如何在本地创建一个空的仓库

    1.在任意地方创建文件夹,并进入该文件夹: 2.通过git init命令把该文件夹变成Git可管理的仓库: 3.该文件夹里会多了个.git文件夹,它是Git用来跟踪和管理版本库的: 4.这时候手动把项 ...

  2. depmod -a

    分析可加载模块的依赖性,生成modules.dep文件和映射文件 intelligent bash: ' while true;do grep "reading error" ue ...

  3. JS框架设计之对象数组化一种子模块

    类数组对象是一个很好的存储结构,但是功能太弱了,为了享受纯数组的哪些便捷的方法,使用前可以做下转换,通常可以使用$.slice.call()方法做转换,但是旧版本的IE下的HTMLCollection ...

  4. LINUX 查找替换命令 总结

    find /var/ -name "*.php" > /home/tmp 在/var/目录下查找 所有以.php后缀结尾的文件  结果很多,就 > 输出结果到/home ...

  5. 时间格式为yyyymmdd的String类型的时间,计算时间间隔有错误

    时间格式类型为yyyymmdd,并且为String类型,计算时间间隔有误,一直搞不清楚是什么原因.网上百度了许多,时间格式基本都是yyyy-mm-dd这样的时间格式的,但是yyyymmdd这样的时间格 ...

  6. 安装mysql解压 版

    记录:win10重装系统后,注册mysql服务 其实在重装系统时如果不格式化mysql所在的盘,我们的mysql是不需要重装的 操作: 1.创建mysql服务:   开始-->运行-->c ...

  7. CC2530zigbee技术-简介协议栈

    前言 说实话,我喜欢自己的原创,虽然我写得可能简单了,但我觉得自己在写博客的路途上,一点一点地积累知识,我也借鉴别人的东西,特别是在写这篇文章时所使用的是markdownpad2写的,原来我根本就不知 ...

  8. 一个简单好用的强制删除软件geek

    给大家推荐geek软件工具,一个可以用来强制卸载那些常规手段无法卸载的软件,到官网(https://geekuninstaller.com/download)下载免费版,运行软件后,选择需要强制删除软 ...

  9. android studio的jni和so

    1. android studio自己添加代码生成so 代码地址:https://github.com/maogefff/Android-Test-Sample/tree/master/MyJni 参 ...

  10. 仿淘宝头像上传功能(三)——兼容 IE6 浏览器。

    前两篇目录: 仿淘宝头像上传功能(一)——前端篇. 仿淘宝头像上传功能(二)——程序篇. 仿淘宝头像上传功能(三)——兼容 IE6 浏览器 之前的这两篇虽然实现了功能,但不兼容低版本浏览器,而且有些浏 ...