Q3:Longest Substring Without Repeating Characters
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
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的更多相关文章
- LeetCode3:Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- No.003:Longest Substring Without Repeating Characters
问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List
题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...
- leetcode笔记:Longest Substring Without Repeating Characters
一. 题目描写叙述 Given a string, find the length of the longest substring without repeating characters. For ...
- leetcode:Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【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 ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- tp5日志分表
/** * 记录网站日志 * * @return bool */ public function record() { // 组装数据 $log = self::$param; $log[self:: ...
- 012、Java中发生溢出的转换问题
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- openalyser6学习
1.安装nvm.nvm下载地址:https://github.com/coreybutler/nvm-windows/releases使用nvm-setup安装和下载nodejs:https://ww ...
- DStream-05 updateStateByKey函数的原理和源码
Demo updateState 可以到达将每次 word count 计算的结果进行累加. object SocketDstream { def main(args: Array[String]): ...
- L2d插件
<script src="https://blog-static.cnblogs.com/files/yyhh/L2Dwidget.min.js"></scrip ...
- 用四种方法将两个AJAX改为同步
用四种方法将两个AJAX改为同步 Promise.Generator函数.yield.async/await 相关 今有一题,题目为: 现有ajax1()和ajax2(),用于快速初始化CODE1和C ...
- 如何从Domino迁移到Exchange 2010
从Domino 6.x迁移到Exchange 2010利用了微软提供的工具:Microsoft Transporter Suite,该工具不支持从Domino 6.X直接迁移至Exchange 2 ...
- C++面试常见问题——01预处理与宏定义
C++面试常见问题--------01预编译和宏的使用 C++预处理器 预处理器是一些指令,它将指示编译器在实际编译之前需要完成的预处理.预处理必须要在对程序进行词法与语义分析.代码生成与优化等通常的 ...
- grep -w ,grep -e的使用
1.grep -w -wDoes a word search.grep -w用于字符串精确匹配若文件中的内容包括如下:262 a326226如果 grep ‘26’ file,结果是三行全部都被显示若 ...
- iphone 面试题(转)
转]iPhone 面试题解答 2011-07-20 0:51 转载自 492437598 最终编辑 492437598 1.main() { int a[5]={1,2,3,4,5}; ...