美文网首页
LeetCode第三题Longest Substring Wit

LeetCode第三题Longest Substring Wit

作者: Diffey | 来源:发表于2016-03-12 23:54 被阅读202次

一、题目说明

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.
查看原题;
查看我的Github项目地址;
题目:求给定字符串中没有重复字符的最长子字符串的长度。

二、解题

题目比较简单,不多说
代码如下:

public static int lengthOfLongestSubstring(String s) {
     if (s == null) {
         return 0;
     }
     if (s.length() == 1) {
         return 1;
     }

     char[] sChars = s.toCharArray();
     int sIndex = 0;
     int eIndex = 0;
     int lenght = 0;
     while (eIndex < sChars.length) {
         for (int i = sIndex; i < eIndex; i++) {
             if (sChars[i] == sChars[eIndex]) {
                 lenght = eIndex - sIndex > lenght ? eIndex - sIndex : lenght;
                 sIndex = i + 1;
                 break;
             }
         }
         eIndex++;
     }
     lenght = eIndex - sIndex > lenght ? eIndex - sIndex : lenght;
     return lenght;
 }

相关文章

网友评论

      本文标题:LeetCode第三题Longest Substring Wit

      本文链接:https://www.haomeiwen.com/subject/zjgxlttx.html