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.

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. str中整数的前面可能有很多空格,要用str = str.trim(); 去掉,去掉之后还要判断str是否为空了,为空返回0;
  2. str中整数可能带有'+'或者'-',也可以不带,带有负号的时候要单独处理;
  3. str中整数后面可能还有乱七八槽的非数字符号,直接忽略,所以在遍历过程中如果遇到这些符号,说明整数部分遍历结束,要推出循环。
  4. str转换出来的整数有可能大于Integer.MAX_VALUE,此时需要返回Integer.MAX_VALUE;也有可能小于Integer.MIN_VALUE,此时需要返回Integer.MIN_VALUE。

代码如下:

 public class Solution {
public int atoi(String str) {
if(str == null || str.length() == 0)
return 0; str = str.trim();
if(str.length() == 0)
return 0; int kepeler = 0;
boolean isNeg = false;
if(str.charAt(kepeler) == '-'){
isNeg = true;
kepeler++;
}
else if(str.charAt(kepeler) == '+')
kepeler++; long answer = 0;
for(;kepeler < str.length();kepeler++){
if(str.charAt(kepeler) < '0' || str.charAt(kepeler) > '9')
break;
answer = answer*10+str.charAt(kepeler) - '0';
}
if(isNeg){
answer *= -1;
if(answer < Integer.MIN_VALUE)
return Integer.MIN_VALUE;
return (int)answer;
}
else {
if(answer > Integer.MAX_VALUE)
return Integer.MAX_VALUE;
return (int)answer;
}
}
}

【leetcode刷题笔记】String to Integer (atoi)的更多相关文章

  1. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  2. 【leetcode刷题笔记】Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...

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

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

  4. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  5. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  6. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  7. 【leetcode刷题笔记】Anagrams

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  8. 【leetcode刷题笔记】N-Queens

    The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...

  9. LeetCode刷题笔记(3)Java位运算符与使用按位异或(进制之间的转换)

    1.问题描述 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 算法应该具有线性时间复杂度并且不使用额外空间. 输入: [4,1,2,1,2] 输 ...

  10. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

随机推荐

  1. python函数-------python2.7教程学习【廖雪峰版】(三)

    任务: 看完函数这一章    已完成 2017年6月8日16:23:491.函数的作用:写较少的代码实现较多的功能,可以多次被调用.2.可见,借助抽象,我们才能不关心底层的具体计算过程,而直接在更高的 ...

  2. window 怎么样让nginx开机自启动

    安装Nginx 下载windows版nginx (http://nginx.org/download/nginx-1.10.0.zip),之后解压到需要放置的位置(D:\xampp\nginx) 将N ...

  3. window下php5安装redis扩展 设置自启动服务

    最近想在5.6版本的开发环境装一下redis的扩展,结果找了半天都是失效链接,特此做下备份 5.3-5.6 https://pecl.php.net/package/redis/2.2.7/windo ...

  4. 如何使用subversion管理iOS源代码

    本文转载至 http://2009315319.blog.51cto.com/701759/819216 使用subversion管理iOS源代码 1.安装和配置subversion服务器 在wind ...

  5. 记CBS一次动人心魄的数据保卫战

    接触分布式存储已经有一年多的时间了,首次遇到存储侧三份数据都有异常的情况,三份数据异常意味着客户数据的丢失,这个对云存储来讲是致命的打击.为了保证数据的安全,CBS运维和开发的同学进行了持续两天一夜的 ...

  6. Pell方程(求形如x*x-d*y*y=1的通解。)

    佩尔方程x*x-d*y*y=1,当d不为完全平方数时,有无数个解,并且知道一个解可以推其他解. 如果d为完全平方数时,可知佩尔方程无解. 假设(x0,y0)是最小正整数解. 则: xn=xn-1*x0 ...

  7. java开发知识体系

  8. 【BZOJ3302】[Shoi2005]树的双中心 DFS

    [BZOJ3302][Shoi2005]树的双中心 Description Input 第一行为N,1<N<=50000,表示树的节点数目,树的节点从1到N编号.接下来N-1行,每行两个整 ...

  9. vscode 全局安装和配置 stylelint 像 webstorm 等 ide 一样来检查项目

    商店里安装完插件以后全局安装或者在项目中(记得加--save-dev)安装配置: npm install stylelint-config-recommended -g 然后在vscode setti ...

  10. openssl将私钥和crt证书合成pfx证书

    pfx是什么文件:公钥加密技术12号标准(Public Key Cryptography Standards #12,PKCS#12)为存储和传输用户或服务器私钥.公钥和证书指定了一个可移植的格式.它 ...