zoukankan      html  css  js  c++  java
  • LeetCode OJ-- Valid Number **@

    https://oj.leetcode.com/problems/valid-number/

    判断给的串,是不是合理的 数字形式

    主要问题在需求定义上吧

    class Solution {
    public:
        bool isNumber(const char *s) {
            if(s == NULL)
                return false;
            
            int index = 0;
            // remove heading spaces
            while(s[index] != '' && s[index] == ' ')
                index++;
            
            // only has spaces
            if(s[index] == '')
                return false;
            
            // check + or - allowed
            if(s[index] == '+' || s[index] == '-')
                index++;
                
            // remove tailing spaces
            bool hasSpace = false;
            int tailIndex = 0;
            for(int i = index; s[i] != ''; i++)
            {
                if(s[i] == ' ')
                {
                    if(hasSpace == false)
                        tailIndex = i - 1;
                    hasSpace = true;
                    continue;
                }
                else if(hasSpace && s[i] != ' ')
                    return false;
            
                if(hasSpace == false)
                    tailIndex = i;
            }
            
            
            // check only one . and e or digits allowed 
         // . e can't both exists. and 8. is valid
         // before e and after e must has digits
         // + - before them must be e
    bool hasNum = false; bool hasDot = false; bool hasE = false; for(int i = index; i != tailIndex + 1 && s[i] != ''; i++) { if(s[i] >= '0' && s[i] <= '9') hasNum = true; else if(s[i] == '.') { if(hasDot || hasE) return false; hasDot = true; } else if(s[i] == 'e') { if(hasE || hasNum == false) return false; hasE = true; hasNum = false; } else if(s[i] == '+' || s[i] == '-') { if(!(i > 1 && s[i-1] == 'e')) return false; hasNum = false; } else return false; } return hasNum; } };
  • 相关阅读:
    Django —— DateTimeField格式
    Django——权限组件(中间件判断用户权限--URL初级)
    linux命令
    性能测试--测试分类
    web安全之csrf攻击
    web安全之xss攻击
    测试用例规范
    禅道操作手册
    fiddler弱网测试
    Web测试系列之测试工具
  • 原文地址:https://www.cnblogs.com/qingcheng/p/3918231.html
Copyright © 2011-2022 走看看