# 1507. Dot Product of Two Sparse Vectors ###### tags: `Leetcode` `FaceBook` `Medium` Link: https://leetcode.com/problems/dot-product-of-two-sparse-vectors/ ## Code ```java= class SparseVector { Map<Integer, Integer> mapping; SparseVector(int[] nums) { mapping = new HashMap<Integer, Integer>(); for(int i = 0;i < nums.length;i++){ if(nums[i]!=0){ mapping.put(i,nums[i]); } } } // Return the dotProduct of two sparse vectors public int dotProduct(SparseVector vec) { int result = 0; for(int i:this.mapping.keySet()){ if(vec.mapping.containsKey(i)){ result+=mapping.get(i)*vec.mapping.get(i); } } return result; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up