Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]
题目:
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
翻译:
实现一个atoi函数来把字符串转换为整型变量。
分析:
这道题的AC率只有13.4%,主要是因为对特殊情况的处理上。具体有这么几种情况需要考虑:
1. 前面的空格
2. 除去前面的空格后,可以以“+、-、0”开头,需要做对应的处理
3. 除了起始处可以出现前2种情况提到的非数字字符,其他地方一旦出现,则忽略该字符以及其后的字符
4. 考虑边界,即是否超出Integer.MAX_VALUE,Integer.MIN_VALUE。下面的方案采用long作为临时存储,方便做边界的判断。但是还要考虑是否会超出long的最大值,所以笔者采用length长度做初步判断。
Java版代码(时间复杂度O(n)):
public class Solution {public int myAtoi(String str) {char[] charArr=str.toCharArray();Long result=0L;int startIndex=0;boolean flag=true;//正数int length=0;for(int i=0;i<charArr.length;i++){if(startIndex==i){if(charArr[i]==' '){startIndex++;continue;}if(charArr[i]=='+'||charArr[i]=='0'){continue;}if(charArr[i]=='-'){flag=false;continue;}}if(charArr[i]>='0'&&charArr[i]<='9'){result=result*10+charArr[i]-'0';length++;if(length>10){break;}}else{break;}}if(flag){if(result>Integer.MAX_VALUE){return Integer.MAX_VALUE;}}else{result=-result;if(result<Integer.MIN_VALUE){return Integer.MIN_VALUE;}}return result.intValue();}
}
Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]相关推荐
- Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
- Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]
题目: Given a positive integer, return its corresponding column title as appear in an Excel sheet. For ...
- Leet Code OJ 191. Number of 1 Bits [Difficulty: Easy]
题目: Write a function that takes an unsigned integer and returns the number of '1' bits it has (also ...
- Leet Code OJ 58. Length of Last Word [Difficulty: Easy]
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- Leet Code OJ 171. Excel Sheet Column Number [Difficulty: Easy]
题目: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, r ...
- Leet Code OJ 203. Remove Linked List Elements [Difficulty: Easy]
题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 ...
- Leet Code OJ 328. Odd Even Linked List [Difficulty: Easy]
题目: Given a singly linked list, group all odd nodes together followed by the even nodes. Please note ...
- Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: Easy]
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- Leet Code OJ 388. Longest Absolute File Path [Difficulty: Medium]
题目 Suppose we abstract our file system by a string in the following manner: The string "dir\n\t ...
最新文章
- html input type=quot;filequot;,科技常识:关于type=quot;filequot;的input框样式修改小结...
- matlab去除图片水印_Python利用OpenCV去除图片水印
- mysql一个死锁分析
- 实现 对象在内存中的引用一致性 之第一步
- Linux下root密码丢失和运行级别错误的解决办法
- Spring框架的前世今生以及对Spring的宏观认识
- 农村女人与城市女人的差别
- 操作系统中的一些基本概念
- 【华为云技术分享】干货!!卷积神经网络之LeNet-5迁移实践案例
- 如何验证是否正确安装了CUDA
- wordpress去掉index.php,wordpress去掉url中index.php 创建分类目录404如何解决
- Node Stream 入门与深入
- hdu 6080-度度熊保护村庄
- IOS 隐藏app图标
- cobalt strik启动
- vue结合elementUI实现tag多标签页
- Django开发了个人博客以及开通公众号
- 收敛因子和黄金正弦指引机制的蝴蝶优化算法
- (六)Linux环境部署(Centos+Nginx+Tomcat+Mysql) - 常用命令总结
- 机器视觉2D点旋转中心标定及旋转后坐标计算