Skip to content

Latest commit

 

History

History
145 lines (105 loc) · 3.7 KB

File metadata and controls

145 lines (105 loc) · 3.7 KB

English Version

题目描述

给定两个稀疏向量,计算它们的点积(数量积)。

实现类 SparseVector

  • SparseVector(nums) 以向量 nums 初始化对象。
  • dotProduct(vec) 计算此向量与 vec 的点积。

稀疏向量 是指绝大多数分量为 0 的向量。你需要 高效 地存储这个向量,并计算两个稀疏向量的点积。

进阶:当其中只有一个向量是稀疏向量时,你该如何解决此问题?

 

示例 1:

输入:nums1 = [1,0,0,2,3], nums2 = [0,3,0,4,0]
输出:8
解释:v1 = SparseVector(nums1) , v2 = SparseVector(nums2)
v1.dotProduct(v2) = 1*0 + 0*3 + 0*0 + 2*4 + 3*0 = 8

示例 2:

输入:nums1 = [0,1,0,0,0], nums2 = [0,0,0,0,2]
输出:0
解释:v1 = SparseVector(nums1) , v2 = SparseVector(nums2)
v1.dotProduct(v2) = 0*0 + 1*0 + 0*0 + 0*0 + 0*2 = 0

示例 3:

输入:nums1 = [0,1,0,0,2,0,0], nums2 = [1,0,0,0,3,0,4]
输出:6

 

提示:

  • n == nums1.length == nums2.length
  • 1 <= n <= 10^5
  • 0 <= nums1[i], nums2[i] <= 100

解法

哈希表实现。

用哈希表存储非 0 点的下标与值。求点积时,遍历长度较小的哈希表。

Python3

class SparseVector:
    def __init__(self, nums: List[int]):
        self.v = {}
        for i, num in enumerate(nums):
            if num != 0:
                self.v[i] = num

    # Return the dotProduct of two sparse vectors
    def dotProduct(self, vec: 'SparseVector') -> int:
        res = 0
        if len(self.v) > len(vec.v):
            self.v, vec.v = vec.v, self.v
        for i, num in self.v.items():
            if i not in vec.v:
                continue
            res += num * vec.v[i]
        return res


# Your SparseVector object will be instantiated and called as such:
# v1 = SparseVector(nums1)
# v2 = SparseVector(nums2)
# ans = v1.dotProduct(v2)

Java

class SparseVector {

    private Map<Integer, Integer> v;

    SparseVector(int[] nums) {
        v = new HashMap<>();
        for (int i = 0; i < nums.length; ++i) {
            if (nums[i] != 0) {
                v.put(i, nums[i]);
            }
        }
    }

    // Return the dotProduct of two sparse vectors
    public int dotProduct(SparseVector vec) {
        int res = 0;
        if (v.size() > vec.v.size()) {
            Map<Integer, Integer> t = v;
            v = vec.v;
            vec.v = t;
        }
        for (Map.Entry<Integer, Integer> entry : v.entrySet()) {
            int i = entry.getKey(), num = entry.getValue();
            res += num * vec.v.getOrDefault(i, 0);
        }
        return res;
    }
}

// Your SparseVector object will be instantiated and called as such:
// SparseVector v1 = new SparseVector(nums1);
// SparseVector v2 = new SparseVector(nums2);
// int ans = v1.dotProduct(v2);

...