My Answer
1. 第四次提交(改进)
/**
* Before update algorithm: <br>
* 1. 利用hashMap 存储被check过的char和它的index <br>
* 2. remove hashMap里 所有在repeat char position之前的all chars<br>
* 3. 利用hashMap.size()计算subString的长度 <br>
* After update algorithm: <br>
* 1. 利用hashMap 存储被check过的char和它的index<br>
* 2. 利用two pointers: left, right作为substring的fromIndex, toIndex. subString长度为right-left <br>
*/
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int left = 0, right = 0;
int maxLgth = 0;
while (right < s.length()) {
char ch = s.charAt(right);
Integer pos = map.get(ch);
// ch already exists in the substring
if (pos != null && pos >= left) {
maxLgth = maxLgth < right - left ? right - left : maxLgth;
left = pos + 1;
}
map.put(ch, right++);
}
// check the last one subString.length
return maxLgth < right - left ? right - left : maxLgth;
}
2. 第三次提交
<span style="font-weight: normal;"> public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int start = 0;
int maxLength = 0;
char[] arr = s.toCharArray();
HashMap<Character, Integer> charForIndex = new HashMap<Character, Integer>();
for (int i = 0; i < arr.length; i++) {
char ch = arr[i];
Integer index = charForIndex.get(ch); // O(1)
if (index != null) {
maxLength = maxLength < charForIndex.size() ? charForIndex.size() : maxLength;
for (int j = start; j < index + 1; j++) {
charForIndex.remove(arr[j]); // remove all chars before index
}
start = index + 1;
}
charForIndex.put(ch, i); // if key is repeat, update value
}
return maxLength = maxLength < charForIndex.size() ? charForIndex.size() : maxLength;
}</span>
<span style="font-weight: normal;"> public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int start = 0;
int maxLength = 0;
char[] arr = s.toCharArray();
HashMap<Character, Integer> charForIndex = new HashMap<Character, Integer>();
for (int i = 0; i < arr.length; i++) {
char ch = arr[i];
Integer index = charForIndex.get(ch); // O(1)
if (index != null) {
maxLength = maxLength < charForIndex.size() ? charForIndex.size() : maxLength;
for (int j = start; j < index + 1; j++) {
charForIndex.remove(arr[j]); // remove all chars before index
}
start = index + 1;
}
charForIndex.put(ch, i); // if key is repeat, update value
}
return maxLength = maxLength < charForIndex.size() ? charForIndex.size() : maxLength;
}</span>
2. 第二次提交
<span style="font-weight: normal;">public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int maxLength = 0;
char[] arr = s.toCharArray();
List<Character> subStr = new LinkedList<Character>();
for (int i = 0; i < arr.length; i++) {
char ch = arr[i];
int index = subStr.indexOf(ch);
if (index > -1) {
int length = subStr.size();
maxLength = maxLength < length ? length : maxLength;
if (index == length - 1) {
subStr.clear();
} else {
subStr = subStr.subList(index + 1, length);
}
}
subStr.add(ch);
}
return maxLength < subStr.size() ? subStr.size() : maxLength;
}</span>
3. 第一次提交
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0){
return 0;
}
char[] arr = s.toCharArray();
Set<Character> charSet = new HashSet<Character>();
int maxSubLength = 0;
for(int i=0 ; i<arr.length; i++){
if(charSet.contains(arr[i])){
if(maxSubLength < charSet.size()){
maxSubLength = charSet.size();
}
charSet.clear();
}
charSet.add(arr[i]);
}
if(maxSubLength < charSet.size()){
maxSubLength = charSet.size();
}
return maxSubLength;
}
Summary
1. 这个题目花了自己很长时间,主要是自己没完全理解题目就“开跑”。 题目tags “Hash Table”,"Two pointers" "String" 。虽然花了很长时间,看到提交运行时间还不错得到些许鼓励。要是正式面试自己这题早超时failure了。
2. 第一次提交基于Set,set是不能保证subString的顺序。一旦出现重复char就会将整个set clear。 例如"abcaxyz"最长子串为"bcaxyz"而不是"axyz"。基于set当遇到第二个a时,set会将'b','c'也clear掉。
3. 第二次提交基于String.indexOf(); String.subString(fromIndex,toIndex)。超时了 。这两个方法都是O(n),在加上外层for-loop.算法复杂度打到O(n^2)
4. 第三次提交基于hashMap. hashMap.get()/remove() running time都是O(1)。另外借助start pointer 去除重复char之前的all chars. 虽然hashMap不能保证subString里char的顺序 没关系,因为题目只需要返回substring的长度。
5. 第四次提交基于two pointers, hashMap. 相对于第三次提交代码更简洁,计算时间更短。
5. String的basic case记得考虑空字符串“”