给定一个只包含数字的字符串,例如“127343456”,找出最长递增子序列,例如上面的就是3456就是最长递增子序列.
可以使用滑动窗口来解决这个问题,从而在O(n)的时间复杂度内完成.
public static Pair<Integer, Integer> getMaxIncreSubStr(String s){
int maxLength = 0;
int startResult = 0;
int start = 0;
int end = start + 1;
int length = s.length();
while(start < length && end < length){
int current = s.charAt(end);
int before = s.charAt(end - 1);
if(current == before + 1){
if(end == length - 1){
int tempLength = end - start + 1;
if(tempLength > maxLength){
maxLength = tempLength;
startResult = start;
}
}
end++;
}else{
int tempLength = end - start;
if(tempLength > maxLength){
maxLength = tempLength;
startResult = start;
}
start = end;
end = start + 1;
}
}
return new Pair<>(maxLength, startResult);
}
556

被折叠的 条评论
为什么被折叠?



