[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 trim and edit videos in Photos for OS X
Don't let the name fool you, Photos for OS X also stores all your videos. Whether you synced them fr ...
- C函数实现返回多个值的方法
C语言中,一个函数最多只能实现一个返回值. int func (int b) { int a=5; if (a>b) return a; else return b; return 0; } ...
- Ruby元编程:执行某个目录下的全部测试用例
目前手里有个测试项目各个feature的测试用例都放在对应的子目录下,虽然有自动化测试框架的帮助执行起来很方便,但是偶尔也有需要在本地执行某个feature的全部测试用例集合.因为本人对shell脚本 ...
- uni-app中Vuex的引用
//store 中 store.js import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vue ...
- 2013年最流行的php框架盘点
2013年最流行的php框架盘点 PHP框架的发展背景 毫无疑问,Web框架技术在近几年已经得到了突飞猛进的发展和普及,在过去几年里,框架技术的普遍经历了比较大的完善过程,很大一部分可以归因于Ruby ...
- vuejs 使用less
当所有东西都 准备好之后 : 第一步: 安装less依赖, npm install less less-loader --save 第二步: 修改webpack.config.js文件,配置loade ...
- netty服务端启动--ServerBootstrap源码解析
netty服务端启动--ServerBootstrap源码解析 前面的第一篇文章中,我以spark中的netty客户端的创建为切入点,分析了netty的客户端引导类Bootstrap的参数设置以及启动 ...
- C++虚函数与虚表
有一定面向对象知识的朋友对继承与多态一定很熟悉,C++想实现继承的话就要使用虚函数,那么什么是虚函数,其原理是什么,下面尽量给大家分析一下C++中其运行机制: 首先,基础,什么是虚函数,什么是多态? ...
- 机器学习经典算法之KNN
一.前言 KNN 的英文叫 K-Nearest Neighbor,应该算是数据挖掘算法中最简单的一种. 先用一个例子体会下. /*请尊重作者劳动成果,转载请标明原文链接:*/ /* https://w ...
- JavaWeb入门_模仿天猫整站Tmall_SSM实践项目
Tmall_SSM 技术栈 Spring MVC+ Mybatis + Spring + Jsp + Tomcat , 是 Java Web 入门非常好的练手项目 效果展示: 模仿天猫前台 模仿天猫后 ...