[LeetCode][8]String to Integer (atoi)解析与模仿Java源码实现 -Java实现

Q:

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.

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.

A:

以下解法和代码没有借阅以往任何资料,如果有更好的解法请在评论区留言

这题大概意思,就是让我们实现atoi,也就是把一个String转化成数字返回而已,在java中我们直接String.valueOf()就好了。有几个hint说要注意所有的输入的可能性。再往下看有一个Requirements for atoi:

1、可能有空格

2、正负号,而且可能有不止一个正负号

3、空值

4、超出int范围

5、没有显示内容返回0,超出范围显示INT_MAX和INT_MIN

我一看这一题我就笑了。。。不就是昨天我们说的源码吗?昨天我们使用了位运算中间就参考了以前看过的部分源码。只不过是昨天是从integer到string。今天反过来而已,不过今天这要求真够多的。

在java源码中,大致是先验证了第一个字符是不是正负号,不是的话或者只含正负号就跑异常了,然后就开始遍历呀,转化呀。重点就在转化了。我们普通转化可能用switch是吧。但是源码中可不是哟。对于-128到127之间是缓存好的数字,除此之外做了一个stirng到int的转换,在这个int转换中把char直接强转成int然后对unicode做的操作,这个就挖的太深了,我们没有必要深究了,做一些swtich转换即可。

代码如下:

public class StringtoInteger {
	public static void main(String[] args){
		System.out.println(method("   -  94dsad564as"));
	}
	public static int method(String s){
		int i = 0;
		int digit = 0;
		int result = 0;
		char[] cs = s.toCharArray();
		int negative = 0;//0未赋值,1负值,2正值
		while(i<cs.length){

			if(cs[i]=='+')//正号
			{
				if(negative != 0){
					return 0;
				}
				negative = 2;
			}else if (cs[i]=='-') {//负号
				if(negative != 0){
					return 0;
				}
				negative = 1;
			}
			else if (cs[i]>=48&&cs[i]<=57) {//数字
				digit = (int)cs[i]-48;
				result = (result<<3)+(result<<1)+digit;//*10;

			}
			i++;
		}
		return (negative==1)?-result:result;
	}
}

虽然算法不如前几次那么奇葩,但是还是有点意思的

时间: 2024-10-01 11:54:02

[LeetCode][8]String to Integer (atoi)解析与模仿Java源码实现 -Java实现的相关文章

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

Leetcode 数 String to Integer (atoi)

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie String to Integer (atoi) Total Accepted: 9862 Total Submissions: 67880 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,

LeetCode 008 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 b

【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 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 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 inte

[LeetCode][JavaScript]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) 字符串

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 intende

[LeetCode][Algorithms]String to Integer (atoi)

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/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

【LeetCode】String to Integer (atoi) 解题报告

这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] 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 pos