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. R 正态性检验:正态概率图

    检验模型是否满足正态性假设的方法: 1.正态概率图 这是我编写的画正态概率图的函数: #绘制正态概率图 plot_ZP = function(ti) #输入外部学生化残差 { n = length(t ...

  2. 关于dom树

    当用户访问ip地址,比如 ==www. aa .com/bb/cc/dd.html== 正常情况下是访问一个叫aa.com的服务器里的bb文件夹里的cc文件夹里的dd.html这个文件(其实很多情况都 ...

  3. 008、Java中变量与常量的区别

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  4. spring core:@AliasFor的派生性

    spring对Annotation的派生性应用可谓炉火纯青,在spring core:@Component的派生性讲过支持层次上派生性,而属性上派生的需求则借助了@AliasFor,它是从spring ...

  5. Netty 中队列的使用

    任务队列中的Task有3种典型使用场景 用户程序自定义的普通任务 此前代码: 参考https://www.cnblogs.com/ronnieyuan/p/12016712.html NettySer ...

  6. CentOS 6.5(x86_32)下安装Oracle 10g R2

    一.硬件要求 1.内存 & swap Minimum: 1 GB of RAMRecommended: 2 GB of RAM or more 检查内存情况 # grep MemTotal / ...

  7. 洛谷P1002——过河卒

    又是洛谷题,要不是有小姐姐不会,我才不想动脑子.先贴一下题目地址https://www.luogu.org/problem/P1002 再贴一下题目: 我们读一下题目,这可不比学校的**算法题,读完一 ...

  8. .nerCore-RabbitMQDemo消息队列

    1.定义:MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.MQ是消费- ...

  9. cf 763A. Timofey and a tree

    呵呵呵,直接判断是不是一个点连起来所有的特殊边(连接2不同颜色的点的边) (一开始还想各种各样奇怪的dfs...垃圾) #include<bits/stdc++.h> #define LL ...

  10. STM32中ARM系列编译工具链的编译宏选择(__CC_ARM、__ICCARM__、__GNUC__、__TASKING__)

    一 前言 stm32 f103中.关系到一个选择何种编译宏的问题.这里就梳理一下吧. 二 正文 1  在 core_cm3.h 文件中,有如下代码: #if defined ( __CC_ARM ) ...