Valid Number 判断一个string是不是有效地数字表达式 @LeetCode

148 篇文章 69 订阅
27 篇文章 0 订阅

题目:

判断一个string是不是有效地数字表达式

思路:

繁琐,先移除前导零和后缀零,然后记录点和e的位置。

再分情况讨论。


这道题看了看网上的解法,很多都做得很麻烦。其实我觉得,这道题就是考察正则表达式。


package Level2;

/**
 * Valid Number
 * 
 *  Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.


 *
 */
public class S65 {

	public static void main(String[] args) {

	}

	public boolean isNumber(String s) {
        if(s.trim().isEmpty()){
        	return false;
        }
        String regex = "[-+]?(\\d+\\.?|\\.\\d+)\\d*(e[-+]?\\d+)?";
        if(s.trim().matches(regex)){
        	return true;
        }else{
        	return false;
        }
    }
}

推荐一些较好的教程:

http://developer.51cto.com/art/200912/166310.htm

http://luolei.org/2013/09/regula-expression-simple-tutorial/

http://net.tutsplus.com/tutorials/php/regular-expressions-for-dummies-screencast-series/

http://deerchao.net/tutorials/regex/regex.htm


Second try:

参考http://blog.csdn.net/doc_sgl/article/details/13505069

面试时如果只用正则表达式估计不行,所以写了下面的方法,其实还算比较直观,把几种情况分出来也就好写了!


public static boolean isNumber(String s) {
		int i = 0;
		while(s.charAt(i) == ' '){	// 移除前导whitespace
			i++;
			if(i >= s.length()){
				return false;
			}
		}
		if(s.charAt(i)=='+' || s.charAt(i)=='-'){	// 忽略符号位
			i++;
		}
		int j = s.length()-1;
		while(s.charAt(j) == ' '){	// 移除后缀whitespace
			j--;
		}
		if(i <= j){
			s = s.substring(i, j+1);
		}else{
			return false;
		}
		
		int dot = -1;	// 记录点的位置
		int ee = -1;	// 记录e的位置
		for(i=0; i<s.length(); i++){
			if(dot==-1 && s.charAt(i)=='.'){
				dot = i;
			}else if(ee==-1 && s.charAt(i)=='e'){
				ee = i;
				if(i+1<s.length() && (s.charAt(i+1)=='-' || s.charAt(i+1)=='+')){
					i++;
				}
			}else{
				if(Character.isDigit(s.charAt(i))){
					continue;
				}else{
					return false;
				}
			}
		}
		
		//xxx.xxexx
		String startStr, midStr, lastStr;
		if(dot==-1 && ee==-1){	//xxx  
			startStr = s;	// xxx
			if(startStr.length()<1){
				return false;
			}
		}else if(dot!=-1 && ee==-1){	//xxx.yyy  
			startStr = s.substring(0, dot);	// xxx
			midStr = s.substring(dot+1);		// yyy
			if(startStr.length()<1 && midStr.length()<1){
				return false;
			}
		}else if(dot==-1 && ee!=-1){	// xxxeyyy
			startStr = s.substring(0, ee);	// xxx
			if(startStr.length()<1){
				return false;
			}
			if(ee+1<s.length() && (s.charAt(ee+1)=='-' || s.charAt(ee+1)=='+')){	// xxxe-zz
				lastStr = s.substring(ee+2);	// zz
			}else{
				lastStr = s.substring(ee+1);
			}
			if(lastStr.length() < 1){
				return false;
			}
		}else{		//xxx.yyezz
			if(dot>ee){		// 位置不对
				return false;
			}
			startStr = s.substring(0, dot);	// xxx
			midStr = s.substring(dot+1, ee);	// yy
			if(startStr.length()<1 && midStr.length()<1){
				return false;
			}
			if(ee+1<s.length() && (s.charAt(ee+1)=='-' || s.charAt(ee+1)=='+')){
				lastStr = s.substring(ee+2);	// zz
			}else{
				lastStr = s.substring(ee+1);
			}
			if(lastStr.length() < 1){
				return false;
			}
		}
		return true;
	}


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值