LeetCode - Longest Substring Without Repeating Characters
来源:互联网 发布:学生档案表sql查询 编辑:程序博客网 时间:2024/06/10 21:58
Question:
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.
用一个数组来存储每一个字符在字符串中的位置,数组的下标表示这个字符的ASCII码,元素表示其在字符串中的位置即可。如下图所示(转):
代码如下所示:
import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;import java.util.Set;public class LeetCode3 {/** * 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. * @param args */public static void main(String[] args) {// TODO Auto-generated method stubString s1 = "";String s2 = null;String s3 = "abebcdab";String s4 = "abcabcde";int length = lengthOfLongestSubstring(s3);System.out.println(length);}public static int lengthOfLongestSubstring(String s) {if (s == null) {return -1;}int length = s.length();if (length == 0) {return 0;}int[] countTable = new int[256];Arrays.fill(countTable, -1);int max = 1;int start = 0;int end = 1;countTable[s.charAt(0)] = 0;while (end < length) {// Has not reached a duplicate charif (countTable[s.charAt(end)] >= start) {start = countTable[s.charAt(end)] + 1;}max = Math.max(max, end - start + 1);countTable[s.charAt(end)] = end;end++;}return max;}}
0 0
- LeetCode: Longest Substring Without Repeating Characters
- LeetCode Longest Substring Without Repeating Characters
- LeetCode: Longest Substring Without Repeating Characters
- [Leetcode] Longest Substring Without Repeating Characters
- leetcode 23: Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- LeetCode 3 - Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters
- leetcode Longest Substring Without Repeating Characters
- 【leetcode】 Longest Substring Without Repeating Characters
- LeetCode-Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [leetcode] Longest Substring Without Repeating Characters
- LeetCode - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- leetcode之Longest Substring Without Repeating Characters
- Deep Learning(深度学习)学习笔记整理
- hdu 1308 What Day Is It? (算星期几)
- C++编程
- HTML5游戏引擎中音频的播放策略
- awk的BEGIN和END
- LeetCode - Longest Substring Without Repeating Characters
- poj 1189 DP
- C函数调用与堆栈的变化
- 云计算体系架构研究
- iOS 开发,工程中混合使用 ARC 和非ARC
- armeabi 和armeabi-v7a文件夹的区别
- Codeforces_392B_Tower of Hanoi(记忆化搜索)
- STL 容器
- SharePoint Powershell Script