length of the longest substring without repeating character
Given a string, find the length of the longest substring without repeating characters.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这道题目一拿到手,想都不用想,直接暴力遍历。然而结果虽然可行,但是效率太低,最终还有一个案例没通过,晕。还是要多动脑啊
public int lengthOfLongestSubstring(String s) {
        int length = 0;
        if(isUnique(s))
        {
            length = s.length();
        }
        int index = s.length();
        for(int i = 0; i < index; i++)
        {
            for(int j = index; j >= i; j--)
            {
                String str = s.substring(i, j);
                if(isUnique(str) && str.length() > length)
                {
                    //从字符串的后面往前遍历,遇到第一个符合条件的肯定就是最长的之一,因此不用再往前遍历了
                    length = str.length();
                    break;
                }
                //剩下的子字符串的长度小于等于暂时的结果,也无需再往前遍历
                if(j - i <= length)
                {
                    break;
                }
            }
        }
        return length;
    }
    public boolean isUnique(String s)
    {
        Set<Character> set = new HashSet<Character>();
        char[] chars = s.toCharArray();
        for(char c: chars)
        {
            set.add(c);
        }
        return (set.size() == s.length());
    }
无论怎么去优化,最终还是有一个案例没能通过。貌似暴力解法在此题行不通??
看到各种题解都提及到滑动窗口解法,于是去查了一下这个算法的思想,并根据自己的理解去写一下代码,终于通过了,虽然还有很大的提升空间...
    public int lengthOfLongestSubstring(String s) {
        int result = 0;
        int left = 0;   //窗口左边界
        int right = 0; //窗口右边界
        int length = s.length();
        Set<Character> set = new HashSet<Character>();
        while(left < length && right < length)
        {
            int temp = 0;
            if(!set.contains(s.charAt(right)))
            {
                set.add(s.charAt(right));
                right++;
                temp = set.size();
                if(temp > result)
                {
                    result = temp;
                }
            }
            else
            {
                //如果发现set中已经存在此字符,就把左边界右移
                set.remove(s.charAt(left));
                left++;
            }
        }
        return result;
    }
    
length of the longest substring without repeating character的更多相关文章
- 3. Longest Substring Without Repeating Character[M] 最大不重复子串
		题目 Given a string, find the length of the longest substring without repeating characters. Example 1: ... 
- [刷题] 3 Longest Substring Without Repeating Character
		要求 在一个字符串中寻找没有重复字母的最长子串 举例 输入:abcabcbb 输出:abc 细节 字符集?字母?数字+字母?ASCII? 大小写是否敏感? 思路 滑动窗口 如果当前窗口没有重复字母,j ... 
- Leetcode 解题 Longest Substring without repeating charcater python
		原题: Given a string, find the length of the longest substring without repeating character For example ... 
- LeetCode[3] Longest Substring Without Repeating Characters
		题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ... 
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
		Given a string, find the length of the longest substring without repeating characters. For example, ... 
- No.003:Longest Substring Without Repeating Characters
		问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ... 
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
		Given a string, find the length of the longest substring without repeating characters. For example, ... 
- No.003  Longest Substring Without Repeating Characters
		Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ... 
- Java [leetcode 3] Longest Substring Without Repeating Characters
		问题描述: Given a string, find the length of the longest substring without repeating characters. For exa ... 
随机推荐
- Linux杂谈:解决配置静态ip后eth0网卡启动不了的问题
			今天在看imooc上的<Linux网络管理>的课程中,在做一些实验时修改了下网络配置,发现了一些问题,就是保存网络配置后eth0网卡打不开,可能也会有很多人出现这类问题,我就在这里分享下自 ... 
- Mac tensorflow mnist实例
			Mac tensorflow mnist实例 前期主要需要安装好tensorflow的环境,Mac 如果只涉及到CPU的版本,推荐使用pip3,傻瓜式安装,一行命令!代码使用python3. 在此附上 ... 
- linux安装redis及外网访问
			1.下载Redis,最新版是redis-3.2.1.tar.gz 2.上传到Linux上,解压到/usr/local/下面 ,命令:tar -zxvf redis-3.2.1.tar.gz 3.我们 ... 
- 【AtCoder - 5659 】>< (思维题)
			>< 直接写中文了 Problem Statement 给定的是长度为N-1的字符串S. S中的每个字符都是<或>. 当对所有i(1≤i≤N-1)都满足以下条件时,N个非负整数 ... 
- OV5640摄像头配置一些值得注意的关键点(三)
			一.字节标志的注意点 由于摄像头的输出是RGB56格式,所以需要将两帧的数据进行拼接,之后送到上位机进行显示. reg byte_flag; always@(posedge cmos_pclk_i) ... 
- GitHub_Hexo_Next 搭建博客
			利用最新版本的 hexo+next 重构了个人博客,下面简单记录了搭建博客的完整过程: 一.环境准备 1.安装 Node.js 2.安装 Git 3.注册 Github 账号 二.在GitHub上创建 ... 
- 《JS高程》-教你如何写出可维护的代码
			1.前言 在平时工作开发中,大部分开发人员都花费大量的时间在维护其他人员的代码.很难从头开始开发新代码,很多情况下都是以他人成果为基础的,或者新增修改需求,自己写的代码也会被其他开发人员调用,所以 ... 
- linux 设置固定ip和dns
			目录 1. centos 1.1 ifconfig 查看网卡名称 1.2 设置固定ip和dns 1.3 重启网络 2. ubuntu 2.1 ifconfig 查看网卡名称 2.2 设置固定ip和dn ... 
- SQlALchemy session详解
			系列文章: Python SQLAlchemy入门教程 概念 session用于创建程序和数据库之间的会话,所有对象的载入和保存都需通过session对象 . 通过sessionmaker调用创建一个 ... 
- 1005 Spell It Right(20 分)
			1005 Spell It Right(20 分) Given a non-negative integer N, your task is to compute the sum of all the ... 
