Implement atoi which converts a string to an integer.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned.

Note:

  • Only the space character ' ' is considered as whitespace character.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

Example 1:

Input: "42"
Output: 42

Example 2:

Input: "   -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
  Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical
  digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:

Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
  Thefore INT_MIN (−231) is returned.
class Solution {
public int myAtoi(String str) {
if (str == null) {
return 0;
}
str = str.trim();
if (str.length() == 0) {
return 0;
}
int first = 0, sign = 1;
// assin with long type;
long res = 0;
if (str.charAt(0) == '+') {
first += 1;
} else if (str.charAt(0) == '-') {
sign = -1;
first += 1;
}
for (int i = first; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return sign * (int)res;
}
res = 10 * res + str.charAt(i) - '0';
if (sign == 1 && res > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
if (sign == -1 && res > Integer.MAX_VALUE) {
return Integer.MIN_VALUE;
}
}
return sign * (int)res;
}
}

最新文章

  1. css学习笔记 10
  2. PSP(16/03/14-16/03/15)
  3. 关于C#本质论和CLR via C#中译本,不吐不快
  4. 010editor 破解 扩展
  5. C语言的关键字,运算符,标识符
  6. 【Android】HorizontalScrollView内子控件横向拖拽
  7. Windows平台下安装Hadoop
  8. Windows上安装使用MongoDB(一)
  9. 设计模式之美:Singleton(单件)
  10. HTML5表单与PHP交互
  11. 第三章 DispatcherServlet详解
  12. 【Nhibernate】HQL 分页
  13. Android组件系列----BroadcastReceiver广播接收器
  14. 3.5 用NPOI操作EXCEL--巧妙使用Excel Chart
  15. Mac下MySQL的安装与配置
  16. 用python爬虫爬取去哪儿4500个热门景点,看看国庆不能去哪儿
  17. h5的localStorage和sessionStorage
  18. PHP二维数组去重(指定键名)
  19. 使用 random() 生成礼包码
  20. DNN网络(二)反向传播算法

热门文章

  1. Python笔记_第一篇_面向过程_第一部分_2.内存详解
  2. php 去除中间空格
  3. 黑马oracle_day02:04.oracle对象&amp;&amp;05.oracle编程(a)
  4. openvino资源
  5. 蓝屏(BSOD)转储设置,看本文就够了!
  6. oracle误删scott文件如何恢复
  7. Python实现自动处理表格,让你拥有更多的自由时间!
  8. Nginx模块-ngx_http_mirror_module-流量复制
  9. spring boot pom demo
  10. Python语言学习:pyc是什么