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.

Update (2015-02-10):

The signature of the C++ function had been updated. If you still see your function signature accepts a const
char *
 argument, please click the reload button  to reset your code definition.

本文简单点说就是把字符串里的数字转成整形,可是题目有几点要求:

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,若有返回数值超出边界,则返回边界值。

一、思路:

       本题尽管属于比較简单的级别。可是在没考虑好所有可能的情况下。总是会出现遗漏一些细节的结果,导致提交后总是有新的意外情况出现。如:“     +-15651”、“    *    6644”这些首次出现的字符串不能构成数字的都是属于不合法的情况。直接返回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实现的更多相关文章

  1. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  2. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  3. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

  4. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  5. [leetcode] 8. String to Integer (atoi) (Medium)

    实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...

  6. Leetcode 8. String to Integer (atoi)(模拟题,水)

    8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...

  7. [LeetCode][Python]String to Integer (atoi)

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...

  8. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  9. Java [leetcode 8] String to Integer (atoi)

    问题描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

随机推荐

  1. LeetCode 11. Container With Most Water 单调队列

    题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...

  2. MyBatis学习总结(1)——MyBatis快速入门

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

  3. ASP.NET-表单验证-DataAnnotations

    DataAnnotations  [数据注解,数据注释] 需要引入两个脚本文件 <script src="@Url.Content("~/Scripts/jquery.val ...

  4. UVA 11294 - Wedding(Two-Set)

    UVA 11294 - Wedding 题目链接 题意:有n对夫妻,0号是公主.如今有一些通奸关系(男男,女女也是可能的)然后要求人分配在两側.夫妻不能坐同一側.而且公主对面一側不能有两个同奸的人,问 ...

  5. bzoj5157: [Tjoi2014]上升子序列(树状数组LIS)

    5157: [Tjoi2014]上升子序列 题目:传送门 题解: 学一下nlogn的树状数组求最长上生子序列就ok(%爆大佬) 离散化之后,用一个数组记录一下,直接树状数组做 吐槽:妈耶...一开始不 ...

  6. SAS小记

    2011年8月13日 最近一直在跟着李东风的<统计软件教程>学习SAS,刚刚学完初等统计,感觉还没入门,找不到matlab编程时那种手顺的感觉.继续学习吧,加油!     最近用spss处 ...

  7. HD-ACM算法专攻系列(5)——N!

    题目描述: 源码: #include"iostream" using namespace std; int main() { int n, digit, carry, tmp; i ...

  8. BZOJ 2793: [Poi2012]Vouchers(调和级数)

    Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 582  Solved: 250[Submit][Status][Discuss] Description ...

  9. Codeforces 987B. High School: Become Human

    解题思路: 1.题意:判断x^y和y^x谁大谁小. 2.由于x^y和y^x太大了,时间复杂度也不允许,所以做同等变换,比较e^(ylnx)和e^(xlny). 3.即为比较ylnx和xlny的大小. ...

  10. SpringCloud学习笔记(5)----Spring Cloud Netflix之Eureka的服务认证和集群

    1. Eureka服务认证 1. 引入依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...