博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3. Longest Substring Without Repeating Characters
阅读量:4636 次
发布时间:2019-06-09

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

题目:

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

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", 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.

 

复杂度:

时间:O(n^2)、空间:O(1)

 

实现:

public class Solution {    public int lengthOfLongestSubstring(String s) {        int first = 0, stepMax = 1, max = s.length() > 0 ? 1 : 0;        for(int i = 1; i < s.length(); i++) {            boolean stop = false;            for(int j = first; j < i; j++) {                if(s.charAt(j) == s.charAt(i)) {                    first = j + 1;                    stop = true;                    break;                }            }                        stepMax = stop ? (i - first + 1) : (stepMax + 1);            max = stepMax > max ? stepMax : max;        }                return max;    }}

转载于:https://www.cnblogs.com/YaungOu/p/6718762.html

你可能感兴趣的文章
UNDERSTANDING CALLBACK FUNCTIONS IN JAVASCRIPT
查看>>
2019 Multi-University Training Contest 1 - 1012 - NTT
查看>>
浏览器调试淘宝首页看到有趣的招聘信息
查看>>
ASP.NET Identity “角色-权限”管理 4
查看>>
[转][译]ASP.NET MVC 4 移动特性
查看>>
SOC CPU
查看>>
get_result --perl
查看>>
163镜像地址
查看>>
ehcache memcache redis 三大缓存男高音
查看>>
eclipse 快捷键Open Implementation 直接退出
查看>>
minix中管道文件和设备文件的读写
查看>>
JAXB - Annotations, Annotations for Enums: XmlEnum, XmlEnumValue
查看>>
context 插图
查看>>
文件管理器中不支持的wma歌曲也显示可以播放的音乐图标
查看>>
Java基础学习-流程控制语句
查看>>
Shell中read的常用方式
查看>>
01javascript数据类型
查看>>
asp.net实现md5加密方法详解
查看>>
AJAX
查看>>
table 的thead th 固定 tbody滚动例子
查看>>