[算法练习]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); }
[算法练习]String to Integer (atoi)的更多相关文章
- [leetcode]经典算法题- String to Integer (atoi)
题目描述: 把字符串转化为整数值 原文描述: Implement atoi to convert a string to an integer. Hint: Carefully consider al ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode day6 -- String to Integer (atoi) && 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 ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
随机推荐
- 豆瓣电影信息爬取(json)
豆瓣电影信息爬取(json) # a = "hello world" # 字符串数据类型# b = {"name":"python"} # ...
- poj3207 Ikki's Story IV - Panda's Trick 2-SAT
题目传送门 题意:在一个圆上顺时针安放着n个点,给出m条线段连接端点,要求线段不相交,线段可以在圆内也可以在圆外,问是否可以. 思路:假设一条线段,放在圆外是A,放在园内是A',那么两条线段如果必须一 ...
- 【Maven学习】远程仓库的配置
很多情况下,默认的中央仓库无法满足项目的需求,我们可能需要配置新的远程仓库,此时我们可以这样配置: <repository> <id>java-net</id> & ...
- idea中web.xml报错 Servlet should have a mapping
配置springmvc时,报错,实际mapping已经写了,错误截图如下: 搜索无果,后来发现是工程的web.xml位置配置错误,因为我之前换过根目录位置. 修改方法: 打开Project Struc ...
- 【LDAP】LDAP常用命令解析
ldapadd -x 进行简单认证-D 用来绑定服务器的DN-h 目录服务的地址-w 绑定DN的密码-f 使用ldif文件进行条目添加的文件例子 ldapadd -x -D &qu ...
- Jquery 在多个相同标签click的问题
最近在做文章的删除动作,用Jquery来执行操作.但是实现时一开始总是只能对第一个起作用,其他的点击删除后没反应. 一开始的jquery代码是这样的, $('#articledelete').on(' ...
- 深度学习(十) GoogleNet
GoogLeNet Incepetion V1 这是GoogLeNet的最早版本,出现在2014年的<Going deeper with convolutions>.之所以名为“GoogL ...
- 如果天空不死博客java阅读列表整理
如果天空不死的主页https://home.cnblogs.com/u/skywang12345 下面是最近总结的Java集合(JDK1.6.0_45)相关文章的目录. 01. Java 集合系列01 ...
- 什么是SharePoint?
在聊SharePoint开发之前,有必要说下什么是SharePoint. 在我工作的过程中,经常遇到客户对SharePoint不太了解的情况.有客户说,SharePoint太烂了,DropBox能做到 ...
- python爬虫实战(七)--------伯乐在线文章(模版)
相关代码已经修改调试成功----2017-4-21 一.说明 1.目标网址:伯乐在线 2.实现:如图字段的爬取 3.数据:存放在百度网盘,有需要的可以拿取 链接:http://pan.baidu.co ...