博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
19.Longest Substring Without Repeating Characters(长度最长的不重复子串)
阅读量:4558 次
发布时间:2019-06-08

本文共 1197 字,大约阅读时间需要 3 分钟。

Level:

  Medium

题目描述:

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

Example 1:

Input: "abcabcbb"Output: 3 Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"Output: 3Explanation: The answer is "wke", with the length of 3.              Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

思路分析:

   给定一个字符串,找到其长度最长的不重复子串,用滑动窗口的思想,设置窗口start,应尽量使窗口变大,从第一个字符开始遍历,遍历时,如果字符未出现过,在map中给出该字符和下标的映射,窗口的大小始终为start到i,如果遍历到某个字符在map中已经有它的映射,那么start的值发生变化,更新为这个字符所对应的map中的下标,即左窗口向右滑动。

代码:

public class Solution{    public int lengthOfLongestSubstring(String s){        if(s==null||s.length()==0)            return 0;        int []map=new int [256];        int start=0;        int len=0;        for(int i=0;i
start) //大于start的原因是abba这种情况,当b重复时start更新为2,如果这里不是大于start而是大于0,那么访问a的时候会认为a重复,所以必须是大于start。 start=map[s.charAt(i)]; map[s.charAt(i)]=i+1; len=Math.max(len,(i+1-start)); } return len; }}

转载于:https://www.cnblogs.com/yjxyy/p/10727910.html

你可能感兴趣的文章
Android系统Intent中的Uri使用
查看>>
事件模拟场景经典例子
查看>>
洛谷—— P1407 工资
查看>>
洛谷——P3128 [USACO15DEC]最大流Max Flow
查看>>
洛谷—— P1190 接水问题
查看>>
Windows Phone开发之Coding4Fun对话框操作类
查看>>
现代3D图形编程学习-关于本书
查看>>
linux系统下php安装mbstring扩展的二种方法
查看>>
gridview分頁:第一頁 下一頁 1 2 3 4 上一頁 最末頁
查看>>
Hadoop-MR实现日志清洗(一)
查看>>
Bootstrap 3之美01-下载并引入页面
查看>>
在Brackets中使用Emmet
查看>>
lodash用法系列(5),链式
查看>>
ASP.NET Web API的安全管道
查看>>
推荐一个好用的 sqlite 管理器 sqliteman 感觉比 navicat 好用
查看>>
第三周学习进度报告
查看>>
使用JSON Web Tokens和Spring实现微服务
查看>>
JS学习笔记 - 运动 - 淘宝轮播图
查看>>
之字形打印矩阵
查看>>
POJ 1004 Financial Management
查看>>