String to Integer (atoi)
来源:互联网 发布:安卓6.0源码多大 编辑:程序博客网 时间:2024/06/07 01:07
Implement atoi to convert a string to an integer.
题目解析:atoi函数 把字符串转为整型
思路解析:主要是考虑各种输入的 +123,-123 ,123, 123(空格),算是个比较简单的题目,直接上AC代码
public int myAtoi(String str) { // 安全性检查if (str.length() == 0 || str == null)return 0;// trim过滤空格str = str.trim();// 考虑正负数int i = 0;if (str.charAt(0) == '+')i++;if (str.charAt(0) == '-')i++;double resultNum = 0;while (i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9') {resultNum = resultNum * 10 + (str.charAt(i) - '0');i++;}// 输出结果if (str.charAt(0) == '-')resultNum = -resultNum;if (str.charAt(0) == '+') {if (resultNum > Integer.MAX_VALUE)return Integer.MAX_VALUE;}if (str.charAt(0) == '-') {if (resultNum < Integer.MIN_VALUE)return Integer.MIN_VALUE;}return (int)resultNum; }
0 0
- 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)
- 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)
- String to Integer (atoi)
- String to Integer (atoi)
- CSS基础-4类选择器
- hdu5318 The Goddess Of The Moon (矩阵快速幂优化dp)
- 安装和升级--基础--许可证信息--title and Copyright information
- wxhl ios bj 49 张浩 学习心得体会连载 第三章
- CSS基础-5属性选择器
- String to Integer (atoi)
- VC学习笔记2文档视图操作
- Mac配置Android adb环境变量
- UI006---app九宫格排列下载
- php最简的给图片加水印,详细解答
- 【Leetcode】Rotate List
- mono for android 深入探究APK包结构
- git忽略不需要放到代码仓库的文件夹
- 设置ImageView的大小