[LeetCode] 8. String to Integer (atoi)
Implement atoi to convert a string to an integer.
public class Solution {
public int myAtoi(String s) {
if (s == null || s.length() < 1) {
return 0;
}
int i = 0;
while (i < s.length() && s.charAt(i) == ' ') {
i++;
}
if (i == s.length()) {
return 0;
}
boolean isN = false;
if (s.charAt(i) == '-') {
isN = true;
i++;
} else if (s.charAt(i) == '+') {
i++;
}
double r = 0;
for (; i < s.length(); i++) {
if (s.charAt(i) < '0' || s.charAt(i) > '9') {
break;
}
r = r * 10 + (s.charAt(i) - '0');
}
if (isN) {
r = -r;
if (r < Integer.MIN_VALUE) {
return Integer.MIN_VALUE;
}
} else if (r > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
return (int)r;
}
}
[LeetCode] 8. String to Integer (atoi)的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- 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] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- 【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 ...
- LeetCode 7 -- String to Integer (atoi)
Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...
随机推荐
- 数据挖掘系列(1)关联规则挖掘基本概念与Aprior算法
整理数据挖掘的基本概念和算法,包括关联规则挖掘.分类.聚类的常用算法,敬请期待.今天讲的是关联规则挖掘的最基本的知识. 关联规则挖掘在电商.零售.大气物理.生物医学已经有了广泛的应用,本篇文章将介绍一 ...
- Eclipse Java class修改后的即时编译
通常情况下,修改了java文件,需要重启eclipse.但是myeclipse可以不用. 其实即时编译早就有了,通过简单配置javaRebel配置,可以达到修改java文件后不重启eclipse. 注 ...
- HTTP简介
HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议.. HT ...
- H3C汇聚层交换机认证在线人数展示系统之需求说明和功能点说明
一.需求 (一)每五分钟查询一次交换机的连接情况: (二)每2.5分钟更新每栋楼的连接情况. 二.功能点 序号 功能点说明 待定 完成 未完成 完成时间 预计用时(min) 实际用时(min) 备注 ...
- ScriptableObject本地序列化后重启Unity后报The associated script can not be loaded.Please fix any compile errors and assign a valid script的坑
踩坑 做编辑器一些设置序列化存在本地的时候,继承自ScriptableObject的类通过 创建的asset文件. 在重启Unity后查看这个asset发现上面的所有序列化属性丢失,报的错就是 在不存 ...
- 在Sublime Text 3 中安装SublimeLinter,Node.js进行JS&CSS代码校验
转载自:http://www.wiibil.com/website/sublimelinter-jshint-csslint.html 在Sublime Text中安装SublimeLinter,No ...
- Python实现对文件夹内文本文件递归查找
平台:Win7 64 bit,IDLE Python 3.4.0 经常有这样的需求:在一个文本文件里查找特定字符串,这很好实现,用任何文本查看工具几乎都可以做到.而有的时候,想查找一个文件夹下的所有文 ...
- 【科普】为什么WiFi自动信道选到的信道多数在1/6/11
http://bbs.hiwifi.com/thread-4126-1-1.html 论坛上不少朋友很困惑,为什么小极的自动信道选择选到的信道只会在1.6.11这三个信道呢?WiFi不是一共有10几个 ...
- Latex 数学符号
本文完全转自 <常用数学符号的 LaTeX 表示方法>,在此转载仅仅为了便于查阅,谨向原作者致以崇高的敬意. 常用数学符号的 LaTeX 表示方法 (以下内容主要摘自“一份不太简短的 LA ...
- PHP Web实时消息后台服务器推送技术---GoEasy
越来越多的项目需要用到实时消息的推送与接收,怎样用php实现最方便呢?我这里推荐大家使用GoEasy, 它是一款第三方推送服务平台,使用它的API可以轻松搞定实时推送! 浏览器兼容性:GoEasy推送 ...