1570. Dot Product of Two Sparse Vectors
Given two sparse vectors, compute their dot product.
Implement class SparseVector:
SparseVector(nums)Initializes the object with the vectornumsdotProduct(vec)Compute the dot product between the instance of SparseVector andvec
A sparse vector is a vector that has mostly zero values, you should store the sparse vector efficiently and compute the dot product between two SparseVector.
Follow up: What if only one of the vectors is sparse?
Example 1:
Input: nums1 = [1,0,0,2,3], nums2 = [0,3,0,4,0] Output: 8 Explanation: v1 = SparseVector(nums1) , v2 = SparseVector(nums2) v1.dotProduct(v2) = 1*0 + 0*3 + 0*0 + 2*4 + 3*0 = 8
Example 2:
Input: nums1 = [0,1,0,0,0], nums2 = [0,0,0,0,2] Output: 0 Explanation: v1 = SparseVector(nums1) , v2 = SparseVector(nums2) v1.dotProduct(v2) = 0*0 + 1*0 + 0*0 + 0*0 + 0*2 = 0
Example 3:
Input: nums1 = [0,1,0,0,2,0,0], nums2 = [1,0,0,0,3,0,4] Output: 6
1 class SparseVector { 2 Map<Integer, Integer> indexMap = new HashMap<>(); 3 SparseVector(int[] nums) { 4 for (int i = 0; i < nums.length; i++) 5 if (nums[i] != 0) 6 indexMap.put(i, nums[i]); 7 } 8 9 public int dotProduct(SparseVector vec) { 10 if (indexMap.size() == 0 || vec.indexMap.size() == 0) { 11 return 0; 12 } 13 14 if (indexMap.size() > vec.indexMap.size()) { 15 return vec.dotProduct(this); 16 } 17 18 int productSum = 0; 19 for (Map.Entry<Integer, Integer> entry : indexMap.entrySet()) { 20 int index = entry.getKey(); 21 Integer vecValue = vec.indexMap.get(index); 22 if (vecValue != null) { 23 productSum += (entry.getValue() * vecValue); 24 } 25 } 26 return productSum; 27 } 28 }
做法二:
1 class SparseVector { 2 List<int[]> list = new ArrayList<>(); 3 SparseVector(int[] nums) { 4 for (int i = 0; i < nums.length; i++) 5 if (nums[i] != 0) 6 list.add(new int[] {i, nums[i]}); 7 } 8 9 public int dotProduct(SparseVector vec) { 10 List<int[]> passedIn = vec.list; 11 if (list.size() == 0 || passedIn.size() == 0) { 12 return 0; 13 } 14 15 if (list.size() > passedIn.size()) { 16 return vec.dotProduct(this); 17 } 18 19 int productSum = 0; 20 int idxCurrent = 0, idxPassedIn = 0; 21 while (idxCurrent < list.size() && idxPassedIn < passedIn.size()) { 22 int[] current = list.get(idxCurrent); 23 int[] passedInArray = passedIn.get(idxPassedIn); 24 25 if (current[0] == passedInArray[0]) { 26 productSum += current[1] * passedInArray[1]; 27 idxCurrent++; 28 idxPassedIn++; 29 } else if (current[0] < passedInArray[0]) { 30 idxCurrent++; 31 } else { 32 idxPassedIn++; 33 } 34 } 35 return productSum; 36 } 37 }
做法三:
1 class SparseVector { 2 List<int[]> list = new ArrayList<>(); 3 SparseVector(int[] nums) { 4 for (int i = 0; i < nums.length; i++) 5 if (nums[i] != 0) 6 list.add(new int[] {i, nums[i]}); 7 } 8 9 public int dotProduct(SparseVector vec) { 10 List<int[]> passedIn = vec.list; 11 if (list.size() == 0 || passedIn.size() == 0) { 12 return 0; 13 } 14 15 if (list.size() > passedIn.size()) { 16 return vec.dotProduct(this); 17 } 18 19 int productSum = 0; 20 for(int idxCurrent = 0; idxCurrent < list.size(); idxCurrent++) { 21 int[] current = list.get(idxCurrent); 22 int idxInPassedIn = binarySearch(passedIn, current[0]); 23 if (idxInPassedIn != -1) { 24 productSum += current[1] * passedIn.get(idxInPassedIn)[1]; 25 } 26 } 27 return productSum; 28 } 29 30 private int binarySearch(List<int[]> list, int target) { 31 if (list == null || list.isEmpty()) { 32 return -1; 33 } 34 35 int begin = 0; 36 int end = list.size() - 1; 37 while (begin <= end) { 38 int mid = (end - begin) / 2 + begin; 39 if (list.get(mid)[0] == target) { 40 return mid; 41 } else if (list.get(mid)[0] > target) { 42 end = mid - 1; 43 } else { 44 begin = mid + 1; 45 } 46 } 47 return -1; 48 } 49 }

浙公网安备 33010602011771号