[算法]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.
Analysis
The following cases should be considered for this problem:
1. null or empty string
2. white spaces
3. +/- sign
4. calculate real value
5. handle min & max
Java Solution
public int atoi(String str) {if (str == null || str.length() < 1)return 0;// trim white spacesstr = str.trim();char flag = '+';// check negative or positiveint i = 0;if (str.charAt(0) == '-') {flag = '-';i++;} else if (str.charAt(0) == '+') {i++;}// use double to store resultdouble result = 0;// calculate valuewhile (str.length() > i && str.charAt(i) >= '0' && str.charAt(i) <= '9') {result = result * 10 + (str.charAt(i) - '0');i++;}if (flag == '-')result = -result;// handle max and minif (result > Integer.MAX_VALUE)return Integer.MAX_VALUE;if (result < Integer.MIN_VALUE)return Integer.MIN_VALUE;return (int) result;}
[算法]String to Integer(atoi)的更多相关文章
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- 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 ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
随机推荐
- 人不在囧途 便携式3G上网设备+套餐推介
来源: http://network.pconline.com.cn/317/3174920_all.html [PConline资讯]过年回家,本该是再高兴不过的事,可一想到要在路上颠簸数十个小时, ...
- jQuery 遍历 - eq() 和siblings() 方法
eq() 方法将匹配元素集缩减值指定 index 上的一个. 通过为 index 为 2 的 div 加入适当的类.将其变为蓝色: <!DOCTYPE html> <html> ...
- ACM算法整理(不断补充ing)
动态规划 1.背包问题 (1)01背包 ,n) DFR(v,V,C[i]) F[v]=max(F[v],F[v-C[i]]+W[i]); } //初始化时 //若背包不一定装满F全初始化为0 //若装 ...
- Django--登录认证
COOKIE 与 SESSION 概念 cookie的使用,不止局限于我们所使用的登录认证,cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此 ...
- Unity3D - 性能优化之Draw Call
Unity3D - 性能优化之Draw Call 分类: Unity 3D2012-09-13 11:18 1002人阅读 评论(0) 收藏 举报 性能优化引擎测试脚本图形算法 Unity(或者说基本 ...
- RealProxy AOP过滤方法的参数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- (C#)为应用程式设定运行权限(System.Security类下的GenericIdentity,GenericPrincipal,PrincipalPermission)
最近看书<编写高质量代码改善C#程序的157个建议>,知识点备忘: System.Security.Principal.GenericIdentity==>表示一般用户 System ...
- GreenDao学习
官方文档地址:http://greenrobot.org/greendao/documentation//introduction/ 英语不好的可以使用谷歌翻译,很轻松的看懂文档,不需要FQ. 看不懂 ...
- python操作Excel读写--使用xlrd (转)
(转自:http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html) 一.安装xlrd模块 到python官网下载http://pypi ...
- hihoCoder #1312 : 搜索三·启发式搜索(A*, 康托展开)
原题网址:http://hihocoder.com/problemset/problem/1312 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在小Ho的手机上有 ...