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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.


题目标签:String

  题目给了我们一个 str,让我们把它 转换为 int。

  其中有很多违规的条件没有说明:

    正负的符号只能有0个 或者 1个;

    符号后面就应该是数字了,如果遇到不是数字的符号,返回目前为止合格的数字,不需要考虑后面的数字;

    如果数字overflow,大于MAX的要返回MAX,小于MIN 的要返回MIN;

    etc。

Java Solution:

Runtime beats 57.67%

完成日期:01/09/2017

关键词:String

关键点:考虑到所有违规情况

 class Solution
{
public int myAtoi(String str)
{
long res = 0; // the res number to return. Note: res to return should be long and cast it to int when return it at the end.
int sign = 1; // the sign before the number. default is 1 (positive).
int index = 0; // index for num string to go through. // Step 0: if parameter str is null or "", then return 0.
if(str.length() == 0 || str == null)
return 0; // Step 1: trim the whitespace.
str = str.trim(); // Step 2: check first char is '+' or '-', move the index by 1 and also sign value.
if(str.charAt(0) == '+')
index++;
else if(str.charAt(0) == '-')
{
index++;
sign = -1; // change the sign to -1 (negative).
} // Step 3: go through the str string.
for(; index<str.length(); index++)
{
// if this char is not a number char, then break. No matter there are more numbers after.
if(str.charAt(index) > '9' || str.charAt(index) < '0')
break; // add this char value into res.
res = res * 10 + (str.charAt(index) - '0'); // char - '0' is the correct int value. // check the num exceed the max or not.
if(res > Integer.MAX_VALUE) // res should be long because here res might be over Integer.max value.
break;
} // Step 4: depending on the sign and max or min value, return res.
if(res * sign >= Integer.MAX_VALUE)
return Integer.MAX_VALUE;
else if(res * sign <= Integer.MIN_VALUE)
return Integer.MIN_VALUE; // goes here meaning the res number doesn't exceed max and min integer value.
return (int)res * sign; // here need to cast res to int.
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

最新文章

  1. android 一些常用开源框架
  2. VO对象和PO对象的区别
  3. springMVC学习笔记(五)
  4. maven-shade-plugin
  5. JSP调用JAVA方法小例子
  6. 从php脚本到浏览器,编码方式浅析
  7. 理论沉淀:RANSAC算法
  8. 10327 - Flip Sort
  9. 【LeetCode练习题】Reverse Linked List II
  10. Android studio dabao
  11. lesson - 1 - IP /DNS /cat !$ /putty 知识扩充
  12. C#ComboBox控件“设置 DataSource 属性后无法修改项集合”的解决方法
  13. [小技巧]ASP.NET Core中如何预压缩静态文件
  14. CentOS-常用安装
  15. 小程序getLocation出现的各种问题汇总
  16. Asp.net中GridView详解《转》
  17. shell无法捕获程序输出的问题
  18. CGLIB代理基础
  19. Expo大作战(二十六)--expo sdk api之Video和WebBrowser
  20. 扩展一个boot的插件—tooltip&amp;做一个基于boot的表达验证

热门文章

  1. LR接口测试---socket
  2. 329.-io流(字符-练习-复制文本文件二)
  3. 网络编程基础_4.2TCP-客户端
  4. IOS 11,UIWebView内容随状态栏高度下移,导致状态栏不透明
  5. 如何使用fio模拟线上环境
  6. 题解 洛谷P1501/BZOJ2631【[国家集训队]Tree II】
  7. 洛谷——P2827 蚯蚓
  8. for 循环新的写法==列表解析
  9. Batchelor Prize
  10. Q-criterion- definition and post-processing