LeetCode 8 String to Integer (string转int)
题目来源:https://leetcode.com/problems/string-to-integer-atoi/
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.
解题思路:
照着要求写代码,可以总结如下:
1. 字串为空或者全是空格,返回0;
2. 字串的前缀空格需要忽略掉;
3. 忽略掉前缀空格后,遇到的第一个字符,如果是‘+’或‘-’号,继续往后读;如果是数字,则开始处理数字;如果不是前面的2种,返回0;
4. 处理数字的过程中,如果之后的字符非数字,就停止转换,返回当前值;
5. 在上述处理过程中,如果转换出的值超出了int型的范围,就返回int的最大值或最小值。
Java代码:
public class Solution {
public int myAtoi(String str) {
int max = Integer.MAX_VALUE;
int min = -Integer.MIN_VALUE;
long result = 0;
str = str.trim();
int len = str.length();
if (len < 1)
return 0;
int start = 0;
boolean neg = false;
if (str.charAt(start) == '-' || str.charAt(start) == '+') {
if (str.charAt(start) == '-')
neg = true;
start++;
}
for (int i = start; i < len; i++) {
char ch = str.charAt(i);
if (ch < '0' || ch > '9')
break;
result = 10 * result + (ch - '0');
if (!neg && result > max)
return max;
if (neg && -result < min)
return min;
}
if (neg)
result = -result;
return (int) result;
}
};
LeetCode 8 String to Integer (string转int)的更多相关文章
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- 【一天一道LeetCode】#8. String to Integer (atoi)
一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- 【Leetcode】【Easy】String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- LeetCode.8-字符串转整数(String to Integer (atoi))
这是悦乐书的第349次更新,第374篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第4题(顺位题号是8).实现将字符串转换为整数的atoi方法. 该函数首先去掉所需丢 ...
- LeetCode 8. String to Integer (atoi) (字符串到整数)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
随机推荐
- sizeof()用法汇总【转载】
转载自:http://www.cnblogs.com/chengxin1982/archive/2009/01/13/1374575.html 参考:http://blog.csdn.net/free ...
- .NET框架面向对象分层的个人想理
简单.层次清晰不要过度优化,接口这玩意儿就是个双刃剑,玩好了解藕,玩不好自找麻烦,好的代码永远都是傻瓜都能看懂的. 总结成以下几条: 公用层 代码公用并且与第三方DLL和业务逻辑无关的 独立出来 逻辑 ...
- Angular系列----AngularJS入门教程02:静态模板(转载)
为了说明angularJS如何增强了标准HTML,我们先将创建一个静态HTML页面模板,然后把这个静态HTML页面模板转换成能动态显示的AngularJS模板. 在本步骤中,我们往HTML页面中添加两 ...
- Winform开发框架之权限管理系统改进的经验总结(2)-用户选择界面的设计
在上篇总结随笔<Winform开发框架之权限管理系统改进的经验总结(1)-TreeListLookupEdit控件的使用>介绍了权限管理模块的用户管理部分,其中主要介绍了其中的用户所属公司 ...
- C#中override和new修饰符的区别
(new)“隐藏”,(override)“覆盖”(重写).不过要弄清楚这两个有什么区别确实也很难,因为子类在使用父类方法时根本看不出区别,子类不管父类是new了还是override了,用的都是父类方法 ...
- Unity烂笔头1-自定义INSPECTOR属性窗口节点项
1.添加输入框和标签 LevelScript: using UnityEngine; using System.Collections; public class LevelScript : Mono ...
- Diy页面服务端渲染解决方案
1. 问题由来 在移动互联网电商领域,运营每天需要搭建多个促销页面来吸引用户去点击去购买,一开始程序员临时写个新页面去实现,可这些页面可以用几次就不用了,每次创建新页面去实现费时费力,而且,电商的运营 ...
- csharp: MongoDB
安装配置: Install MongoDB on Windows(安装配置官方参考) http://docs.mongodb.org/manual/tutorial/install-mongodb-o ...
- Spring 接口代理 类代理
1.Question Description : when you use @Transactional annotation and @RequiresPermissions annotation ...
- linux下socket编程
相关结构 //下边这两个结构定义在<sys/types.h>里 //一般的地址结构,只能用于覆盖(把其他地址转换为此类型),且只能引用该地址的sa_family字段 struct sock ...