【发布时间】:2015-08-31 18:52:44
【问题描述】:
我有一个 java 地图:
Map<String, ArrayList<Integer>> positions = new HashMap<String, ArrayList<Integer>>();
基本上这张地图包含:
word1: 2 10 17
word2: 3 8 15 20
word3: 6 9 19 22
word4: 7 12 18 24
..... and so on
现在我想找到所有单词位置之间的最小范围。我们将拥有唯一且已排序的位置(即没有两个整数相同)。
The minimum range here is 3 (between 10, 8, 9 and 7)
我们应该如何解决这个问题?
I thought of the following steps:
1) Have pointers as many as words length.
2) In this example, we have 4 pointers, pointing towards 2, 3, 6 and 7.
3) The range is calculated which comes out be 5.
4) Keep this range in a certain variable say 'min'.
5) Repeat until all positions in all lists have not been read:
a) Move the lowest pointer to the next in the list(In this example, we move pointer-1 to point to 10).
b) Calculate the range again (this comes to be 7 now).
c) If the new range < min:
min = new range
6) Return 'min'
但我不知道如何在 Java 中解决这个问题。有人可以帮帮我吗?如果您有某些不同的方法,我会欢迎。
【问题讨论】: