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.

列举需要注意的集中情况

1、去空格 trim()

2、首字母为“+”或者“-”,则要记录下来。设一个flag变量,用于存储符号位

3、数字前出现了非数字字符,则返回0

4、在数字中出现了非数字字符,则返回前面的一些数字字符组成的数,后面的直接舍弃

5、存在数字越界的情况,所以要对最后得到的数字进行判断

public static int atoi(String str){
if(str == null || str.length() == 0)
{
return 0;
}
str = str.trim();
int flag = 1;
int start = 0;
long res = 0;
if(str.charAt(0)=='+'){
flag = 1;
start++;
}else if(str.charAt(0)=='-'){
flag = -1;
start++;
}
for(int i = start;i<str.length();i++){
if(Character.isDigit(str.charAt(i))){
return (int)res*flag;
}
res = res*10 + str.charAt(i)-'0';
}
if (flag == 1 && res > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
if (flag == -1 && (-1) * res < Integer.MIN_VALUE)
return Integer.MIN_VALUE; return (int)(flag * res); }

【LeedCode】String to integer(atoi)的更多相关文章

  1. 【leetcode】String to Integer (atoi)

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

  2. LeetCode【8】. String to Integer (atoi) --java实现

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

  3. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  4. 【Leetcode】【Easy】String to Integer (atoi)

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

  5. 【Leetcode-easy】String to Integer(atoi)

    题目要求:字符串->整型 * 1. 首先需要丢弃字符串前面的空格. * 2. 然后可能有正负号(注意只取一个,如果有多个正负号,那么说这个字符串是无法转换的,返回0.比如测试用例里就有个“+-2 ...

  6. 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  7. 【Leet Code】String to Integer (atoi) ——常考类型题

    String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement ato ...

  8. 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)

    Implement atoi which converts a string to an integer.The function first discards as many whitespace ...

  9. 【leetcode刷题笔记】String to Integer (atoi)

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

随机推荐

  1. chrome谷歌浏览器导致的密码被修改现象

    版本 68.0.3440.106(正式版本) (32 位)记住密码功能有个缺陷,会把自己的密码自动填写到别人的密码框中,假如这个时候点击保存密码,就会导致其他用户的密码被修改为登录用户的密码.   很 ...

  2. TLS协议分析

    TLS协议分析 本文目标: 学习鉴赏TLS协议的设计,透彻理解原理和重点细节 跟进一下密码学应用领域的历史和进展 整理现代加密通信协议设计的一般思路 本文有门槛,读者需要对现代密码学有清晰而系统的理解 ...

  3. stap中的entry函数

    只有在ret probe函数中,在这个函数中才会使用@entry函数去提取变量 是因为ret probe 有什么特殊的吗?在中间这个变量会变化吗? A new operator, @entry, is ...

  4. JSP表单提交出现中文乱码的解决方法

    1)post方式 在servlet的doGet( )  doPost( )  中增加以下代码: response.setContentType("text/html;charset=utf- ...

  5. Partial Class部分类

    Partial Class,部分类 或者分布类.顾名思义,就是将一个类分成多个部分.比如说:一个类中有3个方法,在VS 2005将该类中3个方法分别存放在3个不同的.cs文件中.这样做的好处:1.一个 ...

  6. Git 删除服务器的远程分支

    git push origin :分支名 可能会出现,在A机子操作,刷新下成功删除,但在B机子上还显示,再用下命令提示不存在该分支,只要再推送一个任意分支即可正常显示

  7. Educational Codeforces Round 56 (Rated for Div. 2) ABCD

    题目链接:https://codeforces.com/contest/1093 A. Dice Rolling 题意: 有一个号数为2-7的骰子,现在有一个人他想扔到几就能扔到几,现在问需要扔多少次 ...

  8. poj 2104 (主席树写法)

    //求第K的的值 1 #include<stdio.h> #include<iostream> #include<algorithm> #include<cs ...

  9. 在linux环境下让java代码生效的步骤

    1.kill jboss 2.compile 3.deploy 4.bootstrap jboss.

  10. (转)用python获取页面返回的cookie

    网址如下: crifan:http://www.crifan.com/get_cookie_from_web_response_in_python/ . . . .