LeetCode【8】. String to Integer (atoi) --java实现
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 argument, please click the reload button to reset your code definition.
char *
本文简单点说就是把字符串里的数字转成整形,可是题目有几点要求:
Requirements
for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical
digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists
because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
翻译大概意思是:
1、先丢弃字符串前面的空白字符知道扫到第一个非空白字符;然后将一个合法的数值的字符串以及其前面的一个正负号一同装换为int。
2、该合法数值子字符串中或后,可能包括其它非数值的特殊字符,忽略这些子字符串;
3、若第一个非空子字符串非护法的整型数值。或根本就没有,甚至该字符串可能都是特殊字符或空字符,则不作转换0;
4、若没有有效地转换,则返回0,若有返回数值超出边界,则返回边界值。
一、思路:
二、Java程序
public class Solution {
public int myAtoi(String str) {
int sl = str.length();
int index=0;
char tempc; //获取字符
boolean flag = false; //负数标志
int firstI=-1;
long re=0;
if(sl<=0)
return 0;
//1. 開始至第一个非空字符
while(str.charAt(index)==' ')
{
index++;
if(index>=sl) //超过字符串长度
return 0;
}
//2. 扫到第一个非空字符,推断是否合法
tempc = str.charAt(index);
//2.1 若是+-或数值则进行,否则则返回0
if(tempc=='-'||tempc=='+'||(tempc<='9'&&tempc>='0'))
{
//2.2 若是数值则跳出推断
if((tempc<='9'&&tempc>='0'))
{
}else //2.3 若是+-号,则推断接下来的是否为数值
{
if(tempc=='-') flag = true;//2.3.1 若是-号。则设置标志位
index++; //2.4 若是+-号,则推断下一位是不是数值,是则合法,不是则返回0
tempc = str.charAt(index);
if((tempc<='9'&&tempc>='0'))
{}else
{
return 0;
}
}
}else //2.1 若是+-或数值则进行,否则则跳出
{
return 0;
}
//3. 開始扫描有效数字
while(index<sl)
{
tempc = str.charAt(index);
if(tempc<='9'&&tempc>='0')
{
re = re*10 + (str.charAt(index)-'0');
if(re>=Integer.MAX_VALUE||re<=Integer.MIN_VALUE)
break;
}else
{
break;
}
index++;
}
if(flag==true)
{ re=-re;
re = re<Integer.MIN_VALUE?
Integer.MIN_VALUE:re;
}else
{
re = re>Integer.MAX_VALUE?Integer.MAX_VALUE:re;
}
return (int)re;
}
}
LeetCode【8】. String to Integer (atoi) --java实现的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- 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 ...
- 【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) (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- ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- Java [leetcode 8] String to Integer (atoi)
问题描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
随机推荐
- js 学习思维导图
- oracle 表空间Tablespaces
1.表空间 一个数据库可以有多个表空间,一个表空间里可以有多个表.表空间就是存多个表的物理空间: 可以指定表空间的大小位置等. 创建表空间:create tablespace ts1 datafile ...
- C/s模式与B/S模式
C/S模式事是client/server,即客服端/服务模式
- ASP.NET-Session cooike
Application .Cookie和 Session 两种会话有什么不同 答:Application是用来存取整个网站全局的信息,而Session是用来存取与具体某个访问者关联的信息, Sessi ...
- ZOJ 3556
终于做出来了,激动.... 这道题隐藏得深啊,但若推导下来,就变简单了. 首先,一个集合的子集的个数为2^n=s.注意了,题目求的是有序集合组,并且每个集合是可以重复使用的,怎么办呢?这就要想到多重集 ...
- Spring表达式语言SpEL简单介绍
Spring3引入了Spring表达式语言(Spring Expression Language,SpEL). SpEL有非常多特性.比較经常使用的包含: 1.使用bean的id来引用bean, 以下 ...
- nyoj--18--The Triangle(dp水题)
The Triangle 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure ...
- vue 组件之间的传值
父向子传值父组件 <v-footer :projectdat="dat"></v-footer> export default { data() { ret ...
- 启动项目报错:org.springframework.beans.factory.UnsatisfiedDependencyException
dubbo项目: 启动项目报错:(web端) org.springframework.beans.factory.UnsatisfiedDependencyException: Error creat ...
- Java中如何解决线程安全问题
给出一个问题,如下: 解决方案如下: public class Demo_5 { public static void main(String[] args) { //创建一个窗口 TicketWin ...