zoukankan      html  css  js  c++  java
  • *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.

    HashSet:

    set.contains()

    set.remove()

    set.add()

     1 public class Solution {
     2     public int lengthOfLongestSubstring(String s) 
     3     {
     4         if(s==null||s.length()==0) return 0;
     5         HashSet<Character> set = new HashSet<Character>();
     6         
     7         int leftbound=0;
     8         int maxlen=0;
     9         for(int i=0;i<s.length();i++)
    10         {
    11             
    12             if(set.contains(s.charAt(i)))
    13             {
    14                 while(leftbound<i&&s.charAt(leftbound)!=s.charAt(i))
    15                 {
    16                     set.remove(s.charAt(leftbound));
    17                     leftbound++;
    18                 }
    19                 leftbound++;
    20             }
    21             else
    22             {
    23                 set.add(s.charAt(i));
    24                 maxlen=Math.max(maxlen,i-leftbound+1);
    25             }
    26             
    27         
    28         }
    29         return maxlen;
    30 
    31         
    32     }
    33 }
  • 相关阅读:
    CSS的扩展less和sass
    html5小游戏基础知识
    htm5拖放和画布
    htm5
    并查集模板
    二叉树的建树
    kmp的书写
    贪心算法
    容器
    POJ2442 优先队列
  • 原文地址:https://www.cnblogs.com/hygeia/p/4859944.html
Copyright © 2011-2022 走看看