[算法题目]无重复字符的最长子串
无重复字符的最长子串
描述
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
思路
穷举法,从第一个字符开始去找字串中是否存在相同的两个字符,然后比较两段长度
代码
public int lengthOfLongestSubstring(String s) {
int result = 0;
for (int i = 0; i < s.length(); i++) {
HashMap<Character, Integer> hashMap = new HashMap();
for (int j = i; j < s.length(); j++) {
if (hashMap.get(s.charAt(j)) == null) {
if (j == s.length() - 1) {
result = Math.max(result, s.length() - i);
}
hashMap.put(s.charAt(j), j);
} else {
result = Math.max(result, Math.max(j - i, j - hashMap.get(s.charAt(j))));
break;
}
}
}
return result;
}
效率

本文来自博客园,作者:异世界阿伟,转载请注明原文链接:https://www.cnblogs.com/yusishi/p/16845064.html

浙公网安备 33010602011771号