Valid Number - C++


  • 0
    L

    '''
    class Solution {
    public:
    bool isNumber(string s) {
    string rstr=s;
    int lt=s.size();
    int start=0;
    int end = 0;
    int pc = count(s.begin(),s.end(),'-');
    if((pc)>2) return false;
    int ec = count(s.begin(),s.end(),'+');
    if((ec)>2) return false;

        ec=0;
            
        for(int i=0;i<lt;i++)
        {
            if(s[i]=='-'||s[i]=='+') 
            {
                start++;
                ec=1;
            }
            else if(s[i]==' '&&ec==0) 
            {
                start++;
            }
            else if(s[i]==' ')
            {
                return false;
            }
            else 
            {
                break;
            }
        }
         for(int i=s.size()-1;i>-1;i--)
        {
               if(s[i]==' ') 
               {lt--;
               }
                else 
            {break;
            }
        }
        if(start>lt) return false;
         pc = count(s.begin(),s.end(),'.');
        if(pc>1) return false;
         ec = count(s.begin(),s.end(),'e');
        if(ec>1) return false;
        
        
        int pe=s.find('e', 0);
        int pp=s.find('.', 0);
        if(pe<pp&&pe>-1) return false;
        
        for(int i=start;i<lt;i++)
        {            
           
          if(s[i]==' ') 
           {
               return false;
           }
           else if(s[i]=='e')
           {
               if(i==start) 
               {
                   return false;
               }
               else if(s[i-1]=='.'&&i-1==start)
               {
                   return false;
               }
               else if(i==lt-1)
               {
                   return false;
               }
                else if(s[i+1]=='.')
               {
                   return false;
               }
               else if((s[i+1]=='+'||s[i+1]=='-')&&i+2<lt)
               {
                   i++;
               }
               
           }
            else if(s[i]=='.')
           {
               if(lt-start==1) return false;
              
           }
           else if(s[i]>'9'||s[i]<'0')
           {
               return false;
           }
            
        }
        return true;
    }
    

    };
    '''


  • 0
    L

    @LeavyRoll After the coding ,I Found tha,the testcases dont perfect


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.