zoukankan      html  css  js  c++  java
  • [LeetCode]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters

     Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

    用一个256的数组记录每个char出现的位置,初始化为-1,当其不是-1时,说明该字符存在。

    在一些例子上找规律,比如‘dvdf’为3,‘abb’为2,‘aab’为2,'aa'为1等。

    找到每个子字符串大小的规律即可。

     1 class Solution {
     2 public:
     3     int lengthOfLongestSubstring(string s) {
     4         int result=0;
     5         int position[256];
     6         fill(position,position+256,-1);
     7         int len=0;
     8         for(int i=0;i<s.length();i++)
     9         {
    10             if(position[s[i]]>-1)
    11             {
    12                 result = max(len,result);
    13                 len=0;
    14                 i=position[s[i]]+1;
    15                 fill(position,position+256,-1);
    16             }
    17             len++;
    18             position[s[i]]=i;
    19         }
    20         result = max(len,result);
    21         return result;
    22     }
    23 };
  • 相关阅读:
    有个名字叫随便乱记——css3
    CSS3伸缩布局
    路政整理
    GIst
    SVN回滚版本
    你需要知道的CSS3 动画技术
    iScroll框架的使用和修改
    CSS3阴影 box-shadow的使用和技巧总结
    Javascript异步编程的4种方法
    zepto学习零碎
  • 原文地址:https://www.cnblogs.com/Sean-le/p/4755753.html
Copyright © 2011-2022 走看看