[leetcode]经典算法题- 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.
思路分析:
- 首先判断为空,返回0
- 考虑前面是空格,使用trim()去掉,然后判断长度是否为0,是的话,返回0
- 判断第一个字符是不是+和-,设置变量sign记录。
- 循环取得字符串的数字,考虑字符串中有非数字,遇到就退出,保留前面的数字
- 考虑溢出的情况,溢出返回Integer的最大值和最小值
代码实现:
public class Solution {
public int myAtoi(String str) {
//首先判断空值
if(str == null){
return 0;
}
//去掉空格的情况
str = str.trim();
if(str.length() == 0){
return 0;
}
int sign = 1;
int index = 0;
if(str.charAt(index) == '+'){
index++;
}else if(str.charAt(index) == '-'){
index++;
sign = -1;
}
//取得数字部分,遇到溢出和非数字退出
long number = 0;
for(;index < str.length();index++){
if(str.charAt(index) < '0' || str.charAt(index) > '9'){
break;
}
number = number *10 + (str.charAt(index) - '0');
if(number >= Integer.MAX_VALUE){
break;
}
}
if(number * sign >= Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}
if(number * sign <= Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}
return (int)number*sign;
}
}
我的微信二维码如下,欢迎交流讨论
欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!
微信订阅号二维码如下:
[leetcode]经典算法题- String to Integer (atoi)的更多相关文章
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode第八题--String to Integer (atoi)
Problem: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible inp ...
- LeetCode【8】. String to Integer (atoi) --java实现
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [算法练习]String to Integer (atoi)
题目说明: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
- LeetCode(8)String to Integer (atoi)
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 【leetcode❤python】 8. String to Integer (atoi)
#-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489' ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- 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 ...
随机推荐
- Node.js 流
稳定性: 2 - 不稳定 流是一个抽象接口,在 Node 里被不同的对象实现.例如request to an HTTPserver 是流,stdout 是流.流是可读,可写,或者可读写.所有的流是 E ...
- Node.js HTTP
稳定性: 3 - 稳定 使用 HTTP 服务器或客户端功能必须调用 require('http'). Node 里的 HTTP 接口支持协议里原本比较难用的特性.特别是很大的或块编码的消息.这些接口不 ...
- JavaScript 比较和逻辑运算符
比较和逻辑运算符用于测试 true 或者 false. 比较运算符 比较运算符在逻辑语句中使用,以测定变量或值是否相等. 给定 x=5,下面的表格解释了比较运算符: 实例 »实例 » 大于 大于或等于 ...
- 解读Batch Normalization
原文转自:http://blog.csdn.net/shuzfan/article/details/50723877 本次所讲的内容为Batch Normalization,简称BN,来源于<B ...
- 《Java多线程编程核心技术》推荐
写这篇博客主要是给猿友们推荐一本书<Java多线程编程核心技术>. 之所以要推荐它,主要因为这本书写得十分通俗易懂,以实例贯穿整本书,使得原本抽象的概念,理解起来不再抽象. 只要你有一点点 ...
- Spark:相关错误总结
http://blog.csdn.net/pipisorry/article/details/52916307 路径错误 spark FileNotFoundError: [Errno 2] No s ...
- java获取ip的方式,注意多级代理的方式获取
public String getIP() { String clientIP = ServletActionContext.getRequest().getHeader("x-forwar ...
- 一个整数数组,长度为n,将其分为m份,使各份的和相等,求m的最大值。
比如{3,2,4,3,6} 可以分成 {3,2,4,3,6} m=1; {3,6}{2,4,3} m=2 {3,3}{2,4}{6} m=3 所以m的最大值为3. bool isShare(int* ...
- Android新建工程步骤(AndroidStudio)
1.在 Android Studio 中,创建新项目: 如果您未打开项目,请在 Welcome to Android Studio 窗口中,点击 Start a new Android Studio ...
- JAVA面向对象-----抽象类
1抽象类 为什么使用抽象类 1:定义Dog类 有颜色属性和叫的方法 2:定义Bird类 有颜色属性和叫的方法 3:定义其父类Animal 1:颜色的属性可以使用默认初始化值. 2:叫的方法在父类中如何 ...