题目要求:字符串->整型

  * 1. 首先需要丢弃字符串前面的空格。
  * 2. 然后可能有正负号(注意只取一个,如果有多个正负号,那么说这个字符串是无法转换的,返回0。比如测试用例里就有个“+-2”)。
  * 3. 字符串可以包含0~9以外的字符,如果遇到非数字字符,那么只取该字符之前的部分,如“-00123a66”返回为“-123”。
  * 4. 如果超出int的范围,返回边界值(2147483647或-2147483648)。

思路:顺序读取,顺序处理。正数,result=result*10+digit ;负数:result=reuslt*10-digit

     public int myAtoi(String str) {
if(str==null||str.length()==0) return 0;
str=str.trim(); boolean negative=false;
int i=0;
if(str.charAt(i)=='+'){
i++;
}else if(str.charAt(i)=='-'){
negative=true;
i++;
}
double result=0; //必须要先使用double,否则会先越界
for(;i<str.length();i++){
int digit=str.charAt(i)-'0';
if(digit<0||digit>9) break;
if(negative==false){
result=result*10+digit;
if(result>Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}
}else{
result=result*10-digit;
if(result<Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}
}
}
return (int)result;
}

【Leetcode-easy】String to Integer(atoi)的更多相关文章

  1. 【Leet Code】String to Integer (atoi) ——常考类型题

    String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement ato ...

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

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

  3. 【leetcode】String to Integer (atoi)

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

  4. LeetCode【8】. String to Integer (atoi) --java实现

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

  5. 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

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

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

  7. 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)

    Implement atoi which converts a string to an integer.The function first discards as many whitespace ...

  8. 【leetcode刷题笔记】String to Integer (atoi)

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

  9. 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  10. 【LeetCode 8_字符串_实现】String to Integer (atoi)

    , INVALID}; int g_status; long long SubStrToInt(const char* str, bool minus) { ; : ; while (*str != ...

随机推荐

  1. php 列出当前目录

    $path="."; //初使化用户所操作目录 $prevpath=dirname($path); //初使化当前脚本所在目录 $dir_handle=opendir($path) ...

  2. HDFS冗余数据块的自动删除

    HDFS冗余数据块的自动删除 在日常维护hadoop集群的过程中发现这样一种情况: 某个节点由于网络故障或者DataNode进程死亡,被NameNode判定为死亡,HDFS马上自动开始数据块的容错拷贝 ...

  3. 「工具」Dubbo可视化测试工具的设计和实现

    「工具」Dubbo可视化测试工具的设计和实现 学习了:https://blog.csdn.net/qq355667166/article/details/78914453

  4. 过滤器Filter_03_多个Filter的执行顺序

    过滤器Filter_03_多个Filter的执行顺序 学习了:https://www.cnblogs.com/HigginCui/p/5772514.html 按照在web.xml中的顺序进行filt ...

  5. Spring3和Quartz2的应用实例

    /** * 任务调度类 * @author Joyce.Luo * @date 2015-3-31 下午03:32:04 * @version V3.0 * @since Tomcat6.0,Jdk1 ...

  6. spring-struts-mybatis整合错误集锦

    尽管三大框架特别特别的好用,可是,当我第一次把这三个框架用maven整合到一起的时候.各种错误接踵而至,以下来做一下三大框架整合的总结: 首先是在导入三大框架的各种依赖包的时候,由于我用的是j2ee  ...

  7. POJ 1698 Alice&#39;s Chance(最大流+拆点)

    POJ 1698 Alice's Chance 题目链接 题意:拍n部电影.每部电影要在前w星期完毕,而且一周仅仅有一些天是能够拍的,每部电影有个须要的总时间,问能否拍完电影 思路:源点向每部电影连边 ...

  8. (LeetCode)两个链表的第一个公共节点

    LeetCode上面的题目例如以下: Write a program to find the node at which the intersection of two singly linked l ...

  9. 数据库sql的join多表

    摘录文章 SQL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据.注意,join后的数据记录数不一定就是左或右表的简单连接,图表只代表集合关系,在数量上并不准确,如这个条件后结果, ...

  10. ubuntu16.04下Cmake学习一

    根据网上的资料,我总结了一下,一个工程应该有根目录(bin)存放可执行文件,头文件目录(include)存放头文件,源码文件(src)存放你的算法,还需要一个库文件夹存放你编译的静态库或者动态库.然后 ...