题目来源:https://leetcode.com/problems/string-to-integer-atoi/

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.

解题思路:

照着要求写代码,可以总结如下:

1. 字串为空或者全是空格,返回0;

2. 字串的前缀空格需要忽略掉;

3. 忽略掉前缀空格后,遇到的第一个字符,如果是‘+’或‘-’号,继续往后读;如果是数字,则开始处理数字;如果不是前面的2种,返回0;

4. 处理数字的过程中,如果之后的字符非数字,就停止转换,返回当前值;

5. 在上述处理过程中,如果转换出的值超出了int型的范围,就返回int的最大值或最小值。

Java代码:

 public class Solution {
public int myAtoi(String str) {
int max = Integer.MAX_VALUE;
int min = -Integer.MIN_VALUE;
long result = 0;
str = str.trim();
int len = str.length();
if (len < 1)
return 0;
int start = 0;
boolean neg = false; if (str.charAt(start) == '-' || str.charAt(start) == '+') {
if (str.charAt(start) == '-')
neg = true;
start++;
} for (int i = start; i < len; i++) {
char ch = str.charAt(i); if (ch < '0' || ch > '9')
break;
result = 10 * result + (ch - '0');
if (!neg && result > max)
return max;
if (neg && -result < min)
return min; }
if (neg)
result = -result; return (int) result;
} };

LeetCode 8 String to Integer (string转int)的更多相关文章

  1. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

  2. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  3. 【一天一道LeetCode】#8. String to Integer (atoi)

    一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  4. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  5. LeetCode: String to Integer (atoi) 解题报告

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  6. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  7. 【Leetcode】【Easy】String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  8. LeetCode.8-字符串转整数(String to Integer (atoi))

    这是悦乐书的第349次更新,第374篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第4题(顺位题号是8).实现将字符串转换为整数的atoi方法. 该函数首先去掉所需丢 ...

  9. LeetCode 8. String to Integer (atoi) (字符串到整数)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

随机推荐

  1. Understanding CMS GC Logs--转载

    原文地址:https://blogs.oracle.com/poonam/entry/understanding_cms_gc_logs Understanding CMS GC Logs By Po ...

  2. weblogic集群中获取jndi的方式

    # The following example specifies a list of WebLogic Servers using the same port: ht.put(Context.PRO ...

  3. 启动weblogic的错误:Could not obtain an exclusive lock to the embedded LDAP data files directory

    http://hi.baidu.com/kaisep/item/0e4bf6ee5da001d1ea34c986 源地址 启动weblogic的错误:Could not obtain an exclu ...

  4. left join on

    问题: select * from A left join  f on e.cust=f.account_id where f.status='0' 与 select * from A left jo ...

  5. Win10安装.net framework 4.0失败提示已是操作系统一部分如何解决

    有位用户因为工作需求,所以想在win10系统电脑中安装microsoft .net framework 4.0.可是在安装过程中却失败了,还遇到提示"Microsoft.net framew ...

  6. Web开发常用知识点 - PHP

    Ubuntu下面,如何用PHP代码获得系统的临时文件夹 用PHP方法 sys_get_temp_dir() 来取得系统的临时文件夹路径,比如Ubuntu下面,返回值为"/tmp".

  7. html5菜单折纸效果

    类似猎豹浏览器安装时的用户须知效果. html文件代码,保存为html文件打开: <!DOCTYPE html> <html> <head> <meta ht ...

  8. Android中的消息通知(NotificationManager和Notification)

    下面来谈谈notification,这个notification一般用在电话,短 信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这 ...

  9. IDEA默认常用快捷键

    作为Java的利器,IDEA属实是非常好用,参考网文总结其常用快捷键如下: Ctrl + /(Ctrl + Shift + /):注释或反注释指定的语句.这个是本人最喜欢的,所以排在第一位. Ctrl ...

  10. 重新想象 Windows 8 Store Apps (41) - 打印

    [源码下载] 重新想象 Windows 8 Store Apps (41) - 打印 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 打印 示例1.需要打印的文档Pr ...