LeetCode8. 字符串转换整数 (atoi)(java)

题目;

请你来实现一个 atoi 函数,使其能将字符串转换成整数。

首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。

当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。

该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。

注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。

在任何情况下,若函数不能进行有效的转换时,请返回 0。

说明:

假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231, 231 − 1]。如果数值超过这个范围,qing返回 INT_MAX (231 − 1) 或 INT_MIN (−231) 。

示例:
在这里插入图片描述在这里插入图片描述
代码:

  • 解法一
class Solution {
public int myAtoi(String str) {
		String s = "";
		//去除字符串前后的空格
		str = str.trim();
		//新的字符串 如果长度为0
		if (str.length() == 0) {
			return 0;
		}
		//新的字符串 如果长度为1
		if (str.length() == 1) {
			if (str.charAt(0) >= '0' && str.charAt(0) <= '9') {
				return Integer.parseInt(str);
			} else {
				return 0;
			}
		}
		//如果新字符串 第一个字符为‘-’
		if (str.charAt(0) == '-') {
			if (str.length() == 2 && str.charAt(1) == '0') {
				return 0;
			}
			if (str.charAt(1) < '0' || str.charAt(1) > '9') {
				return 0;
			} else {
				s = s + str.charAt(0);
				int i;
				if (str.charAt(1) == '0') {
					i = 2;
				} else {
					i = 1;
				}
				for (; i < str.length(); i++) {

					if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
						s = s + str.charAt(i);
					} else {
						break;
					}
				}
				try {
					if (Integer.parseInt(s) <= (int) Math.pow(-2, 31)) {

					} else {

					}

				} catch (NumberFormatException e) {
					if (s.equals("-")) {

						return 0;
					}
					return (int) Math.pow(-2, 31);

				}
				return Integer.parseInt(s);
			}
		}
		//如果新字符串 第一个字符‘+’
		if (str.charAt(0) == '+') {
			if (str.length() == 2 && str.charAt(1) == '0') {
				return 0;
			}
			if (str.charAt(1) < 48 || str.charAt(1) > 57) {
				return 0;
			} else {
				s = s + str.charAt(0);
				int i;
				if (str.charAt(1) == '0') {
					i = 2;
				} else {
					i = 1;
				}
				for (; i < str.length(); i++) {

					if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
						s = s + str.charAt(i);
					} else {
						break;
					}
				}
				try {
					if (Integer.parseInt(s) >= (int) Math.pow(2, 31)) {

					} else {

					}

				} catch (NumberFormatException e) {
					if (s.equals("+")) {

						return 0;
					}
					return (int) Math.pow(2, 31);

				}
				return Integer.parseInt(s);
			}
		}
		//如果新字符串 第一个字符为数字
		if (str.charAt(0) >= 48 && str.charAt(0) <= 57) {
			int i;
			if (str.charAt(0) == 48) {
				i = 1;
			} else {
				i = 0;
			}
			for (; i < str.length(); i++) {
				if (str.charAt(i) >= 48 && str.charAt(i) <= 57) {
					s = s + str.charAt(i);
				} else {
					break;
				}
			}
			try {
				if (Integer.parseInt(s) <= (int) Math.pow(2, 31)) {

				} else {

				}

			} catch (NumberFormatException e) {
				if (s.equals("")) {

					return 0;
				}
				return (int) Math.pow(2, 31);

			}
			return Integer.parseInt(s);
		}
		return 0;
	}

}

  • 别人的代码
class Solution {
    public int myAtoi(String str) {
        if(str.length()==0)
        return 0;	//判断是否为空字符串
        int i=0;	//字符串索引
        int flag=1;//正负判断
        int base=0;//存储转换后数字
       
        while(str.charAt(i)==' '){	//去除空格
            i++;
            if(i==str.length()) //如果全为空格则直接返回,如果不加这个if会导致全为空格时的out of range的错误
                return 0;
        }	
        //第一个字符为正负号时
        if(str.charAt(i)=='-'||str.charAt(i)=='+'){
            flag = str.charAt(i++) == '-' ? -1 : 1;
        }
        while(i<str.length()&&str.charAt(i)>='0'&&str.charAt(i)<='9'){            
                 if (base>Integer.MAX_VALUE/10||(base==Integer.MAX_VALUE/10&&str.charAt(i)-'0'>7)) { 
                     return (flag==1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
                 }
                base=(str.charAt(i++)-'0')+base*10;                               
        }
        return base*flag;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值