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.

    简单来说就是维护一个没有重复字符的窗口,确定起点后先向后拓展终点,直到重复再向后移动起点直到无重复。

    class Solution {
    public:
        bool check(int*a,int n){
            for(int i=0;i<n;i++){
                if(a[i]>1)return false;
            }
            return true;
        }
        int lengthOfLongestSubstring(string s) 
        {
            if(s.length()<=0)return 0;
            int* all=new int[26];
            for(int i=0;i<26;i++)all[i]=0;
            int maxLength=1;
            int start=0;
            int end=0;
            all[s[start]-'a']++;
            
            while(true)
            {
                if(check(all,26))
                {
                    if(end-start+1>maxLength)
                    {
                        maxLength=end-start+1;
                    }   
                    end++;
                    if(end>=s.length())break;
                    all[s[end]-'a']++;
                }
                else
                {
                    all[s[start]-'a']--;
                    start++;
                }
                
            }
            delete all;
            return maxLength;
        }
    };
    
    public class Solution {
      public int lengthOfLongestSubstring(String s) {
            // Note: The Solution object is instantiated only once and is reused by
            // each test case.
            if (s.length() == 0)
                return 0;
            boolean[] count = new boolean[256];
            Arrays.fill(count, false);
            int start = 0, end = 0;
            int maxLen = 0;
            boolean flag = true;
            count[(int) s.charAt(0)] = true;
            end++;
            while (true) {
                if (end < s.length()) {
                    if (count[(int) s.charAt(end)]) {
                        if (end - start > maxLen)
                            maxLen = end - start;
                        while (count[(int) s.charAt(end)]) {
                            count[(int) s.charAt(start)] = false;
                            start++;
                        }
                    } else {
                        count[(int) s.charAt(end)] = true;
                        end++;
                    }
                } else {
                    if (end - start > maxLen)
                        maxLen = end - start;
                    break;
                }
            }
            return maxLen;
        }
    }
    Java
  • 相关阅读:
    [Python学习之路] 猜大小游戏
    C语言学生成绩管理系统(简易版)
    malloc动态分配字符串数组“ 一个月内的提醒”
    结构体指针排序
    C语言结构体排序
    数据测压,数据库查询,修改,添加
    badboy脚本录制
    性能测试
    JDK开发环境搭建及环境变量配置(win10)Jmeter之安装和配置
    Charles 修改返回值response(方法 breakpoints)
  • 原文地址:https://www.cnblogs.com/superzrx/p/3322051.html
Copyright © 2011-2022 走看看