LeetCode||3. Longest Substring Without Repeating Characters

来源:互联网 发布:实体店结算软件 编辑:程序博客网 时间:2024/06/06 04:07

3. Longest Substring Without Repeating Characters

  • Total Accepted: 200515
  • Total Submissions: 860059
  • Difficulty: Medium
  • Contributors: Admin

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 asubstring, "pwke" is a subsequence and not a substring.

题意还是很好理解的,最长无重复的字符的字符串。

代码如下:

public class Solution {    public int lengthOfLongestSubstring(String s) {        int length = s.length();          if (length == 0) {              return 0;          }          int [] countTable = new int[256];          for(int i = 0;i<256;i++){            countTable[i] = -1;        }        int max = 1;          int start = 0;          int end = 1;                    countTable[s.charAt(0)] = 0;          while (end < length) {              //Has not reached a duplicate char              if (countTable[s.charAt(end)] >= start) {                  start = countTable[s.charAt(end)] + 1;                            }              max = Math.max(max, end - start + 1);              countTable[s.charAt(end)] = end;              end++;          }          return max;      }}



0 0
原创粉丝点击