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項目地址;
題目:求給定字符串中沒有重復(fù)字符的最長子字符串的長度。

二、解題

題目比較簡單,不多說
代碼如下:

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;
 }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容