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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Leetcode 3 Longest Substring Without Repeating CharactersLeetCode
- #3 Longest Substring Without Repeating Characters[M]
- Leetcode 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
- LeetCode3:Longest Substring Without Repeating Characters(無重複字元的最長子串)LeetCode字元
- Leet Code 3. Longest Substring Without Repeating Characters (最長的沒有重複字元的子字串)字元字串
- 【Leetcode】3. Longest Substring Without RepeatingCharacters無重最長子串LeetCodeGC
- LeetCode 5 (Longest Palindromic Substring)LeetCode
- [LeetCode] 2414. Length of the Longest Alphabetical Continuous SubstringLeetCodeAlphabet
- [leetcode] 1624. Largest Substring Between Two Equal CharactersLeetCode
- SPOJ 1811 Longest Common Substring(字尾自動機)
- Leetcode5: Longest Palindromic Substring(最長迴文子串)LeetCode
- CSS repeating-radial-gradient()CSS
- CSS repeating-linear-gradient()CSS
- Js的substring和C#的SubstringJSC#
- JavaScript substring()JavaScript
- Longest Valid Parentheses
- SCSS without和withCSS
- Longest Univalue Path
- C# SubstringC#
- CSS3 repeating-radial-gradient()CSSS3
- CSS3 repeating-linear-gradient()CSSS3
- Performance Without the Event LoopORMOOP
- scp without interative password
- C#中substringC#
- Substring with Concatenation of All Words
- SQL Server SUBSTRING FunctionsSQLServerFunction
- MySQl 擷取函式 left(),right(),substring(),substring_index() 的用法MySql函式Index
- Leetcode 32 Longest Valid ParenthesesLeetCode
- Leetcode 14 Longest Common PrefixLeetCode
- docker_sshd without passwordDocker
- 687-Longest Univalue Path
- Installing Windows Features without InternetWindows
- LeetCode之Find Common Characters(Kotlin)LeetCodeKotlin
- [LeetCode] 451. Sort Characters By FrequencyLeetCode
- substr()和substring()區別
- SUBSTRING() 與 CONV() 函式函式