[关闭]
@XQF 2018-03-07T22:58:21.000000Z 字数 2388 阅读 839

如何计算两个有序整型数组的交集

数据结构与算法


问题描述:假设两个含有n个元素的有序(非降序)整型数组a和b,其中a={0,1,2,3,4}.b={1,3,5,7,9}.那么他们的交集为{1,3}

1.长度差不多的情况下

  1. 二路归并法
  2. 放入哈希表
  3. 散列

哈希表不是很正确,。因为,非降序应该是代表着可以相等的情况。
散列,。,。不会呀

  1. public class Solution {
  2. public ArrayList<Integer> mix(int[] a, int[] b) {
  3. if(a==null|||b==null){
  4. return null;
  5. }
  6. int len = a.length + b.length;
  7. ArrayList<Integer> result = new ArrayList<>();
  8. int i = 0;
  9. int j = 0;
  10. int counter = 0;
  11. while (counter < len) {
  12. if (i >= a.length) {
  13. break;
  14. } else if (j >= b.length) {
  15. break;
  16. } else if (a[i] == b[j]) {
  17. counter++;
  18. result.add(a[i]);
  19. i++;
  20. j++;
  21. } else if (a[i] < b[j]) {
  22. i++;
  23. } else if (a[i] > b[j]) {
  24. j++;
  25. }
  26. }
  27. return result;
  28. }
  29. public static void main(String[] args) {
  30. Solution solution = new Solution();
  31. int[] a = {1, 2, 3};
  32. int[] b = {1, 3, 4};
  33. System.out.println(solution.mix(a, b));
  34. }
  35. }

2.长度悬殊较大

遍历短数组,在长数组中进行二分搜索

  1. public class Solution {
  2. public ArrayList<Integer> mix(int[] a, int[] b) {
  3. if (a == null || b == null) {
  4. return null;
  5. }
  6. ArrayList<Integer> result = new ArrayList<>();
  7. int lenA = a.length;
  8. int lenB = b.length;
  9. int[] temp = null;
  10. if (lenA < lenB) {
  11. temp = a;
  12. a = b;
  13. b = temp;
  14. }
  15. for (int i = 0; i < lenB; i++) {
  16. boolean find = binarySearch(a, 0, a.length - 1, b[i], false);
  17. if (find) {
  18. result.add(b[i]);
  19. }
  20. }
  21. return result;
  22. }
  23. public boolean binarySearch(int[] nums, int left, int right, int target, boolean find) {
  24. if (left > right) {
  25. return find;
  26. }
  27. int mid = (left + right) / 2;
  28. if (nums[mid] == target) {
  29. find = true;
  30. return find;
  31. } else if (nums[mid] > target) {
  32. return binarySearch(nums, left, mid - 1, target, find);
  33. } else {
  34. return binarySearch(nums, mid + 1, right, target, find);
  35. }
  36. }
  37. public static void main(String[] args) {
  38. Solution solution = new Solution();
  39. int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25};
  40. int[] b = {1, 3, 4, 25};
  41. System.out.println(solution.mix(a, b));
  42. }
  43. }

优化的二分,返回索引

  1. public class Solution {
  2. public ArrayList<Integer> mix(int[] a, int[] b) {
  3. if (a == null || b == null) {
  4. return null;
  5. }
  6. ArrayList<Integer> result = new ArrayList<>();
  7. int lenA = a.length;
  8. int lenB = b.length;
  9. int[] temp = null;
  10. if (lenA < lenB) {
  11. temp = a;
  12. a = b;
  13. b = temp;
  14. }
  15. int left = 0;
  16. int right = lenA - 1;
  17. int find = -1;
  18. for (int i = 0; i < lenB; i++) {
  19. find = binarySearch(a, left, right, b[i], Integer.MAX_VALUE);
  20. if (find != Integer.MAX_VALUE) {
  21. result.add(a[find]);
  22. left = find;
  23. }
  24. }
  25. return result;
  26. }
  27. public int binarySearch(int[] nums, int left, int right, int target, int find) {
  28. if (left > right) {
  29. return find;
  30. }
  31. int mid = (left + right) / 2;
  32. if (nums[mid] == target) {
  33. find = mid;
  34. return find;
  35. } else if (nums[mid] > target) {
  36. return binarySearch(nums, left, mid - 1, target, find);
  37. } else {
  38. return binarySearch(nums, mid + 1, right, target, find);
  39. }
  40. }
  41. public static void main(String[] args) {
  42. Solution solution = new Solution();
  43. int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25};
  44. int[] b = {1, 3, 4, 25};
  45. System.out.println(solution.mix(a, b));
  46. }
  47. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注