599:两个列表的最小索引总和
This commit is contained in:
parent
fbbc11e24a
commit
a607ae99e9
@ -44,27 +44,32 @@ public class MinimumIndexSumOfTwoLists{
|
|||||||
//测试代码
|
//测试代码
|
||||||
Solution solution = new MinimumIndexSumOfTwoLists().new Solution();
|
Solution solution = new MinimumIndexSumOfTwoLists().new Solution();
|
||||||
}
|
}
|
||||||
|
|
||||||
//力扣代码
|
//力扣代码
|
||||||
//leetcode submit region begin(Prohibit modification and deletion)
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
class Solution {
|
class Solution {
|
||||||
public String[] findRestaurant(String[] list1, String[] list2) {
|
public String[] findRestaurant(String[] list1, String[] list2) {
|
||||||
HashMap< Integer, List < String >> map = new HashMap < > ();
|
List<String> listS1 = new ArrayList<>(Arrays.asList(list1));
|
||||||
for (int i = 0; i < list1.length; i++) {
|
List<String> listS2 = new ArrayList<>(Arrays.asList(list2));
|
||||||
for (int j = 0; j < list2.length; j++) {
|
List<String> list = new ArrayList<>();
|
||||||
if (list1[i].equals(list2[j])) {
|
int min = list1.length + list2.length;
|
||||||
if (!map.containsKey(i + j)) {
|
for (int i = 0; i < listS2.size(); i++) {
|
||||||
map.put(i + j, new ArrayList < String > ());
|
int index = listS1.indexOf(listS2.get(i));
|
||||||
}
|
if (index >= 0) {
|
||||||
map.get(i + j).add(list1[i]);
|
if (list.isEmpty() || index + i < min) {
|
||||||
|
list = new ArrayList<>();
|
||||||
|
list.add(listS2.get(i));
|
||||||
|
min = index + i;
|
||||||
|
} else if (index + i == min) {
|
||||||
|
list.add(listS2.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int min_index_sum = Integer.MAX_VALUE;
|
String[] strs = new String[list.size()];
|
||||||
for (int key: map.keySet()) {
|
for (int i = 0; i < list.size(); i++) {
|
||||||
min_index_sum = Math.min(min_index_sum, key);
|
strs[i] = list.get(i);
|
||||||
}
|
}
|
||||||
String[] res = new String[map.get(min_index_sum).size()];
|
return strs;
|
||||||
return map.get(min_index_sum).toArray(res);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//leetcode submit region end(Prohibit modification and deletion)
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
Loading…
Reference in New Issue
Block a user