[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 ...
随机推荐
- CSS布局套路
这篇笔记的目的是记录分别应用float和flex布局的方法.主要是对遇到的问题进行总结. 1.float布局 总结: 1.1 使用float布局要清除浮动,清除的方法是,在父元素添加如下样式 .cle ...
- R语言do.call 函数用法详解
虽然R语言有类型很丰富的数据结构,但是很多时候数据结构比较复杂,那么基本就会用到list这种结构的数据类型.但是list对象很难以文本的形式导出,因此需要一个函数能快速将复杂的list结构扁平化成da ...
- Protobuf3语法详解
定义一个消息类型 先来看一个非常简单的例子.假设你想定义一个"搜索请求"的消息格式,每一个请求含有一个查询字符串.你感兴趣的查询结果所在的页数,以及每一页多少条查询结果.可以采用如 ...
- ACM 最小公倍数
给定两个正整数,计算这两个数的最小公倍数. Input 输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.Output对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行. ...
- PHP 5 Calendar 函数
PHP Calendar 简介 日历扩展包含了简化不同日历格式间的转换的函数. 它是基于 Julian Day Count(儒略日计数),是从公元前 4713 年 1 月 1 日开始计算的. 注释:如 ...
- Python安装与使用的常见问题
1. Python安装问题 到Python官网下载Python最新版本 Windows x86-64 executable installer (64为操作系统选择这个) Windows x86 ex ...
- 20160212.CCPP体系详解(0022天)
程序片段(01):01.二维数组.c 内容概要:二维数组 #include <stdio.h> #include <stdlib.h> //01.关于栈内存开辟数组: // 诀 ...
- MS Office2016留下的坑
背景 问题源自论坛用户反馈,他用管家有几年了,之前使用IE都很正常,没有任何问题,但是最近突然发现,启动IE时,就会出现系统错误提示:无法启动此程序,因为计算机中丢失 api-ms-win-core- ...
- Linux 高性能服务器编程——多线程编程
问题聚焦: 在简单地介绍线程的基本知识之后,主要讨论三个方面的内容: 1 创建线程和结束线程: 2 读取和设置线程属性: 3 线程同步方式:POSIX信号量,互斥锁和条件变量 ...
- Android简易实战教程--第三十二话《使用Lrucache和NetworkImageView加载图片》
转载本专栏每一篇博客请注明转载出处地址,尊重原创.此博客转载链接地址:小杨的博客 http://blog.csdn.net/qq_32059827/article/details/5279131 ...