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

  * 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. IOS之怎样把自己开发的App安装到越狱的手机

    场景: 有开发人员账号,能够把设备加到开发人员账号中,真机调试.如今须要打包,安装到的越狱手机上(此越狱手机没有加到开发人员账号中,另外公司的人). 常识: 没有越狱的话,最大的问题就是设备的签名,每 ...

  2. log4j.properties(信息打印)

    ### set log levels ###log4j.rootLogger = INFO , console , debug , error ### console ###log4j.appende ...

  3. Openstack nova代码部分凝视一

    做个一个没怎么学过python的菜鸟.看源代码是最好的学习方式了,如今就从nova入手,主要凝视一下 nova/compute/api.py 中的 create_instance函数 def _cre ...

  4. SVN切分支步骤

    1.右键project选择Brankch/Tag 2.选择SVN路径并在改路径下填写project名称 3.选择最新版本号 4.填写必要的凝视备忘,方便日后查看 5.刷新父文件夹文件夹.下载被切出来的 ...

  5. 【React Native开发】React Native控件之DrawerLayoutAndroid抽屉导航切换组件解说(13)

    ),请不要反复加群! 欢迎各位大牛,React Native技术爱好者增加交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文章技术推送! 该DrawerLayoutAndroid组件封 ...

  6. Java reference的种类及使用场景

    Java 中一共有 4 种类型的引用 : StrongReference. SoftReference. WeakReference 以及 PhantomReference (传说中的幽灵引用).这  ...

  7. POJ 开关问题 1830【高斯消元求矩阵的秩】

    Language: Default 开关问题 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6656   Accepted: ...

  8. CentOS7.1 KVM虚拟化之经常使用管理虚拟机命令(3)

    一.查看虚拟机列表及状态 [root@kvm01 ~]# virsh list --all Id Name State ---------------------------------------- ...

  9. jquery代码小片段

    1. 使用jQuery来切换样式表 //找出你希望切换的媒体类型(media-type),然后把href设置成新的样式表. $(‘link[media="screen"]‘).at ...

  10. (一)关于jQuery的网上资源

    jQuery官网: http://jquery.com/ jQuery API: http://jquery.cuishifeng.cn/ w3school学习网站:http://www.w3scho ...