LeetCode第三题Longest Substring Without Repeating Characters

一、题目说明

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;
 }

推荐阅读更多精彩内容