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的更多相关文章

  1. 3. Longest Substring Without Repeating Character[M] 最大不重复子串

    题目 Given a string, find the length of the longest substring without repeating characters. Example 1: ...

  2. [刷题] 3 Longest Substring Without Repeating Character

    要求 在一个字符串中寻找没有重复字母的最长子串 举例 输入:abcabcbb 输出:abc 细节 字符集?字母?数字+字母?ASCII? 大小写是否敏感? 思路 滑动窗口 如果当前窗口没有重复字母,j ...

  3. Leetcode 解题 Longest Substring without repeating charcater python

    原题: Given a string, find the length of the longest substring without repeating character For example ...

  4. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  5. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  6. No.003:Longest Substring Without Repeating Characters

    问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...

  7. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  8. No.003 Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...

  9. Java [leetcode 3] Longest Substring Without Repeating Characters

    问题描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

随机推荐

  1. rsync的笔记整理

    Rsyncd数据同步工具 1.什么是Rsyncs? Rsync(Remote synchronization)是一款开源的,快速的,多功能的,可实现全量及增量的本地或远程数据同步备份的优秀工具.Rsy ...

  2. class定义类 及 实现继承

    class 定义类 代码如下: class Student { constructor(name) { this.name = name; } sayHello() { console.log(&qu ...

  3. 《吊打面试官》系列-Redis基础

    你知道的越多,你不知道的越多 点赞再看,养成习惯 前言 Redis在互联网技术存储方面使用如此广泛,几乎所有的后端技术面试官都要在Redis的使用和原理方面对小伙伴们进行360°的刁难.作为一个在互联 ...

  4. Unity调用Android相册

    最近有一个项目有这个需求,让用户上传自己的交易凭证的截图,之前因为对调Android原生的东西不太熟悉,就先放了一边 因为项目已经上线,只不过是该功能未开放而已,那么现在为什么要写这篇博客呢,是因为. ...

  5. MIT线性代数:5.转置,置换,向量空间

  6. [2018-06-28] django项目 实例

    实例一.显示一个基本的字符串在网页中 首先先进入views.py def home(request): string = u'随便写' return render(request, 'home.htm ...

  7. 安装ubuntu16虚拟机,下载android源码,配置编译环境

    Android 源码编译步骤: 我考虑了一下,目前电脑装了SSD,8G内存,使用虚拟机编译源码应该够用. 首先下载虚拟机软件,由于最近一直在使用virtualbox,感觉蛮不错了,下载地址: http ...

  8. 「刷题」xor

    说实话这道题没有A掉,不过所有的思路都是我自己想的,我觉得这个思路真的很棒很棒很棒的. 首先这个题的题面描述告诉我这种运算有封闭性,满足结合律和交换率,那么其实这个东西是个群运算了,而且这个群有单位元 ...

  9. 来,我们手写一个简易版的mock.js吧(模拟fetch && Ajax请求)

    预期的mock的使用方式 首先我们从使用的角度出发,思考编码过程 M1. 通过配置文件配置url和response M2. 自动检测环境为开发环境时启动Mock.js M3. mock代码能直接覆盖g ...

  10. Java基础系列5:Java代码的执行顺序

    该系列博文会告诉你如何从入门到进阶,一步步地学习Java基础知识,并上手进行实战,接着了解每个Java知识点背后的实现原理,更完整地了解整个Java技术体系,形成自己的知识框架. 一.构造方法 构造方 ...