The solution coded by C#


  • 0
    I

    I find that the number of the solutions coded by C# is very small .So i write it.

    public int LengthOfLongestSubstring(string s) {
         char[] temp = s.ToCharArray();
         int max = 0;
         Dictionary<char,int> collection=new Dictionary<char,int>();
         for (int i = 0, j = 0; i < temp.Length; i++)
         {
         if (collection.ContainsKey(temp[i]))
         {
             j = Math.Max(j, collection[temp[i]] + 1);
             collection.Remove(temp[i]);
            collection.Add(temp[i], i);
         }
         else
         {
             collection.Add(temp[i], i);
         }
        max = Math.Max(max, i - j + 1);
      }
     return max;
    }
    

Log in to reply
 

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