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

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

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

  1. public int myAtoi(String str) {
  2. if(str==null||str.length()==0) return 0;
  3. str=str.trim();
  4.  
  5. boolean negative=false;
  6. int i=0;
  7. if(str.charAt(i)=='+'){
  8. i++;
  9. }else if(str.charAt(i)=='-'){
  10. negative=true;
  11. i++;
  12. }
  13. double result=0; //必须要先使用double,否则会先越界
  14. for(;i<str.length();i++){
  15. int digit=str.charAt(i)-'0';
  16. if(digit<0||digit>9) break;
  17. if(negative==false){
  18. result=result*10+digit;
  19. if(result>Integer.MAX_VALUE){
  20. return Integer.MAX_VALUE;
  21. }
  22. }else{
  23. result=result*10-digit;
  24. if(result<Integer.MIN_VALUE){
  25. return Integer.MIN_VALUE;
  26. }
  27. }
  28. }
  29. return (int)result;
  30. }

【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. 2016.11.29 activiti实战--第19章--统一身份管理(含自定义用户与数组的实现)

    学习资料:<Activiti实战> 第十九章 统一身份管理 本章讲解如何统一业务系统与activiti的用户管理系统. 第5章的时候已经讲解过activiti的用户与组.一般来说业务系统都 ...

  2. oracle软件安装完毕之后,如何创建数据库

    oracle软件安装完毕之后,如何创建数据库 学习了:https://zhidao.baidu.com/question/1800966379896476147.html 使用了Database Co ...

  3. PHP提权之异步执行

    在服务器上都会定时运行一些脚本以完成周期性的任务. 而这些脚本往往是以root权限启动的, 替换或者改变其中的内容就可以完成提权.而今天在这要讲解的就是php提权中的异步执行方法. 在php中一般大家 ...

  4. java调试工具jdb

    Finds and fixes bugs in Java platform programs. Synopsis jdb [options] [classname] [arguments] optio ...

  5. vue2.X v-model 指令

    1.v-model指令 <!DOCTYPE html> <html> <head> <title></title> <script s ...

  6. iptables和DNS

    1.iptables防火墙 表→链→规则 filter表 数据过滤表 NAT表---内网和外网的地址转换 Mangle-----数据流量,通过防火墙设置流量.特殊数据包标记.太复杂,一般不用.限速工具 ...

  7. 【Python】向函数传递列表

    向函数传递列表 在实际使用中你会发现,向函数传递列表是比较实用的,这种列表可能包含名字.数字.可能更复杂的对象(字典) 假设向一个函数传递一堆水果,我们说出我们喜欢所有的水果 def Obj(frui ...

  8. Windows Thin PC体验 & 语言包更改(win 7 included)

    本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 简介: Window ...

  9. vscode 编译调试c/c++的环境配置

    首先看了一下别人写的文章 http://blog.csdn.net/c_duoduo/article/details/51615381 在按照上文链接博主的安装步骤进行到MINGW的安装时出现一个问题 ...

  10. dm8148 videoM3 link源代码解析

    样例:从A8送一帧jpeg图片到videoM3解码,然后在将解码的数据传递到A8, 这个流程涉及的link源代码例如以下: dm8148 link之间数据传递 1)在A8上调用IpcBitsOutLi ...