3. Longest Substring Without Repeating Characters

官方的链接:3. Longest Substring Without Repeating Characters

Description :

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

Example:


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.


问题描述

给定一个字符串,找出最长不重复子串的长度

思路

建立一个128的数组,存储字符的下标,min存储非重复子串的最小下标,max存储最大下标。max逐步加大,如果下一个字符的下标大于min,则增加min,同时记录最大的max - min + 1

[github-here]

 

 public class Q3_LongestSubstringWithoutRepeatingCharacters {
public int lengthOfLongestSubstring(String s) {
if (null == s || s.length() < 1) {
return 0;
}
// 初始化128的数组,足够存储所有字符
int[] intArray = new int[128];
for (int i = 0; i < intArray.length; i++) {
intArray[i] = -1;
}
char num = s.charAt(0);
intArray[num] = 0;
int min = 0;
int max = 0;
int count = 1;
for (int i = 1; i < s.length(); i++) {
num = s.charAt(i);
max = i;
if (intArray[num] == (max - 1)) {
count = max - min > count ? max - min : count;
min = max;
} else if (intArray[num] >= min) {
count = max - intArray[num] > count ? max - intArray[num] : count;
min = intArray[num] + 1;
} else {
count = max - min + 1 > count ? max - min + 1 : count;
}
intArray[num] = i;
}
return count;
} public static void main(String[] args) {
Q3_LongestSubstringWithoutRepeatingCharacters longest = new Q3_LongestSubstringWithoutRepeatingCharacters();
System.out.println(longest.lengthOfLongestSubstring("nfpdmpi"));
}
}

Q3:Longest Substring Without Repeating Characters的更多相关文章

  1. LeetCode3:Longest Substring Without Repeating Characters

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

  2. No.003:Longest Substring Without Repeating Characters

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

  3. Leetcode经典试题:Longest Substring Without Repeating Characters解析

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

  4. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

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

  5. LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

    题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...

  6. leetcode笔记:Longest Substring Without Repeating Characters

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

  7. leetcode:Longest Substring Without Repeating Characters

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

  8. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

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

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

随机推荐

  1. 【LeetCode】排列硬币

    [问题]你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币.给定一个数字 n,找出可形成完整阶梯行的总行数.n 是一个非负整数,并且在32位有符号整型的范围内. [ ...

  2. php绕过open_basedir设置

    原理关于open_basedir    open_basedir是php.ini中的一个配置选项    它可将用户访问文件的活动范围限制在指定的区域,    假设open_basedir=/home/ ...

  3. Ubuntu 1804 进入紧急模式

    进入系统引导前,长按shift键:     选择第一条,按e进入编辑     添加完成后,只需按下 CTRL+x 或 F10 即可继续启动救援模式         接下来,输入以下命令将根 (/) 文 ...

  4. 文件上传报错java.io.FileNotFoundException拒绝访问

    局部代码如下: File tempFile = new File("G:/tempfileDir"+"/"+fileName); if(!tempFile.ex ...

  5. mapper语句的一些问题,union连表查询和mapper中根据条件不同采用不同语句的查询问题

    根据业务要求,不同表查出来的内容天需要一起展示出来,并且还有分页之类的,不同表查询字段也不完全相同,这样就有一个问题,不同表如何接合在一起,不同字段怎么办? 这个问题就需要用到union联合查询,并将 ...

  6. Redis集群环境之linux搭建多机版---已完结,跟着一步一步来你就可以集群成功

    上一篇踩着各种坑写了Redis集群环境之linux搭建单机版,这一篇准备就多机版集群进行搭建,主要目的一来是在上一篇的基础上进行精华提粹总结,二来是把单机版与多机版的区别进行记录. 首先软硬件环境: ...

  7. 快速进阶Vue3.0

    在2019.10.5日发布了Vue3.0预览版源码,但是预计最早需要等到 2020 年第一季度才有可能发布 3.0 正式版. 可以直接看 github源码. 新版Vue 3.0计划并已实现的主要架构改 ...

  8. 091-PHP数组过滤函数array_filter,过滤非整数

    <?php function not_int($x){ //定义过滤非整数的函数 if(is_int($x)) return TRUE; } $arr=array(1,2,'a',3,4,'c' ...

  9. Centos7安装rabbitMQ3.6.0

    文章中的erlang和rabbitmq3.6.0 http://pan.baidu.com/s/1c2Nn64w ​ Centos7 系统操作 cd /etc/yum.repos.d/ mv Cent ...

  10. crontab 实现Linux系统上定时任务的关键命令

    附上原文链接 https://www.cnblogs.com/ftl1012/p/crontab.html 原文真是写得非常好,所谓他山之石可以攻玉~,我就不客气啦,哈哈