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;
 }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容