由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - lengthOfLongestSubstring 最后一个test case总是超时
相关主题
facebook一题关于java synchronized statement和static method or variable
share int2roman and roman2int java version发个evernote的code challenge
4sum o(n^2)超时Isomorphic Strings 的单Hashmap解法
问一下OJ的Anagrams那道题Amazon常见设计题——设计电话簿求解
Leetcode的Substring with Concatenation of All Words超时。来讨论个uber的电面题
好挫的F家面经上午偷闲把TopKFrequentWords写出来了
LRU cache 超时Leetcode第30题真心不容易
LRU cache 超时, 大家帮忙看看input a string "hello word", print l:3 o:2 e:1 d:1 h:1 r:1 w:1.不知道哪错了
相关话题的讨论汇总
话题: currentlen话题: maxlength话题: cstart话题: totallen话题: int
进入JobHunting版参与讨论
1 (共1页)
n*******n
发帖数: 45
1
木有用DP....
public int lengthOfLongestSubstring(String s) {
if (s == null) {
return 0;
}
if (s.length() < 2) {
return s.length();
}
int maxLength = 0;
int totalLen = s.length();
int currentLen = 0;
int cStart = 0;
Map indexMap = new HashMap<>();
while((totalLen - cStart) > maxLength) {
for(int i = cStart; i < totalLen; i++) {
Character cur = s.charAt(i);
if (!indexMap.containsKey(cur)) {
currentLen++;
indexMap.put(cur, i); //update position
} else {
int position = indexMap.get(cur);
indexMap.put(cur, i); //update position
if (position < cStart || position == i){
currentLen++; // continue
} else {
if (currentLen > maxLength) {
maxLength = currentLen;
}
cStart = position + 1;
currentLen = 0;
break;
}
}
if (i == (totalLen-1)){ // corner case for last
if (currentLen > maxLength) {
maxLength = currentLen;
}
}
}
}
return maxLength;
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
input a string "hello word", print l:3 o:2 e:1 d:1 h:1 r:1 w:1.不知道哪错了Leetcode的Substring with Concatenation of All Words超时。
Amazon first phone interview好挫的F家面经
问道题,谁给个效率高点的解法LRU cache 超时
一个实际碰到的问题LRU cache 超时, 大家帮忙看看
facebook一题关于java synchronized statement和static method or variable
share int2roman and roman2int java version发个evernote的code challenge
4sum o(n^2)超时Isomorphic Strings 的单Hashmap解法
问一下OJ的Anagrams那道题Amazon常见设计题——设计电话簿求解
相关话题的讨论汇总
话题: currentlen话题: maxlength话题: cstart话题: totallen话题: int