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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts aconst char * argument, please click the reload buttonto reset your code definition.

spoilers alert... click to show requirements for atoi.

Subscribe to see which companies asked this question

分为以下几种情况:

第一种:输入,str="1234" ,输出, 1234

第二种:输入,str="+1234",输出, 1234

             输入,str="++1234" ,输出,0;

             输入,str="+-1234",输出,0;

             输入,str="-1234",输出, -1234

第三种:输入,str="a1234",输出,0 ; 

              输入,str="1a234",输出,1 ;

              输入,str="12a34" ,输出,12;

              输入,str="12#a34", 输出,12;

              输入,str="#1234" ,输出, 0;

              即输出字符前的数字;

第四种:前面含有空格或末尾含有空格,输入str="  1234" ,输出,1234;

                                                                 输入str="1234  ",输出,1234;

第五种情况:当string转换成整形时超出int的范围:str="999999999"  输出,2147483647

......................

package leetcode;

/**
 * -号对应45 +号对应43
 * 
 * @author Mouse
 *
 */

public class Solution {
	public static int myAtoi(String str) {
		// 1 、去掉首末的空格
		String s = str.trim();
		// 2 、处理字符串为空
		if (s.length() == 0) {
			return 0;
		}

		// 带符号的情况判断
		// 有符号,但只有一位 如s="-"
		if ((int) s.charAt(0) <= 47 || (int) s.charAt(0) >= 58) {// 第一个是非数字
			if (s.length() == 1) {// 没有数字
				return 0;
			}
		}

		// 处理负号和正号
		boolean flag = false;
		if ((int) s.charAt(0) == 45 || (int) s.charAt(0) == 43) {// 第一位是符号
			// 从第一位往后再看一位
			for (int i = 1; i < s.length(); i++) {// ----------------
				// 不是数字
				if (i == 1
						&& ((int) s.charAt(i) <= 47 || (int) s.charAt(i) >= 58))
					return 0; // 如-a

				// 程序要是进行到这一步,说明第二位不是符号
				if ((int) s.charAt(i) <= 47 || (int) s.charAt(i) >= 58) {
					flag = true;
					// 首次检测到符号
					
					String temp = s.substring(0, i);
					System.out.println("temp--->"+temp);
					Long num = Long.parseLong(temp);
					Long MaxInt = (long) Integer.MAX_VALUE;
					Long MinInt = (long) Integer.MIN_VALUE;
					if (num > MaxInt) {
						return Integer.MAX_VALUE;
					} else if (num < MinInt) {
						return Integer.MIN_VALUE;
					} else {
						return Integer.valueOf(temp);
					}
				}
				if (flag) {
					break;
				}

			}// end for--------------------

			// 要是有符号上面一定可以拦截的,若能到达这一步,说明符号后全是数字
			String temp = s.substring(0, s.length());
			if (temp.length()>18&&(int) s.charAt(0) == 45) {
				return Integer.MIN_VALUE;
			}else if(temp.length()>18&&(int) s.charAt(0) == 43){
				return Integer.MAX_VALUE;
			}
			Long num = Long.parseLong(temp);
			Long MaxInt = (long) Integer.MAX_VALUE;
			Long MinInt = (long) Integer.MIN_VALUE;
			if (num > MaxInt) {
				return Integer.MAX_VALUE;
			} else if (num < MinInt) {
				return Integer.MIN_VALUE;
			} else {
				return Integer.valueOf(temp);
			}

		} else {// 处理第一位没有符号的 可能有字母

			for (int i = 0; i < s.length(); i++) {
				if (i == 0
						&& ((int) s.charAt(i) <= 47 || (int) s.charAt(i) >= 58))
					return 0; // 如a

				if (s.length() == 1) {
					return Integer.valueOf(s);
				}

				if ((int) s.charAt(i) <= 47 || (int) s.charAt(i) >= 58) {
					// 首次检测到符号
					String temp = s.substring(0, i);
					Long num = Long.parseLong(temp);
					Long MaxInt = (long) Integer.MAX_VALUE;
					Long MinInt = (long) Integer.MIN_VALUE;
					if (num > MaxInt) {
						return Integer.MAX_VALUE;
					} else if (num < MinInt) {
						return Integer.MIN_VALUE;
					} else {
						return Integer.valueOf(temp);
					}
				}
				if (flag) {
					break;
				}
			}// end for-------------------

			// 若能到达这一步,说明符号后全是数字
			String temp = s.substring(0, s.length());
			if (temp.length()>18) {
				return Integer.MAX_VALUE;
			}
			Long num = Long.parseLong(temp);
			Long MaxInt = (long) Integer.MAX_VALUE;
			Long MinInt = (long) Integer.MIN_VALUE;
			if (num > MaxInt) {
				return Integer.MAX_VALUE;
			} else if (num < MinInt) {
				return Integer.MIN_VALUE;
			} else {
				return Integer.valueOf(temp);
			}
		}

	}

	public static void main(String[] args) {
		int a = myAtoi("1234567890123456789012345678901234567890");
		System.out.println(a);

	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值