[算法练习]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 ...
随机推荐
- Webpack学习错误解决笔记
错误1:在用npm install 安装模块时,时常会出现没有以下类似的错误 解决方法:右键点击node_modules文件夹,选取属性,将文件夹只读选项去除 错误2:在学习到清理/dist文件夹这块 ...
- Java多线程笔记[未更新完]
最近课上可摸鱼时间较多,因此并发开坑学习 本篇学习自Java多线程编程实战指南 目前进展:刚开坑,处于理解概念阶段 本篇学习自Java多线程编程实战指南 Q.进程和线程的区别 进程Process是程序 ...
- 在Azuer创建自己的Linux_VM
---恢复内容开始--- emm..就是想搭个自己的VPN去YouTube看看视屏找找资源什么的... (滑稽.jpg)然后发现似乎需要这个玩意儿 先去申请一个Azuer账户 然后根据要求一步步来就好 ...
- gerapy的初步使用(管理分布式爬虫)
一.简介与安装 Gerapy 是一款分布式爬虫管理框架,支持 Python 3,基于 Scrapy.Scrapyd.Scrapyd-Client.Scrapy-Redis.Scrapyd-API.Sc ...
- DGIS之遥感影像数据获取
1.概要 在GIS圈的同行或多或少接触过遥感,记得在大学老师就说过"数据是GIS的核心".本文介绍在国内下载遥感影像的方法. 地理空间数据云,这个是中科院计算机网络中心建设的一个免 ...
- springboot入门神器 -http://start.spring.io/(在线项目构建)
参考并直接引用:http://www.sousou.io/article/1506656459859 最近在学习spring boot,看的书是<JavaEE开发的颠覆者 Spring Boot ...
- C# 文件读写系列二
读取文件原则上非常简单,但它不是通过FileInfo和DirectoryInfo来完成的,关于FileInfo和DirectoryInfo请参考C# 文件操作系列一,在.Net Framework4. ...
- Win10 VS2015 静态编译Qt5.6.2源码
由于VS2015需要CRT等拓展组件,因此把内部编写的工具软件以静态发布,固需要编译Qt源码.Qt5.6.2版本,VS2015,Win10 1.安装python,perl,下载jom 2.改文件com ...
- maven的安装配置超详细教程【含nexus】
1 下载 下载地址:http://maven.apache.org/download.cgi 界面效果如下: 点击之后进入的apache 软件基金的发布目录,在这里你可以下载apache的所有项目. ...
- 【开源组件】FastDFS极速入门与安装
FastDFS是一个开源的轻量级的分布式文件系统,为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供 ...