[leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字
遵循几个规则:
1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符。
2. 此时取初始加号或减号。
3. 后面跟着尽可能多的数字,并将它们解释为一个数值。
4. 字符串可以在组成整数的字符之后包含其他字符,这些字符将被忽略,并且对该函数的行为没有影响。
5. 如果str中的第一个非空格字符序列不是有效的整数,则为0。
Runtime: 16 ms, faster than 62.80% of C++ online submissions for String to Integer (atoi).
class Solution
{
public:
int myAtoi(string str)
{
long res = ;
int sign = ;
int i = ;
while (str[i] == ' ')
++i; if (str[i] == '+' || str[i] == '-')
{
sign = str[i] == '+' ? : -;
++i;
} while (str[i] >= '' && str[i] <= '')
{
res = res * + str[i] - '';
if (res * sign >= INT_MAX)
return INT_MAX;
if (res * sign <= INT_MIN)
return INT_MIN;
++i;
} return res * sign;
}
};
[leetcode] 8. String to Integer (atoi) (Medium)的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- 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 ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- 蜗牛慢慢爬 LeetCode 8. String to Integer (atoi) [Difficulty: Medium]
题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cas ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- LeetCode 8. String to Integer (atoi) (字符串到整数)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
随机推荐
- How To Compile Qt with Visual Studio
How To Compile Qt with Visual Studio FEBRUARY 1, 2011 This post is a step-by-step guide on how to co ...
- Debug监视器(监视运行期程序通过API函数OutputDebugString输出的字符串)
http://download.csdn.net/detail/zswang/207199
- qt---cdb(Microsoft Console Debugger)调试
支持的调试器 windows系统下主要的调试器: CDB ,只能调试用户程序,只有控制台界面,以命令行形式工作 NTSD, 只能调试用户程序,只有控制台界面,以命令行形式工作 KD,主要用于内核调试, ...
- 这里有123个黑客必备的Python工具!
123个Python渗透测试工具,当然不仅于渗透~ 如果你想参与漏洞研究.逆向工程和渗透,我建议你时候用Python语言.Python已经有很多完善可用的库,我将在这里把他们列出来. 这个清单里的工具 ...
- SYN1610型B码时统设备
SYN1610型B码时统设备 产品概述 SYN1610型B码时统设备是由西安同步电子科技有限公司精心设计.自行研发生产的一款模块化配置的通用性时统终端,可接收北斗/ GPS信号/ IRIG-B码 ...
- Kong:Nginx支持的API Gateway管理解决方案
Kong的主要功能 Kong可灵活扩展:只要增添更多的服务器实例,它就能横向扩展,毫无问题,那样你可以支持更多流量,同时确保网络延迟很短. Kong可在任何地方运行:它可以部署在单个或多个数据中心环境 ...
- Programming In Lua 第四章
1, 2, 3, 4, 5, 6, 7,
- 深度残差网络(ResNet)
引言 对于传统的深度学习网络应用来说,网络越深,所能学到的东西越多.当然收敛速度也就越慢,训练时间越长,然而深度到了一定程度之后就会发现越往深学习率越低的情况,甚至在一些场景下,网络层数越深反而降低了 ...
- 【HDU - 1010】Tempter of the Bone(dfs+剪枝)
Tempter of the Bone 直接上中文了 Descriptions: 暑假的时候,小明和朋友去迷宫中寻宝.然而,当他拿到宝贝时,迷宫开始剧烈震动,他感到地面正在下沉,他们意识到这是一个陷阱 ...
- black box黑盒测试
软件规格说明书 等价类划分,完备性,无冗余性(不能有交集). 健壮等价类:无效等价类 边界值分析,对于一个含有n个变量的程序,采用边界值分析法测试程序会产生4n+1个测试用例 ...