leetcode-剑指20-OK

// language C with STL(C++)
// 剑指20
// https://leetcode-cn.com/problems/biao-shi-shu-zhi-de-zi-fu-chuan-lcof/
// 好恶心的题

class Solution {
public:
	bool isAnum(char a){
		if(a == '0')
			return true;
		if(a == '1')
			return true;
		if(a == '2')
			return true;
		if(a == '3')
			return true;
		if(a == '4')
			return true;
		if(a == '5')
			return true;
		if(a == '6')
			return true;
		if(a == '7')
			return true;
		if(a == '8')
			return true;
		if(a == '9')
			return true;
		return false;
	}


    bool isNumber(string s) {	// 有e有点,有e无点,无e有点,无e无点
    	int firstnotspace = 0;
    	int len = s.length();
    	while(s[firstnotspace] ==' ' && firstnotspace<len-1){
    		firstnotspace++;
    	}
    	int lastnotspace = len-1;
        // printf("%d-", lastnotspace);
    	while(s[lastnotspace] ==' '&& lastnotspace>0)
    		lastnotspace --;
    	if(s[lastnotspace] ==' ')
    		return false;
    	len = len-(firstnotspace)-(len-1-lastnotspace);
        // printf("%d-", firstnotspace);
        // printf("%d-", lastnotspace);
    	if(len <=0)
    		return false;
    	bool eflag=false;
    	bool pointflag = false;
    	bool numflag = false;
    	for(int i = firstnotspace; i<= lastnotspace; i++){
    		if(isAnum(s[i])){
    			numflag =true;
    		}else if((s[i] =='e') ||(s[i] =='E')){
    			if(numflag && !eflag){
    				eflag = true;
    				numflag = false;
    			}else{
    				// printf("%d-", i);
    				return false;
    			}
    		}else if(s[i] == '.'){
    			if(eflag||pointflag){
    				// printf("%d-", i);
    				return false;
    			}else{
    				pointflag = true;
    			}
    		}else if(((s[i] == '+')||(s[i] == '-')) &&( (i ==firstnotspace)|| (s[i-1] =='e')||(s[i-1] =='E'))){

    		}else{
    			// printf("%d-", i);
    			return false;
    		}
    	}
    	return numflag;
    }
};
posted @ 2021-02-08 05:55  RougeBW  阅读(57)  评论(0编辑  收藏  举报