|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace BinarySearch.Lib |
| 8 | +{ |
| 9 | + // Given two arrays, write a function to compute their intersection. |
| 10 | + |
| 11 | + //Example: |
| 12 | + |
| 13 | + //Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. |
| 14 | + // Note: |
| 15 | + //Each element in the result should appear as many times as it shows in both arrays. |
| 16 | + //The result can be in any order. |
| 17 | + //Follow up: |
| 18 | + |
| 19 | + //What if the given array is already sorted? How would you optimize your algorithm? |
| 20 | + //What if nums1’s size is small compared to nums2’s size? Which algorithm is better? |
| 21 | + //What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once? |
| 22 | + public class IntersectionOfTwoArraysSln |
| 23 | + { |
| 24 | + //交集定义,元素可重复 |
| 25 | + public int[] Intersection(int[] nums1, int[] nums2) |
| 26 | + { |
| 27 | + if (nums1 == null || nums1.Length == 0) return new int[] { }; |
| 28 | + if (nums2 == null || nums2.Length == 0) return new int[] { }; |
| 29 | + nums1 = nums1.OrderBy(r => r).ToArray(); //升序排列 |
| 30 | + nums2 = nums2.OrderBy(r => r).ToArray(); |
| 31 | + |
| 32 | + int n = nums1.Length < nums2.Length ? nums1.Length : nums2.Length; |
| 33 | + List<int> rtn = new List<int>(); |
| 34 | + if (nums1.Length < nums2.Length) |
| 35 | + { |
| 36 | + rtn = getIntersection(nums1, nums2); |
| 37 | + } |
| 38 | + else |
| 39 | + rtn = getIntersection(nums2, nums1); |
| 40 | + return rtn.ToArray(); |
| 41 | + } |
| 42 | + |
| 43 | + //二分查找插入位置(相等元素,新插入位置靠后) |
| 44 | + //beginIndex:查询的起始位置 |
| 45 | + private int searchInsertIndex(int[] sorted, int lo, int e) |
| 46 | + { |
| 47 | + int hi = sorted.Length; |
| 48 | + while (lo < hi) |
| 49 | + { |
| 50 | + int mi = (lo + hi) >> 1; |
| 51 | + if (e < sorted[mi]) |
| 52 | + hi = mi; |
| 53 | + else |
| 54 | + lo = mi + 1; |
| 55 | + } |
| 56 | + return lo; |
| 57 | + } |
| 58 | + |
| 59 | + //nums1个数小于后者得到交集 |
| 60 | + private List<int> getIntersection(int[] nums1, int[] nums2) |
| 61 | + { |
| 62 | + List<int> rtn = new List<int>(); |
| 63 | + int index = 0; |
| 64 | + for (int i = 0; i < nums1.Length; i++) |
| 65 | + { |
| 66 | + index = searchInsertIndex(nums2, index, nums1[i]); |
| 67 | + if (index <= 0) //nums2中一定不存在这个元素 |
| 68 | + continue; |
| 69 | + if (nums1[i] == nums2[index - 1]) |
| 70 | + { |
| 71 | + rtn.Add(nums1[i]); |
| 72 | + int precnt = preSame(nums2, index - 1); |
| 73 | + int succnt = sucSame(nums1, i); |
| 74 | + int sml = precnt < succnt ? precnt : succnt; |
| 75 | + while (sml-- > 0) |
| 76 | + rtn.Add(nums1[i]); |
| 77 | + if (succnt > 0) |
| 78 | + i = i + succnt; |
| 79 | + } |
| 80 | + } |
| 81 | + return rtn; |
| 82 | + } |
| 83 | + |
| 84 | + //有序数组中向<----后搜索相等元素的个数 |
| 85 | + private int preSame(int[] nums, int index) |
| 86 | + { |
| 87 | + int sameCnt = 0; |
| 88 | + for (int i = index; i > 0; i--) |
| 89 | + { |
| 90 | + if (nums[index] == nums[i - 1]) |
| 91 | + sameCnt++; |
| 92 | + } |
| 93 | + return sameCnt; |
| 94 | + } |
| 95 | + |
| 96 | + //有序数组中向<----前搜索相等元素的个数 |
| 97 | + private int sucSame(int[] nums, int index) |
| 98 | + { |
| 99 | + int sameCnt = 0; |
| 100 | + for (int i = index; i < nums.Length - 1; i++) |
| 101 | + { |
| 102 | + if (nums[index] == nums[i + 1]) |
| 103 | + sameCnt++; |
| 104 | + } |
| 105 | + return sameCnt; |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | +} |
0 commit comments