C# 182ms two pointers solutions


  • 0
    public class Solution {
        public int LengthOfLongestSubstring(string s) {
            int rsMax=0;
            if(!String.IsNullOrEmpty(s))
            {
                if(s.Length>1)
                {
                    Dictionary<char,int> dtList=new Dictionary<char,int>();
                    for(int i=0,j=0;i<s.Length;i++)
                    {
                        if(dtList.ContainsKey(s[i]))
                        {
                            j=Math.Max(j,dtList[s[i]]+1);
                            dtList[s[i]]=i;
                        }
                        else
                        {
                            dtList.Add(s[i],i);
                        }
                        rsMax=Math.Max(rsMax,i-j+1);
                    }
                }
                else
                {
                    rsMax=1;
                }
            }
            return rsMax;
        }
    }

Log in to reply
 

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