[Leetcode]008.String to Integer (atoi)
public class Solution {
public int myAtoi(String str) {
int index = 0, sign = 1, total = 0;
//1. 边界条件判断
if(str.length() == 0) return 0;
//2. 移除空格
while(str.charAt(index) == ' ' && index < str.length())
index ++;
//3. 处理符号位
if(str.charAt(index) == '+' || str.charAt(index) == '-'){
sign = str.charAt(index) == '+' ? 1 : -1;
index ++;
}
//4. 转变为int,并且避免溢出
while(index < str.length()){
int digit = str.charAt(index) - '0';
if(digit < 0 || digit > 9) break;
if(Integer.MAX_VALUE/10 < total || Integer.MAX_VALUE/10 == total && Integer.MAX_VALUE %10 < digit)
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
total = 10 * total + digit;
index ++;
}
return total * sign;
}
}
[Leetcode]008.String to Integer (atoi)的更多相关文章
- 【JAVA、C++】 LeetCode 008 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) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- 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 ...
- 【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--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
随机推荐
- SQL Server DBA十大必备工具使生活轻松
[IT168 技术]曾经和一些DBA和数据库开发人员交流时,问他们都用过一些什么样的DB方面的工具,大部分人除了SSMS和Profile之外,基本就没有使用过 其他工具了;诚然,SSMS和Profil ...
- django TimedRotatingFileHandler log
15.9.6. TimedRotatingFileHandler¶ The TimedRotatingFileHandler class, located in the logging.handler ...
- MD5 校验文件
https://blog.csdn.net/wudishine/article/details/42466831 MD5.h #ifndef MD5_H #define MD5_H #include ...
- 用C语言实现中文到unicode码的转换
转自: http://blog.csdn.net/qq_21792169/article/details/50379275 源文件用不同的编码方式编写,会导致执行结果不一样 由于本人喜欢用Notep ...
- window下git,TortoiseGit安装,以及和github托管项目
下载地址:http://msysgit.github.io/,安装时最好是先装git,再安装TortoiseGit. 一.git安装 1.第一步 2.第二步 3.第三步 4.第四步 5.第五步 6.第 ...
- [413D][搜索]D - Field expansion
http://codeforces.com/contest/799/problem/D 解题关键:因为3^11>100000,所以若只把2单独拿出,最多只需要暴力2^11次,故只需要dfs一下即 ...
- WPF TextBox 多行时回车换行
<Setter Property="TextWrapping" Value="Wrap"></Setter> <Setter Pr ...
- 并行fp-growth图解(mahout)
FP-growth Apriori算法的一个主要瓶颈在于,为了获得较长的频繁模式,需要生成大量的候选短频繁模式.FP-Growth算法是针对这个瓶颈提出来的全新的一种算法模式.目前,在数据挖掘领域,A ...
- 14.Nginx 文件名逻辑漏洞(CVE-2013-4547)
由于博主在渗透网站时发现现在Nginx搭建的网站是越来越多 所以对Nginx的漏洞来一个全面性的复习,本次从Nginx较早的漏洞开始分析. 2013年底,nginx再次爆出漏洞(CVE-2013-45 ...
- SQL笔记:基础篇
1.BETWEEN AND (查询某个区间的数据) 例如:查询user表中年龄在15-30岁的人 SELECT * FROM user WHERE age between 15 and 30 2.IN ...