题目描述:

把字符串转化为整数值

原文描述:

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)的更多相关文章

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

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

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

    Problem: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible inp ...

  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. [算法练习]String to Integer (atoi)

    题目说明: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

  5. LeetCode(8)String to Integer (atoi)

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

  6. 【leetcode❤python】 8. String to Integer (atoi)

    #-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489' ...

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

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

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

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

  9. 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 ...

随机推荐

  1. JavaScript 比较和逻辑运算符

    比较和逻辑运算符用于测试 true 或者 false. 比较运算符 比较运算符在逻辑语句中使用,以测定变量或值是否相等. 给定 x=5,下面的表格解释了比较运算符: 实例 »实例 » 大于 大于或等于 ...

  2. SpringMVC之Ajax与Controller交互

    前面学习了拦截器,通过拦截器我们可以拦截请求,做进一步处理之后再往下进行,这里我们使用Ajax的时候会有一个问题就是会把js.css这些静态资源文件也进行了拦截,这样在jsp中就无法引入的静态资源文件 ...

  3. 解放双手——Android的自动化构建及发布

    在一个App从开发到测试的过程中,我有很长一段时间都是这样做的:打包,上传到tower,在tower上编写本次更新说明,通知测试.一般情况下,打包及上传的过程大概也就2分钟.除此之外,由于项目代码有作 ...

  4. 操作系统内核Hack:(二)底层编程基础

    操作系统内核Hack:(二)底层编程基础 在<操作系统内核Hack:(一)实验环境搭建>中,我们看到了一个迷你操作系统引导程序.尽管只有不到二十行,然而要完全看懂还是需要不少底层软硬件知识 ...

  5. Android开发学习之路--性能优化之布局优化

      Android性能优化方面也有很多文章了,这里就做一个总结,从原理到方法,工具等做一个简单的了解,从而可以慢慢地改变编码风格,从而提高性能. 一.Android系统是如何处理UI组件的更新操作的 ...

  6. Android app内存管理的16点建议

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiopshared memory(共享内存) Android通过下面几个方式在不同的Process中来共享RAM: 每一个app的proc ...

  7. TCP协议的性能评测工具 — Tcpdive开源啦

    Github地址:https://github.com/fastos/tcpdive 为什么要开发Tcpdive 在过去的几年里,随着移动互联网的飞速发展,整个基础网络已经发生了翻天覆地的变化. 用户 ...

  8. Android 5.0新控件——TextInputLayout

    Android 5.0(M)新控件--TextInputLayout 介绍之前,先直观的看一下效果 TextInputLayout其实是一个容器,他继承自LinearLayout,该容器是作用于Tex ...

  9. iOS下JS与OC互相调用(七)--Cordova 基础

    Cordova 简介 在介绍Cordova之前,必须先提一下PhoneGap.PhoneGap 是Nitobi软件公司2008年推出的一个框架,旨在弥补web 和iOS 之间的不足,使得web 和 i ...

  10. [Centos]openvpn 服务端的安装(easy-rsa3)

    VPN在办公和fan墙领域有着广泛的应用,  我们小办公网最近可能会用到,先学学来着 vpn的server需要有公网ip,客户端可以在多种环境下使用 概念 PKI:Public Key Infrast ...