[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 ...
随机推荐
- Linux下的wfopen(手工打造)
Of Linux on wfopen (open wide-character version of the file name and mode) to achieve Not directly a ...
- delphi如何输出当前堆栈
想实现,输出当前运行的堆栈,有会的吗?给点思路 方法很多,参考: https://bitbucket.org/shadow_cs/delphi-leakcheck/ 的 https://bitbuck ...
- 使用 acl 编写 UDP 网络程序(UDP 重传及可靠性机制)
在当今网络世界,虽然大部分网络应用都是基于 TCP 的,但有时 UDP 的网络通信也有用武之处.acl 的网络库中不仅提供了基于 TCP 的网络套接字流,同时也提供了 UDP 的网络库(目前 acl ...
- qt实现-给SQLITE添加自定义函数(对某个字段进行加密)
需要使用sqlite里的password对某个字段进行加密,由于使用的sqlite是由QT封装好的QSqlDatabase,没有发现加载扩展函数的方法,所以自己实现了一个. 在网上也没找到相应的参考, ...
- MFC 中 Tooltip 实现的几种方式
方法一:利用CWnd本身自身支持的tooptip来实现,这种方法适用给控件增加tooltip,非常方便和简单方法如下:1.在窗口中增加消息映射ON_NOTIFY_EX(TTN_NEEDTEXT, 0, ...
- SAP TABLECONTROL 自定义SEARCH HELP
项目上需要开发一个界面如下的应用程序.这是一个MB1A发料的辅助程序,限制住移动类型和在特定字段写入产品号. 这个应用程序的主要功能毫无疑问是通过BAPI实现的.但在TABLECONTROL中需要对填 ...
- 如何在 cmd 命令行中查看、修改、删除与添加环境变量
Windows 和 linux 区别 一.查看所有环境变量的名称和值:Linux下:exportWindows下:set二.根据名称查该环境变量的值:Linux下:echo $环境变量名比如:echo ...
- 03 我的第一个html页面
<!--定义文档的类型,一个html就是一个文档--> <!DOCTYPE html> <html lang="en"> <!--head ...
- 如何有效预防XSS?这几招管用!!!
原文链接 预防XSS,这几招管用 最近重温了一下「黑客帝国」系列电影,一攻一防实属精彩,生活中我们可能很少有机会触及那么深入的网络安全问题,但工作中请别忽略你身边的精彩 大家应该都听过 XSS (Cr ...
- 《Spring Cloud》学习(二) 负载均衡!
第二章 负载均衡 负载均衡是对系统的高可用.网络压力的缓解和处理能力扩容的重要手段之一.Spring Cloud Ribbon是一个基于 HTTP 和 TCP 的客户端负载均衡工具,它基于Netfli ...