Java Solution (5 ms)


  • 0
    D
    class Solution {
        public List<String> removeComments(String[] source) {
            
            List<String> res = new ArrayList<String>();
            
            boolean block = false;
            boolean line = false;
            String op = "" ; 
            
            for ( int i=0;i<source.length;i++)
            {
                String ip = source[i] ;
                char[] c = ip.toCharArray() ;
                line = false ;
                
                if ( !block )
                    op = "" ; 
                
              for ( int j = 0; j<ip.length();j++  )
               {
                  
                 if ( !block && !line && c[j] == '/' )
                   {
                      if ( j+1 <ip.length()  && c[j+1] == '/'   )
                       {
                             line = true;
                             j++ ;
                             break;
                              
                       }
                       else   if (  j+1 <ip.length()  && c[j+1] == '*' )
                       {
                           block = true;
                           j++ ;
                           continue;
                       }
                       
                   }
                  
                    if ( !block && !line)
                   {
                        op += c[j] ;
                    
                   }
                   else if ( (block  ) && c[j] == '*' )
                   {
                       if (   j+1 <ip.length()  && c[j+1] == '/' )
                       {
                           block = false;
                           j++ ;
                       }
                       
                       
                   }
                  
                   
               }
                
                if ( op.length() > 0 && !block)
                  res.add(op);
                
                
            }
            
            
            
            
            return res;
        }
    }
    

Log in to reply
 

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