LeetCode_String to Integer (atoi)

32 篇文章 0 订阅

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 a const char * argument, please click the reload button  to reset your code definition.

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

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.


题目不难但是很繁琐,要求很多,输入一个字符串,要求输出一个整形数,先弄清楚题目的要求:

1、如果字符串的开头若干个为空格,则忽略空格,比如 “     123”,则输出“123”

2、如果字符串开头的若干个字符除了空格外,还存在其他非整形字符,则输出0。比如 “        ab123”,输出0.

3、如果出现“+”或“-”则应在紧跟着正负号后边出现数字,否则输出0,比如输入“   +123”则输出“123”,若输入“    +    123”,则输出0。

4、如果在合法数字之后出现了非整形字符,则忽略。比如输入“   -123abc456”则输出“-123”

5、限制输入的数字在int型范围之内,java为-2147483648~+2147483647,若小于最小值,则输出-2147483648,若大于最大值,则输出+2147483647


java代码实现:

public static int myAtoi(String str) {
		StringBuffer sb = new StringBuffer();
		int key=0;
		int key2=0;
		int flag=1;
		for(int i=0;i<str.length();i++){
		//	System.out.print(str.charAt(i)+":");
			if(str.charAt(i)==' '&&key==0){
				System.out.println("空格,跳过");
				continue;
			}
			if(str.charAt(i)=='+'&&key==0&&key2==0){
				key2=1;
				key=1;
				continue;
			}
			else if(str.charAt(i)=='-'&&key==0&&key2==0){
				flag=-1;
				key=1;
				key2=1;
				continue;
			}
			else if((!(str.charAt(i)>='0'&&str.charAt(i)<='9'))&&(key==0)){
			//	System.out.println("有非法字符,输出0");
				break;
			}
			if(str.charAt(i)>='0'&&str.charAt(i)<='9'){
			//	System.out.println("正常");
				key=1;
				sb.append(str.charAt(i));
				if(sb.length()>10){
					sb.setLength(0);
					if(flag==1)
						sb.append(Integer.MAX_VALUE);
					else{
						sb.append("2147483648");
					}
					break;
				}
			}
			else{
			//	System.out.println("截止了");
				break;
			}
		
		}
		if(sb.length()==0)
			return 0;
		else{
			String s = sb.toString();
			long res = flag*Long.parseLong(s);
			if(res>=Integer.MAX_VALUE)
				return Integer.MAX_VALUE;
			else if(res<=Integer.MIN_VALUE)
				return Integer.MIN_VALUE;
			else
				return (int) res;
		}

	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值