My C++ code takes 22ms ,no space O(n) ! Sharing !


  • 0
    T

    I saw chammika's algorithm, I am vary exciting .I think mybe no arrary[256] , so I write the following code ! also 22ms .now sharing !

    /zcx myself
    class Solution {
    public:
    	int lengthOfLongestSubstring(string s)
    	{
    	    if (s.length()==0)
    		{
    			return 0;
    		}
    		int t_nMaxLength=1,t_nLength=1;
    		int t_nRepeatFlag=0;
    		int t_nNoReaptLength=1;
    		for (int i=0;i<s.length();i++)
    		{
    			int j;
    			for (j=i+1+t_nNoReaptLength-1;j<s.length();++j)
    			{
    				t_nNoReaptLength=0;
    				for (int k=i;k<j;++k)
    				{
    					if (s[k]==s[j])
    					{
    						i=k;
    						t_nRepeatFlag=1;
    						t_nNoReaptLength=j-k;
    						break;
    					}
    				}
    				if (t_nRepeatFlag)
    				{
    					t_nLength=t_nNoReaptLength;
    					t_nRepeatFlag=0;
    					break;
    				}
    				else
    				{
    					t_nLength++;
    					if (t_nLength>t_nMaxLength)
    					{
    						t_nMaxLength=t_nLength;
    					}
    				}
    			}
    			if (j==s.length())
    			{
    				break;
    			}
    		}
    		return t_nMaxLength;
    	}
    };

Log in to reply
 

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