先贴连接!https://leetcode.com/problems/longest-substring-without-repeating-characters/ 下面是题目的截图。
题目意思很简单,求最长不重复字串的长度。
暴力求解思路:遍历每个字符,然后求该字符接下去的最长不重复字串,如果长度超过我们的maxLen,就重新复制maxLen,如果出现重复字符,则从下一个字符开始。
用图来表示,即:
然后就是贴代码:
class Solution {
public int lengthOfLongestSubstring(String s) {
int maxLen=0;
HashMap<Character, Character> map = new HashMap<Character, Character>();
for(int i=0;i<s.length();i++) {
for(int j=i;j<s.length();j++)
if(!map.containsKey(s.charAt(j))) {
if(j-i+1>maxLen)
maxLen=j-i+1;
map.put(s.charAt(j), s.charAt(j));
}
else {
map.clear();
break;
}
}
return maxLen;
}
}
最近因为课要结束了,忙着看论文准备大作业,就没啥时间来优化了,有时间再看吧。。。
日常吼一句,java赛高!