【字符串&最长无重复子串】Longest Substring Without Repeating Characters
来源:互联网 发布:淘宝网红直播费用 编辑:程序博客网 时间:2024/05/17 08:40
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
题意:求出最长的无重复子串的长度
解法:扫描字符串,sIndex表示扫描过程中当前最长无重复子串的首字符后一位的下标值(所以初始为-1),table[s.charAt(i)]表示字符s.charAt(i)上一次在字符串中出现的位置
——》若该字符串上一次出现的下标table[x]大于sIndex,则sIndex应该重置为当前遍历字符的前一次位置,即将无重复字符串的首字符设为table[x]+1所在位置
public class Solution { public int lengthOfLongestSubstring(String s) { int [] table = new int[256]; for(int i=0; i<table.length; i++) table[i] = -1; int sIndex = -1; int max = 0; for(int i=0; i<s.length(); i++){ int t = table[s.charAt(i)]; sIndex = Math.max(sIndex, t);//每次重置无重复子串的首字符下标 max = Math.max(max, i-sIndex);//每次都重置无重复子串最大长度 table[s.charAt(i)] = i; } return max; }}
0 0
- 【字符串&最长无重复子串】Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters 字符串中最长的无重复子串长度
- 【字符串】最长无重复子串Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters 无重复字符的最长子字符串
- Longest Substring Without Repeating Characters(最长无重复子字符串算法)
- 寻找最长无重复字符子串 Longest Substring Without Repeating Characters
- leetcode:Longest Substring Without Repeating Characters (寻找最长无重复字符的子串)
- Longest Substring Without Repeating Characters,最长无重复字符子串
- LeetCode OJ 之 Longest Substring Without Repeating Characters (无重复的最长子串)
- Longest Substring Without Repeating Characters (返回最长无字符重复的子串长度)
- #3 Longest Substring Without Repeating Characters(最长无重复元素子串)
- LeetCode-3 Longest Substring Without Repeating Characters(最长无重复子串)
- Longest Substring Without Repeating Characters (最长无重复字符的子串)
- 【LeetCode】4. Longest Substring Without Repeating Characters 最长无重复子串
- leetcode 3 : Longest Substring Without Repeating Characters 最长无重复子串 (C# 语言版)
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
- [Leetcode #3]Longest Substring Without Repeating Characters 无重复字符的最长子串
- [Lintcode] Longest Substring Without Repeating Characters 最长无重复字符的子串
- Enhanced Assertions一文笔记
- 希尔排序的体会
- 轮盘赌算法原理(ACO算法概率选择方法)
- 通过代码组织,让你更好的理解和使用JDK动态代理
- 算法--生成随机数组
- 【字符串&最长无重复子串】Longest Substring Without Repeating Characters
- 要多写博客
- Testing: Review Webserver Metafiles for Information Leakage (OTG-INFO-003)
- 《程序员,你伤不起》读书笔记 (五) :当我彻底放弃自私自利后,前途变得一片光明,不能过多的只是关注自己的功夫,生活不能只局限在方寸虚拟世界里
- 内存监测工具DDMS-->Heap
- multiset的使用
- 微博文本情感分析-开篇
- Myeclipse生成doc文档时出现,编码 GBK 的不可映射字符
- mongodb学习之五:聚合之group复习