[算法练习]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 ...
随机推荐
- 洛谷 P3757 [CQOI2017]老C的键盘
题面 luogu 题解 其实就是一颗二叉树 我们假设左儿子小于根,右儿子大于根 考虑树形\(dp\) \(f[u][i]\)表示以\(u\)为根的子树,\(u\)为第\(i\)小 那么考虑子树合并 其 ...
- Vue局部注册 或者全局注册 组件时,组件定义要用 分隔命名,用驼峰命名是不生效的
Vue.component('all-canuse',{ props:['message'], template:'<div>{{message}}</div>' }) 像这样
- jedis 连接redis
一, 单机版连接 @Test public void testJedis() { //1. 创建jedis 对象 Jedis jedis = new Jedis("192.168.88.1 ...
- excel 正则表达式用法
Private Sub RegEx_Replace() Dim myRegExp As Object Dim Myrange As Range, C As Range ...
- Visual Studio 跨平台開發實戰(3) - Xamarin iOS 多頁面應用程式開發 (转帖)
前言 在前一篇教學中, 我們學會如何使用Visual Studio 搭配Xcode 進行iOS基本控制項的操作. 但都是屬於單一畫面的應用程式. 這次我們要來練習如何透過Navigation Cont ...
- Linux系统编程:进程控制
一.进程相关操作与编程对应函数 1.进程创建:两种方式来实现. ①fork:创建一个子进程,父子进程共享一份代码程序,但是各有一份独立的数据,为了效率和保持数据的独立采用写时复制技术(COW).运行无 ...
- feignClient中修改ribbon的配置
1.使用@FeignClient注解发现服务 服务提供者的controller: @RestController public class StudentController { @Autowired ...
- 【数组】Unique Paths
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- vue测试安装和配置
npm install --save-dev @vue/test-utils mocha mocha-webpack npm install --save-dev jsdom jsdom-global ...
- Difference between $.ajax() and $.get() and $.load()
转自:Difference between $.ajax() and $.get() and $.load() $.ajax() is the most configurable one, where ...