[leetcode]经典算法题- 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.

思路分析:

  • 首先判断为空,返回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面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!

微信订阅号二维码如下:

时间: 2024-10-08 20:48:41

[leetcode]经典算法题- String to Integer (atoi)的相关文章

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

Problem: 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

(atoi)Leetcode第八题_String to Integer (atoi)

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 f

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

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 f

[算法练习]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

LeetCode【8】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 spe

LeetCode(8)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

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

题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 1 class Solution { 2 fun myAtoi(str: String): Int { 3 val maxInt = "2147483647" 4 val maxIntS = "+2147483647" 5 val minIntS = "-21474836

【leetcode刷题笔记】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 spe

String to Integer (atoi) leetcode java

题目: 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