美文网首页
算法练习100天-第3天

算法练习100天-第3天

作者: 有意思的小太太 | 来源:发表于2020-09-02 17:28 被阅读0次

类别:数组

题目: 349. 两个数组的交集

我的解题思路:

class Solution {
    /**
    1.要求返回一个整型,元素不重复数组,可以利用集合set的特性
    2.先将一个nums1值保存在set1中,循环nums2,判断set1中是否存在nums2中的值,存在则保存在set2中
    3.将set2转换为int[]
      >先将set2转为Integer型数组
      >再将Integer型数组转为int型数组
    **/
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet();
        Set<Integer> set2 = new HashSet();
        for (int a : nums1) {
            set1.add(a);
        }
        for (int b : nums2) {
            if (set1.contains(b)) {
                set2.add(b);
            }
        }
        Integer[] temp = set2.toArray(new Integer[] {});
        int[] intArray = new int[temp.length];
        for (int i = 0; i < temp.length; i++) {
            intArray[i] = temp[i].intValue();
        }
        return intArray;
    }
}

官方解题思路:

方法1:Set(题解中出现最多的)

 public int[] intersection(int[] nums1, int[] nums2) {
    if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
      return new int[0];
    }
    Set<Integer> parentSet = new HashSet<>();
    Set<Integer> childSet = new HashSet<>();
    for (int num : nums1) {
      parentSet.add(num);
    }
    for (int num : nums2) {
      if (parentSet.contains(num)) {
        childSet.add(num);
      }
    }
    int[] resArr = new int[childSet.size()];
    int index = 0;
    for (int value : childSet) {
      resArr[index++] = value;
    }
    return resArr;
  }
方法2:双指针
先将nums1 与nums2排序,然后游走两个指针,情况都写出来了,没有用else
时间复杂度:O(nlogn)

public int[] intersection(int[] nums1, int[] nums2) {
  Set<Integer> set = new HashSet<>();
  Arrays.sort(nums1);
  Arrays.sort(nums2);
  int i = 0, j = 0;
  while (i < nums1.length && j < nums2.length) {
    if (nums1[i] == nums2[j]) {
      set.add(nums1[i]);
      i++;
      j++;
    } else if (nums1[i] < nums2[j]) {
      i++;
    } else if (nums1[i] > nums2[j]) {
      j++;
    }
  }
  int[] res = new int[set.size()];
  int index = 0;
  for (int num : set) {
    res[index++] = num;
  }
  return res;
}
方法3:二分查找
将nums2排序,然后查找nums2的元素,需要准备一个binarySearch的辅助方法,注意left<= right

public int[] intersection(int[] nums1, int[] nums2) {
  Set<Integer> set = new HashSet<>();
  Arrays.sort(nums2);
  for (int target : nums1) {
    if (binarySearch(nums2, target) && !set.contains(target)) {
      set.add(target);
    }
  }
  int index = 0;
  int[] res = new int[set.size()];
  for (int num : set) {
    res[index++] = num;
  }
  return res;
}
public boolean binarySearch(int[] nums, int target) {
  int left = 0, right = nums.length - 1;
  while (left <= right) {
    int mid = left + (right - left) / 2;
    if (nums[mid] == target) {
      return true;
    } else if (nums[mid] > target) {
      right = mid - 1;
    } else if (nums[mid] < target) {
      left = mid + 1;
    }
  }
  return false;
}

差异点

  • 没有想到将数组排序,排序后的数组可以用二分查找法来判断两个数组中是否存在相同值

相关文章

  • 前端干货 -03

    37. 算法 算法地址 数据结构与算法 JavaScript 描述. 章节练习https://github.com...

  • 算法-心得

    本周大部分的时间都在练习算法。虽说是有点被逼迫的意思,但是算法还是很重要的,也需要自己练习。 说到算法,我自身的感...

  • 算法练习(-)

    You are a professional robber planning to rob houses alon...

  • 算法练习

    将字符串转化为数字,实现int()方法 回文数 俩数之和 给定一个整数数组 nums 和一个目标值 target,...

  • 算法练习

    背景 Find closing/opening parenthesis You will be implement...

  • 算法练习

    2021 三月份 1. 两数之和 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 ...

  • 刷算法 - 算法练习

    最近断断续续的刷了一些基础算法题. 我们做移动端开发的, 刷算法题有意义吗? 如果对这个问题有疑问, 可以在读这篇...

  • 2018-11-11 算法练习题

    下面是几道算法练习题:

  • iOS + 常用排序算法

    算练习吧参照的原文常用排序算法总结(一)八大排序算法

  • 笨办法学C 练习39:字符串算法

    练习39:字符串算法 原文:Exercise 39: String Algorithms 译者:飞龙 这个练习中,...

网友评论

      本文标题:算法练习100天-第3天

      本文链接:https://www.haomeiwen.com/subject/kpmwsktx.html