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 ...
随机推荐
- PowerDesigner导出表到word
一.模版修改 在导出表时,powerdesigner默认为我们提供了很多的模版,在工具栏中选择[Report--->Report Template]//// [被翻译成报告(Report)--- ...
- 《c程序设计语言》读书笔记--闰年和字符输入不用 && ||
#include <stdio.h> #include <string.h> #define sta 1500 #define Num 1600 int main() { in ...
- ZOJ Problem Set - 3804 YY's Minions
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5336 比较简单的模拟题,题意也很好理解. #include <iostr ...
- Android内存管理(1)WRANGLING DALVIK: MEMORY MANAGEMENT IN ANDROID PART 1
from : http://www.raizlabs.com/dev/2014/03/wrangling-dalvik-memory-management-in-android-part-1-of-2 ...
- [androd] android的在线源码网站,各个版本都有(目前已到俺android 4.2,但不包含kernel部分的代码)
android的在线源码阅读网站,各个版本都有(目前最新版本已到android 4.2,但不包含kernel部分的代码) 这个网站最大的特点是:可以在网页上方的搜索框,搜索整个网站所存储的源码中的字符 ...
- Redis Cluster架构和设计机制简单介绍
之前另一篇文章也介绍了 Redis Cluster (link,在文章的后半部分) 今天看到这一篇,简单说一下(http://hot66hot.iteye.com/blog/2050676) 作者的目 ...
- Hybris 预备知识学习列表
需要学习的: Spring,Spring对于hybris非常重要,整个hybris是基于spring的架构之上. 包括例如spring mvc,spring security, 设计模式 软件构建工具 ...
- laravel, Composer和autoloading
http://www.php-fig.org/psr/psr-4/ http://www.php-fig.org/psr/psr-0/ http://alanstorm.com/laravel_com ...
- 有用的shell命令
1. 查找目录中大小前10 du -hsx * | sort -rh | head -10 2.
- Openerp上传中文名附件,下载时报错的处理方法
文档管理中,如果上传的文件名含有中文字符,下载时会提示出错,如没有权限等.这个问题困惑我比较久的时间,通过跟踪openerp_server.log,可以看到类似提示: 2012-09-28 21:51 ...