题目链接:https://leetcode.cn/problems/merge-similar-items/
# 解题思路
简单的hash表应用,值得一提的是可以用数组表示hash表,这样可以省去排序的步骤,具体可看下面的源码。
# 提交结果

# 代码
class Solution {  | |
public List<List<Integer>> mergeSimilarItems(int[][] items1, int[][] items2) {  | |
int[] w = new int[1001];  | |
for (int[] it : items1) {  | |
w[it[0]] = it[1];  | |
        } | |
for (int[] it : items2) {  | |
w[it[0]] += it[1];  | |
        } | |
List<List<Integer>> ret = new ArrayList<>();  | |
for (int i = 1; i < w.length; ++i) {  | |
if (w[i] > 0) {  | |
ret.add(Arrays.asList(i, w[i]));  | |
            } | |
        } | |
return ret;  | |
    } | |
} |