Question:
My code:
import java.util.HashMap;
public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0)
return 0;
int max = 1;
int length = 0;
HashMap<Character, Integer> c = new HashMap<Character, Integer>();
int tail = 0;
int head = 0;
while (tail < s.length()) {
if (!c.containsKey(s.charAt(tail))) {
length++;
c.put(s.charAt(tail), tail);
tail++;
if (max < length)
max = length;
}
else {
int tempHead = head;
head = c.get(s.charAt(tail));
length = length - (head + 1 - tempHead);
for (int i = tempHead; i < head; i++)
c.remove(s.charAt(i));
c.put(s.charAt(head), tail);
head++;
tail++;
length++;
}
}
return max;
}
public static void main(String[] args) {
String s = "dvdf";
Solution test = new Solution();
System.out.println(test.lengthOfLongestSubstring(s));
}
}
My test result:
這次作業(yè)不是很那,但是一開始有點掉以輕心了,所以寫的不是很好,只用了一個指針。
后來發(fā)現(xiàn),必須得用兩個指針。具體說下思路。
我覺得這個也有貪婪的思想在里面。
有兩個指針,head 和tail.包含了這樣一條字符串,里面沒有重復(fù)。
當(dāng)tail指向的字符出現(xiàn)在哈希表里面時,此時,head應(yīng)該指向該字符(key)所對應(yīng)的index(value)的下一位。并且把前面已經(jīng)存在的character從哈希表中刪除。
然后再配合一些小操作,保證一些細(xì)節(jié)被滿足。
差不多就這樣了。
**
總結(jié): HashMap HashSet
貪心的想法。即保證每一步都奔著更大去的,那些更大也都小于最大的情況,就可以直接排除了。
**
女朋友的父親到底是怎么樣的一個人?
如果真的是因為那樣的小事而為難自己的女兒,那這人的心胸該有多么狹窄啊。我一直覺得我的父親度量小,但那都是可以忍受的,正常的。但是如果是那樣狹窄的心胸,我是不能忍受的,也絕對不會和這樣的人的女兒最后在一起。再看吧。
Anyway, Good luck, Richardo!
My code:
public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0)
return 0;
int max = 0;
int start = 0;
HashMap<Character, Integer> tracker = new HashMap<Character, Integer>();
for (int i = 0; i < s.length(); i++) {
char curr = s.charAt(i);
if (tracker.containsKey(curr) && tracker.get(curr) > 0) { // if current map contains such character
while (tracker.get(curr) > 0) {
char pre = s.charAt(start);
tracker.put(pre, tracker.get(pre) - 1);
start++;
}
tracker.put(curr, 1);
max = Math.max(max, i - start + 1);
}
else {
tracker.put(curr, 1);
max = Math.max(max, i - start + 1);
}
}
return max;
}
}
其實就是sliding window 的思想。
Substring with Concatenation of All Words -
http://www.lxweimin.com/p/c754b343b8e4Minimum Window Substring -
http://www.lxweimin.com/p/d4745ac61b4f
維持一個window,進(jìn)行操作。
HashMap 可以改成用HashSet 來做。
Anyway, Good luck, Richardo!
My code:
public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int i = 0;
int max = 0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
while (i < s.length()) {
char curr = s.charAt(i);
if (!map.containsKey(curr)) {
map.put(curr, i);
i++;
max = Math.max(max, map.size());
}
else {
int pre = map.get(curr);
i = pre + 1;
map.clear();
}
}
return max;
}
}
這是我自己的code,看起來還是很低效冗雜的。
這道題目考的其實是 sliding window,看了參考:
My code:
public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int i = 0;
int j = 0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int maxLen = 0;
for (; i < s.length(); i++) {
char curr = s.charAt(i);
if (map.containsKey(curr)) {
j = Math.max(j, map.get(curr) + 1);
}
maxLen = Math.max(maxLen, i - j + 1);
map.put(curr, i);
}
return maxLen;
}
}
這里的,就簡潔多了。下面介紹下, j = Math.max(j, map.get(curr) + 1) 這個操作
當(dāng) i 的character重復(fù)出現(xiàn)時,這時候需要更新窗口。
如果,i這個character,在j之前出現(xiàn),那么,對當(dāng)前的窗口沒影響,仍然沒有重復(fù)。不更新j
如果,在j之后出現(xiàn),那么,當(dāng)前窗口就存在重復(fù),需要更新 j
j = i + 1
差不多就這個思路。
reference:
https://discuss.leetcode.com/topic/8232/11-line-simple-java-solution-o-n-with-explanation
然后這篇文章寫得也不錯:
https://leetcode.com/articles/longest-substring-without-repeating-characters/