zoukankan      html  css  js  c++  java
  • 【leetcode】剑指 Offer 20. 表示数值的字符串

    bool isNumber(char* s){
        int i, j, pcnt = 0, ecnt = 0, dcnt = 0, scnt=0, len = strlen(s);
        for (i = 0; i<len; i++){
            if (s[i] == '+' || s[i] == '-'){
                if (i != 0 && s[i - 1] != 'e' && s[i - 1] != 'E' && s[i - 1] != ' ')
                    return false;
                else if(i == len - 1 || (isdigit(s[i + 1]) == 0 && s[i + 1] != '.'))
                    return false;
            }        
            else if ( s[i] == '.' ){
                pcnt++;
                if ( pcnt>1 || ecnt > 0)
                    return false;
                else if (i != 0 && s[i - 1] == ' ' && dcnt > 0)
                    return false;
                else if (i == 0 && isdigit(s[i + 1]) == 0)
                    return false;
            }
            else if (s[i]=='e' || s[i]=='E'){
                ecnt++;
                if (i == 0 || ecnt > 1 || i == len - 1)
                    return false;
                else if    (isdigit(s[i + 1]) == 0 && s[i + 1] != '-' && s[i + 1] != '+') 
                    return false;
                else if (isdigit(s[i - 1]) == 0 && s[i - 1] !='.')
                    return false;
            }
            else if (isdigit(s[i])){
                dcnt++;
                if (i != 0 && s[i - 1] == ' ' && (dcnt > 1 || pcnt>0) )
                    return false;
            }
            else if (s[i] == ' '){
                scnt++;
            }
            else
                return false;    
        }
        return (dcnt) ? true : false;
    }
  • 相关阅读:
    URL
    B/S架构
    SQL查询语句
    SQL-Delete语句
    SQL运算符
    SQL结构查询语言
    SQL数据库数据类型详解
    标准文档流
    CSS
    字体样式
  • 原文地址:https://www.cnblogs.com/ganxiang/p/14084171.html
Copyright © 2011-2022 走看看