注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看,没有帐号?注册账号
x
Utilization Checks : https://leetcode.com/discuss/interview-question/376019/
其中一个测试案例是:[46, 73, 77, 53, 75, 22, 55, 84, 45, 40, 80, 66, 54, 39, 68, 23, 54, 22, 11, 91, 47, 56, 91, 97, 5, 44, 62, 73, 26, 99, 96, 74, 4, 0, 8, 56, 3, 21, 37, 94, 83, 68, 91, 83, 41, 22, 81, 59, 37, 29, 93, 8, 88, 41, 94, 62, 63, 97, 73, 46, 80, 91, 65, 69, 52, 31, 35, 81, 60, 44, 8, 80, 75, 94, 16, 45, 12, 29, 22, 59, 88, 87, 55, 43, 67, 8, 15, 26, 31, 99, 35, 99, 1, 98]
没拷贝代码但是全部通过。确保你的代码能通过上面这个案例
Items in Containers: https://leetcode.com/discuss/interview-question/865660/
测试案例除了题目中简单的那些外,马上上到500个长度的startIndices,要确保你的代码是O(n+m)的复杂度。我考完后修改的代码:
[Java] 纯文本查看 复制代码 public static List<Integer> numberOfItems(String s, List<Integer> startIndices, List<Integer> endIndices) {
List<Integer> result = new ArrayList<>();
if (s == null || s.isEmpty()) {
return result;
}
char[] charArray = s.toCharArray();
List<Integer> pipePositions = new ArrayList<>();
int[] nextIndexInPipePositions = new int[charArray.length]; //for each character in the string, the valid counting point will be from the position indicated by the index of pipePositions. for example, "**|**|" the pipePositions will be [2,5] and nextIndexInPipePositions will be [0,0,0,1,1,1] indicating [2,2,2,5,5,5]
for (int i = 0; i < charArray.length; i++) {
nextIndexInPipePositions[i] = pipePositions.size();
if (charArray[i] == '|') {
pipePositions.add(i);
}
}
int indicesSize = Math.min(startIndices.size(), endIndices.size());
for (int i = 0; i < indicesSize; i++) {
int firstPipePositionIndex = nextIndexInPipePositions[startIndices.get(i) - 1];
int lastPipePositionIndex = nextIndexInPipePositions[endIndices.get(i) - 1];
if (charArray[endIndices.get(i) - 1] != '|') { //the next pipe is after the endIndex - should count to the previous pipe
lastPipePositionIndex--;
}
if (lastPipePositionIndex > firstPipePositionIndex) {
// count of *s = the total distance between the first pipe and the last pipe - the pipe count between the fist piple and the last pipe
result.add(pipePositions.get(lastPipePositionIndex) - pipePositions.get(firstPipePositionIndex) - lastPipePositionIndex + firstPipePositionIndex);
} else { // no pipe or only one pipe between
result.add(0);
}
}
return result;
}
|