leetcode 3. Longest Substring Without Repeating Characters

来源:互联网 发布:知乎 盛世美颜 编辑:程序博客网 时间:2024/05/16 05:21

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

给定一个字符串,求出最长的没有重复字符的子串的长度

public class A3LongestSubstring {public int lengthOfLongestSubstring(String s) {if(s.length() == 0 || s == null) {return 0;}int[] length = new int[s.length()];length[0] = 1;for(int i = 1,j; i < s.length(); i++) {for(j = i - 1; j != -1 && s.charAt(i) != s.charAt(j); j--) {}if(j < 0) {length[i] = i + 1;} else {length[i] = i - j;}if(length[i] > length[i - 1] + 1) {length[i] = length[i - 1] + 1;}}Arrays.sort(length);        return length[s.length() - 1];    }}


0 0
原创粉丝点击