LeetCode(8) - String to Integer (atoi)
虽然是easy,却是比较繁琐的一道题,需要考虑各种边界条件。在WA了好几遍之后,才把各种边界条件给补全。需要考虑到的因素如下:
- 输入不合法字符,非"0-9",对于首位,合法字符还包括"+"和"-"来代表正负号;
- 最前面允许出现很多个空格,即" 56";
- 当中间出现不合法字符,输出该不合法字符前面有效字符,如"+56a123",输出就是56;
- 注意越界的情况。
当注意了上述情况之后,就没有太大问题了:
public class Solution {
public int myAtoi(String str) {
if (str.length() == 0) return 0;
int index = 0;
//前面是空格的情况
while (str.charAt(index) == ' ') index++;
char c0 = str.charAt(index++);
long num = c0 < '0' || c0 > '9'? 0 : c0 - '0';
int flag = 1;
//记录正负号。
if (c0 == '-') flag = -1;
//判断首位的不合法字符
if ((c0 < '0' || c0 > '9') && (c0 != '+'&& c0 != '-')) return 0;
for (int i = index; i < str.length(); i++) {
char c = str.charAt(i);
//判断不合法字符,若不合法,马上返回
if (c < '0' || c > '9') {
if (flag == -1) num = -num;
return (int)num;
}
num = num * 10 + (c - '0');
//注意越界的情况
if (num > 2147483647 && flag == 1) return 2147483647;
if (num > 2147483647 && flag == -1) return -2147483648;
}
//正负号的修正
if (flag == -1) num = -num;
return (int) num;
}
}
LeetCode(8) - String to Integer (atoi)的更多相关文章
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【一天一道LeetCode】#8. String to Integer (atoi)
一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- 【LeetCode】8. String to Integer (atoi) 字符串转换整数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...
- 【LeetCode】8. String to Integer (atoi) 字符串转整数
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【leetcode】8. String to Integer (atoi)
题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
- 【LeetCode】008. 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)
又是一道恶心的简单题. 一开始没想到这么多情况的,幸好LeetCode是个很人性化的oj,能让你知道你在哪个case上错了,否则一辈子都过不了. 考虑不周到只能一个个补了. 列举一下恶心的case / ...
- 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 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
随机推荐
- 如何在ubuntu下安装合适的翻译词典
http://jingyan.baidu.com/article/9faa7231523dd6473c28cb3f.html
- 函数mem_pool_create
/********************************************************************//** Creates a memory pool. @re ...
- Qt之QLabel
简述 QLabel提供了一个文本或图像的显示,没有提供用户交互功能. 一个QLabel可以包含以下任意内容类型: 内容 设置 纯文本 使用setText()设置一个QString 富文本 使用setT ...
- Asp.Net验证码2
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System. ...
- 淘宝主搜索离线集群完成Hadoop 2
淘宝搜索离线dump集群(hadoop&hbase)2013进行了几次重大升级,本文中将这些升级的详细过程.升级中所遇到的问题以及这些问题的解决方案分享给大家.至此,淘宝主搜索离线集群完全进入 ...
- memcached增删改查
1)add语法:add key flag expire byteskey 键flag 标志expire 过期时间,可以是秒或一个具体的时间戳bytes 要存的东西的bytes长度 PS:只能添加内存里 ...
- win32 API 学习
SendMessage 函数原型 LRESULT SendMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM IParam) 详情:百度百科 msd ...
- core文件分析
http://baidutech.blog.51cto.com/4114344/904419/ http://www.newsmth.net/pc/pccon.php?id=10001977& ...
- 使用RoboCopy 命令
经常进行文件管理操作的朋友们,不满意于Windows系统内置的复制功能,因为它太龟速了.于是大家就使用FastCopy.TeraCopy之类的软件来加速复制,但是你是否知道Windows 7已经内置快 ...
- @Component @Repository @Service @Controller
Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring ...