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

    Solution:

    public class Solution {
        public int lengthOfLongestSubstring(String s) {
            int maxLen = 0;
            if(s.length() == 1) return 1;
            else {
                int l = 0; int r = 0;
                int[] set = new int[256];
                Arrays.fill(set,-1);
                for(int i = 0; i < s.length(); i++){
                    int len = 0;
                    if(-1 == set[s.charAt(i)] || set[s.charAt(i)] < l){
                        r = i;
                        len = r -l+1;
                        set[s.charAt(i)] = i;
                    }else {
                        l = set[s.charAt(i)] + 1;
                        r = i;
                        set[s.charAt(i)] = i;
                    }
                    maxLen = Math.max(maxLen,len);
                }
                return maxLen;
            }
        }
    }
  • 相关阅读:
    SQL结构化查询语言
    数据库主外键
    SQL数据库数据类型详解
    注释和特殊符号
    文本装饰
    列表样式
    网页背景
    SQL数据库数据类型详解
    数据库主外键
    Update 语句
  • 原文地址:https://www.cnblogs.com/yeek/p/3823437.html
Copyright © 2011-2022 走看看