Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples:
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.
- public class T {
- public static void main(String[] args) {
- String s1 = "pwwkew";
- String s2 = "abcabcbb";
- String s3 = "dvdf";
- String s4 = "bbbb";
- System.out.println(lengthOfLongestSubstring(s1));
- }
- public static int lengthOfLongestSubstring(String s) {
- int maxlength = 0;
- int leftIndex = 0;
- int rightIndex = 0;
- while (rightIndex < s.length()) {
- char target = s.charAt(rightIndex);
- int mark = -1;
- for (int i = leftIndex; i < rightIndex; i++) {
- if (s.charAt(i) == target) {
- mark = i + 1;
- break;
- }
- }
- if (mark != -1) {
- if ((rightIndex - leftIndex) > maxlength) {
- maxlength = (rightIndex - leftIndex);
- }
- leftIndex = mark;
- rightIndex = mark;
- } else {
- rightIndex++;
- }
- }
- if ((rightIndex - leftIndex) > maxlength) {
- maxlength = (rightIndex - leftIndex);
- }
- return maxlength;
- }
- }
另附網上的答案一則.
http://www.cnblogs.com/grandyang/p/4480780.html
- public class Solution {
- public int lengthOfLongestSubstring(String s) {
- int[] m = new int[256];
- Arrays.fill(m, -1);
- int res = 0, left = -1;
- for (int i = 0; i < s.length(); ++i) {
- left = Math.max(left, m[s.charAt(i)]);
- m[s.charAt(i)] = i;
- res = Math.max(res, i - left);
- }
- return res;
- }
-
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-2142971/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- #3 Longest Substring Without Repeating Characters[M]
- 3. Longest Substring Without Repeating Characters
- 149 Longest Substring Without Repeating Characters
- Leetcode Longest Substring Without Repeating CharactersLeetCode
- Leetcode 3 Longest Substring Without Repeating CharactersLeetCode
- Leetcode-Longest Substring Without Repeating CharactersLeetCode
- Longest Substring Without Repeating Characters leetcode javaLeetCodeJava
- Leetcode 3. Longest Substring Without Repeating CharactersLeetCode
- LeetCode OJ : 3 Longest Substring Without Repeating CharactersLeetCode
- Leetcode javascript 3 longest-substring-without-repeating-charactersLeetCodeJavaScript
- LeetCode Longest Substring Without Repeating Characters(003)解法總結LeetCode
- [LeetCode] 3. Longest Substring Without Repeating Characters 題解LeetCode
- 【LeetCode從零單排】No 3 Longest Substring Without Repeating CharactersLeetCode
- [LeetCode] Longest Substring Without Repeating Characters 最長無重複字元的子串LeetCode字元
- LeetCode-Longest Substring with At Least K Repeating CharactersLeetCodeAST
- LeetCode3:Longest Substring Without Repeating Characters(無重複字元的最長子串)LeetCode字元
- Leet Code 3. Longest Substring Without Repeating Characters (最長的沒有重複字元的子字串)字元字串
- LeetCode-Longest Substring with At Most K Distinct CharactersLeetCode
- Leetcode-Longest Substring with At Most Two Distinct Characters.LeetCode
- 【Leetcode】3. Longest Substring Without RepeatingCharacters無重最長子串LeetCodeGC
- Leetcode Longest Palindromic SubstringLeetCode
- LeetCode 5 (Longest Palindromic Substring)LeetCode
- LintCode-Longest Common Substring
- Leetcode-Longest Palindromic SubstringLeetCode
- Longest Palindromic Substring leetcode javaLeetCodeJava
- LeetCode 5. Longest Palindromic SubstringLeetCode
- LeetCode OJ : 5 Longest Palindromic SubstringLeetCode
- LeetCode每日一題:longest palindromic substringLeetCode每日一題
- [leetcode] 1624. Largest Substring Between Two Equal CharactersLeetCode
- [LeetCode] 2414. Length of the Longest Alphabetical Continuous SubstringLeetCodeAlphabet
- [LeetCode] Longest Palindromic Substring 最長迴文子串LeetCode
- Leetcode5: Longest Palindromic Substring(最長迴文子串)LeetCode
- Js的substring和C#的SubstringJSC#
- JavaScript substring()JavaScript
- SCSS without和withCSS
- unixODBC without the GUIGUI
- Special Characters in Initialization Parameter Files
- ACM Longest Repeated SequenceACM