题目:
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]相关推荐

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

最新文章

  1. html input type=quot;filequot;,科技常识:关于type=quot;filequot;的input框样式修改小结...
  2. matlab去除图片水印_Python利用OpenCV去除图片水印
  3. mysql一个死锁分析
  4. 实现 对象在内存中的引用一致性 之第一步
  5. Linux下root密码丢失和运行级别错误的解决办法
  6. Spring框架的前世今生以及对Spring的宏观认识
  7. 农村女人与城市女人的差别
  8. 操作系统中的一些基本概念
  9. 【华为云技术分享】干货!!卷积神经网络之LeNet-5迁移实践案例
  10. 如何验证是否正确安装了CUDA
  11. wordpress去掉index.php,wordpress去掉url中index.php 创建分类目录404如何解决
  12. Node Stream 入门与深入
  13. hdu 6080-度度熊保护村庄
  14. IOS 隐藏app图标
  15. cobalt strik启动
  16. vue结合elementUI实现tag多标签页
  17. Django开发了个人博客以及开通公众号
  18. 收敛因子和黄金正弦指引机制的蝴蝶优化算法
  19. (六)Linux环境部署(Centos+Nginx+Tomcat+Mysql) - 常用命令总结
  20. 机器视觉2D点旋转中心标定及旋转后坐标计算

热门文章

  1. java中super关键字_java中super关键字有什么用法
  2. NOIP2018提高组比赛总结
  3. 15.IDA-查看XREF列表
  4. 网狐棋牌(二) CQueueServiceEvent初步分析
  5. Linux网络编程 | Socket编程(二)TCPSocket的封装、TCP服务器多进程、多线程版本的实现
  6. 贝壳app Authorization参数分析
  7. Python获取.wav音频的时长
  8. Java NIO 介绍和基本demo
  9. 系统、应用监控的缜密思路,堪称性能瓶颈的克星
  10. 力扣--扁平化嵌套列表迭代器