[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 ...
随机推荐
- 腾讯移动Web整体解决方案Spirit
Spirit(勇气号),美国航天局NASA派往Mars(火星)的第一艘探测器.移动Web开发是一块新的领域,甚至有很多坑,这一点与人类从未踏上的Mars(火星)相似.为了避免开发者重复遇到相同的问题, ...
- CDC-更改数据捕获存储过程 (Transact-SQL)-学习
背景: 在SQLServer2008之前,对数据变更的捕获通常使用触发器.时间戳等低效高成本的功能来实现,所以很多系统都没有做数据变更或者仅仅对核心表做监控. 适用环境: 仅在SQLServer200 ...
- Model1简介
Model1模型出现前,整个Web应用的情况:几乎全部由JSP页面组成,JSP页面接收处理客户端请求,对请求处理后直接做出响应. 弊端:在界面层充斥着大量的业务逻辑的代码和数据访问层的代码,Web程序 ...
- layui打印表格自定义函数
函数如下 function print (tablelayid) { var v = document.createElement("div"); var f = ["& ...
- LINUX下QT FOR ARM开发环境搭建过程 (使用qt-x11-opensource-src-4.5.2.tar.gz进行编译)
在PC上,我们需要得到两个版本的Qt,分别是:Qt-4.5.2和QtEmbedded-4.5.2-arm.前者包括了Qt Designer等基本工具,用于在PC上对程序的开发调试,使我们能确保程序放到 ...
- 3021Java_数据类型
1.分类 Java数据类型 基本数据类型 数值型 整数类型 浮点类型 字符型 布尔型 引用数据类型 类 接口 数组 2.基本数据类型 2.1 综述 java的8种基本数据类型(简单数据类型) bool ...
- ORACLE(emp)表习题与答案
因为答案都是小编自己写的,解法可能有多种,如果您觉得我的解法有误,希望您有时间给我留言. 一.习题 (1) 查询20部门的所有员工信息. SELECT * FROM emp where deptno ...
- 从零开始的Wordpress个人博客搭建
0x00前言 在博客园写了有一年的博客了,也想换换新口味,wordpress的众多的主题和个性化设置非常符合我的喜好,所以捣鼓了一天也算是把它搭好了. 直接在服务器上搭建wordpress还需要配置m ...
- 原生js封装轮播图
个人实际开发中用到的效果问题总结出来便于自己以后开发查看调用,如果也适用其他人请随意拿走勿喷就行! 原生js对于思路要求比较高,在js代码我都写有备注,足够理解并使用,即使是小白或者刚入行的程序员也比 ...
- 宜信开源|手把手教你安装第一个LAIN应用
LAIN是宜信公司大数据创新中心开发的开源PaaS平台.在金融的场景下,LAIN 是为解放各个团队和业务线的生产力而设计的一个云平台.LAIN 为宜信大数据创新中心各个团队提供了统一的测试和生产环境, ...