[Lintcode]String to Integer II
来源:互联网 发布:c4d r17 mac下载 编辑:程序博客网 时间:2024/06/05 09:12
Example
"10" => 10"-1" => -1"123123123123123" => 2147483647"1.0" => 1
public class Solution { /** * @param str: A string * @return An integer */ public int atoi(String str) { int res = 0, start = 0; boolean neg = false; if(str.length() == 0) return 0; str = str.trim();//(1) char[] arr = str.toCharArray(); if(str.charAt(0) == '-' || str.charAt(0) == '+') {//(2) start = 1; neg = str.charAt(0) == '-'; } for(int i = start; i < arr.length; i++) { char c = str.charAt(i); if(c == '.') {//(3) return neg ? -res : res; } if(c < '0' || c > '9') return neg ? -res : res; else { //正最大数 if(!neg && (res * (res * 10 + c - '0') < 0)) return Integer.MAX_VALUE; //负最大数 else if(neg && (-res * (-res * 10 - c + '0') < 0)) return Integer.MIN_VALUE; else { res = res * 10 + c - '0'; } } } return neg ? -res : res; }}
0 0
- lintcode:String to Integer II
- [Lintcode]String to Integer II
- LintCode54: String to Integer II
- Lintcode54 String to Integer II solution 题解
- *[Lintcode]Roman to Integer
- string to integer & integer to string
- string to integer
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- [LeetCode]String to Integer
- string to integer (atoi)
- String to Integer (atoi)
- zxing实现二维码生成和解析
- Java取小数点6位数
- 生活遐想
- Can you solve this equation?
- BZOJ4710 [Jsoi2011]分特产
- [Lintcode]String to Integer II
- 处理The content of the adapter has changed but ListView did not receive a notification异常
- ORACLE常用函数
- AppBarLayout,CollapsingToolbarLayout以及Toolbar
- 封装的RedisUtil工具类
- 阻塞、非阻塞、多路复用、异步IO
- struts2的DevMode模式
- iOS中URL字符串的URLencode与URLdecode
- 51Nod - 1043 dp