当前位置 : 主页 > 编程语言 > c++ >

【Leetcode】String to Integer

来源:互联网 收集:自由互联 发布时间:2021-06-30
StringtoInteger.java /** * 8. String to Integer (atoi) * @param str * @return * 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
StringtoInteger.java
/**
	 * 8. String to Integer (atoi)
	 * @param str
	 * @return
	 * 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.
	 * There are some problems
	 */
	public static int atoi(String str) {
		str = str.trim();
		if(str.length()==0){
			return 0;
		}
		int base = 1;
		if(str.startsWith("-")){
			base = -1;
			str = str.substring(1);
		}		
		int dev = 1;
		for(int i=1;i
 
   (2<<29)){
				if(base==1){
					return Integer.MAX_VALUE;
				}
				return Integer.MIN_VALUE;
			}
		}
		int result = 0;
		for(int i=0,j=dev;i
 
网友评论