[LeetCode] String to Integer (atoi)
来源:互联网 发布:暮光女 出柜 知乎 编辑:程序博客网 时间:2024/06/07 13:29
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
Update (2015-02-10):
The signature of the C++
function had been updated. If you still see your function signature accepts a const char *
argument, please click the reload button to reset your code definition.
public class Solution { public int myAtoi(String str) { int j=0; for(;j<str.length();j++){ if(str.charAt(j)!=' ') break; } str=str.substring(j); if(str.length()==0) return 0; boolean flag=true; if(str.charAt(0)=='+'){ str=str.substring(1); }else if(str.charAt(0)=='-'){ str=str.substring(1); flag=false; } long re=0L; for(int i=0;i<str.length();i++){ char ch=str.charAt(i); if(ch>'9'||ch<'0') break; re=re*10+ch-'0'; if(flag&&re>Integer.MAX_VALUE) return Integer.MAX_VALUE; else if(!flag&&-re<Integer.MIN_VALUE) return Integer.MIN_VALUE; } return (int)(flag?re:-re); }}
阅读全文
1 0
- LeetCode:String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- LeetCode String to Integer(atoi)
- LeetCode :String to Integer (atoi)
- Leetcode: String to Integer (atoi)
- Leetcode : String to Integer (atoi)
- 【leetcode】String to Integer (atoi)
- [LeetCode]String to Integer (atoi)
- LeetCode-String to Integer (atoi)
- [leetcode] String to Integer (atoi)
- LeetCode - String to Integer (atoi)
- 【leetcode】String to Integer (atoi)
- LeetCode - String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- Leetcode: String to Integer (atoi)
- [LeetCode] String to Integer (atoi)
- leetcode String to Integer (atoi)
- [LeetCode]String to Integer (atoi)
- EXCEL 后台导出
- 牛腩新闻发布系统--分页学习(AspnetPager控件)
- Pandas中,一组数据中,分离出已有数据和新数据
- shell中颜色的设置(不错的总结,别人的)
- HDU 1506 Largest Rectangle in a Histogram (单调栈)
- [LeetCode] String to Integer (atoi)
- 华为云桌面每台服务器应发布虚拟机台数计算
- 在C语言中什么是定义?什么是声明?它们有何区别?
- HDU-1024-Max Sum Plus Plus
- 通过nginx实现windows系统下本地目录的映射
- 利用HttpSessionListener监听器实现用户数据存储
- ZCMU-1016: 组合
- CentOS 7静默(无图形化界面)安装Oracle 11g
- 计算机端口讲解