my exhaustion time limit exceed


  • 0
    S

    The dumbest method..

    class Solution {
    public:
        int countBinarySubstrings(string s) {
           int cout = 0;
    	if (s.size() < 2)
    		return 0;
    	if (s.size() == 2){
    		if (s[1] == s[0])
    		{
    			return 0;
    		}
    		else
    		return 1;
    	}
    	for (int i = 2; i <= s.size()/2; i+=2)
    	{
    		string part1(i / 2, '1');
    		string part0(i/2,'0');
    		string part = part1 + part0;
    		string partr = part0 + part1;
    
    		for (int j = 0; j <= s.size() - i; ++j){
    			if (!strncmp(s.c_str() + j, part.c_str(), i) || !strncmp(s.c_str() + j, partr.c_str(), i)){
    				++cout;
    			}
    		}
    	}
    	std::cout << "-------"<< cout << endl;
    	for (int i = (s.size()/2)%2>0?s.size()/2 +1:s.size()/2 + 2; i<=s.size(); i+=2)
    	{
    		
    		string part1(i/2,'1');
    		string part0(i / 2, '0');
    		string part = part1 + part0;
    		string partr = part0 + part1;
    		for (int j = 0; j <= s.size() - i; ++j){
    			if (!strncmp(s.c_str() + j, part.c_str(), i) || !strncmp(s.c_str() + j, partr.c_str(), i)){
    				++cout;
    			}
    		}
    	}
    
    
            return cout;
        }
    };
    

Log in to reply
 

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