zoukankan      html  css  js  c++  java
  • 3. Longest Substring Without Repeating Characters 最长无重复字符子串Medium

    Examples:

    Description: 

      Given a string, find the length of the longest substring without repeating characters.

    Example:

      Given "abcabcbb", the answer is "abc", which the length is 3.

      Given "bbbbb", the answer is "b", with the length of 1.

      Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

     

    思路分析:

      1.第一想法:从第一个字符起,逐个计算出最长字串长度,然后到遍历到最后一个字符,得到最大值;

      2.其中逐个计算最长字串部分,跟之前的1.Tow Num 问题几乎一样;

      3.于是可以开始编码了........

       一般测试可以通过,但是,当输入字符串特别长的时候会超时,提交失败...看来的重新想解决的方法了  


    C#代码:

     1 public class Solution {
     2     public int LengthOfLongestSubstring(string s) {
     3         int max = 0;
     4         char[] sArr = s.ToCharArray();
     5         for(int i=0;i<s.Length;i++){
     6             int tempMax=1;
     7             int j =i+1;
     8             Hashtable ht = new Hashtable();
     9             ht.Add(i,sArr[i]);
    10             while(j<s.Length&&(!ht.ContainsValue(sArr[j]))){
    11                 ht.Add(j,sArr[j]);
    12                 j++;
    13                 tempMax++;
    14             }
    15 
    16             if(max<tempMax){
    17                 max = tempMax;
    18             }
    19         }
    20         return max;
    21     }
    22 }
  • 相关阅读:
    baomi
    保密|原创解决您的后顾之忧
    为什么选择我们
    c++实现平面上的形状编辑
    完美售后提供完善修改服务
    冰山理论
    边集数组
    图的存储结构(十字链表、邻接多重表、边集数组) 数据结构和算法58
    邻接多重表
    邻接多重表
  • 原文地址:https://www.cnblogs.com/codingHeart/p/6545837.html
Copyright © 2011-2022 走看看