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.
public class Solution {
public int lengthOfLongestSubstring(String s) {// Start typing your Java solution below
// DO NOT write main() function
Set<String> candidate = new HashSet<String>();
int length = 0;
for (int k=0; k<s.length();k++) {
for (int i = k; i < s.length(); i++) {
char tmp = s.charAt(i);
if (candidate.contains(String.valueOf(tmp)) ) {
if (length < candidate.size()) {
length = candidate.size();
}
candidate.clear();
}
candidate.add(String.valueOf(tmp));
}
if (length < candidate.size()) {
length = candidate.size();
}
candidate.clear();
}
return length;
}
}
本文介绍了一个算法,用于找到字符串中不包含重复字符的最长子串长度。通过实例演示,展示了如何解决这一问题。
971

被折叠的 条评论
为什么被折叠?



