题目描述:

把字符串转化为整数值

原文描述:

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. 关于Linux下软件包aptitude的相关操作

    aptitude+回车 - 进入aptitude操作界面,可以对预览查看各种软件包 aptitude show package_name - 列出与XXX相关的软件包信息,但是并不能看到该软件包所安装 ...

  2. 优先使用TimeUnit类中的sleep()

    TimeUnit是什么? TimeUnit是java.util.concurrent包下面的一个类,TimeUnit提供了可读性更好的线程暂停操作,通常用来替换Thread.sleep(),在很长一段 ...

  3. Gi之(二)基础命令

    三个工作区域 使用Git之前,首先要弄清Git的三个管理区域,有助于理解Git的运行原理,以及每个Git命令对文件造成的影响. 对于任何一个文件,在本地的Git内部都有三种状态: l   已修改(mo ...

  4. Git 处理tag和branch的命令

    最近想给GitHub 上的项目设置tag,可是使用GitHub Desktop,找了一圈都没找到快速设置Tag 的地方,最后只能通过终端命令来添加了. 想要查看Git 的命令,可以使用 git --h ...

  5. ROS(indigo)RRT路径规划

    源码地址:https://github.com/nalin1096/path_planning 路径规划 使用ROS实现了基于RRT路径规划算法. 发行版 - indigo 算法在有一个障碍的环境找到 ...

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

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

  7. FORM内置系统函数

    abort_query;                                                    停止查询的执行 add_group_column(record grou ...

  8. SQLite 创建表(http://www.w3cschool.cc/sqlite/sqlite-create-table.html)

    SQLite 创建表 SQLite 的 CREATE TABLE 语句用于在任何给定的数据库创建一个新表.创建基本表,涉及到命名表.定义列及每一列的数据类型. 语法 CREATE TABLE 语句的基 ...

  9. 如何将dtb反编译成dts

    点击打开链接 由于device tree会将一个node的信息分布在各个文件里,查看起来很不方便,比如如下例子,ldb在三个文件中都有配置: imx6qdl-sabresd.dtsi: [plain] ...

  10. UNIX环境高级编程——单实例的守护进程

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h&g ...