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

  * 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. 某音乐类App评论相关API的分析及SQL注入尝试

    关键字:APIfen.工具使用.sql注入 涉及工具/包:Fiddler.Burpsuite.Js2Py.Closure Compiler.selenium.phantomjs.sqlmap 摘要: ...

  2. 基于SNMP的交换机入侵的内网渗透

    前言:局域网在管理中常常使用SNMP协议来进行设备的管理和监控,而SNMP的弱点也成为了我们此次渗透的关键. 使用SNMP管理设备只需要一个community string,而这个所谓的密码经常采用默 ...

  3. imagemagick imagick

    imagemagick#图像处理软件 安装解压 ./configure make make install imagick#是php图像扩展模块 调用imagemagick处理图像 安装解压/opt/ ...

  4. apue学习笔记(第八章 进程控制)

    本章介绍UNIX系统的进程控制,包括创建新进程.执行程序和进程终止. 进程标识 每一个进程都有一个非负整数表示的唯一进程ID,除了进程ID,每个进程还有一些其他标识符.下列函数返回这些标识符 #inc ...

  5. Oracle 修改字段注释

    修改字段注释SQL: COMMENT ON COLUMN 表名.字段名 IS '注释内容';             批量修改所有表的字段注释,这里是修改所有没有注释的字段   附件:   实现步骤: ...

  6. 集团管控的历史读本——Leo鉴书76

    当下中国管理咨询界比較火的课题之中的一个就是"集团管控".公司大了之后怎样正好的用人.怎样对下属分公司不失控制.怎样在二代接手之后系统仍然稳固.种种问题都在困扰着企业们.假设我们把 ...

  7. 安装CoreOS到磁盘

    1 打开翻&&墙软件 2 打开迅雷,启用“使用IE代理”,下载以下两个文件:(翻&&墙后用IE下会中断) http://stable.release.core-os.n ...

  8. ros学习网站

    ROS机器人操作系统入门-中国大学MOOC      https://www.bilibili.com/video/av24585414/?p=39 http://i.youku.com/i/UNDA ...

  9. Msfvenom 学习笔记与总结

    平台:Android,可用Payload: android/meterpreter/reverse_http Run a meterpreter server on Android. Tunnel c ...

  10. modelsim-altera IP核仿真

    modelsim 仿真fifo时出现 Instantiation of 'scfifo' failed. The design unit was not found. 2012-07-21 13:27 ...