成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] Intersection of Two Arrays I &

enda / 3559人閱讀

摘要:先想到的是,其實(shí)也可以,只是需要在遍歷的時(shí)候,添加到數(shù)組中的數(shù)要掉,略微麻煩了一點(diǎn)。在里跑的時(shí)候,也要快一點(diǎn)。另一種類似做法的就快的多了。如果是找出所有包括重復(fù)的截距呢

Problem

Given two arrays, write a function to compute their intersection.

Notice

Each element in the result must be unique.
The result can be in any order.

Example

Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].

Challenge

Can you implement it in three different algorithms?

Note

先想到的是HashSet(),其實(shí)HashMap也可以,只是需要在遍歷nums2的時(shí)候,添加到res數(shù)組中的數(shù)要remove掉,略微麻煩了一點(diǎn)。在LC里跑的時(shí)候,HashSet也要快一點(diǎn)。
另一種類似HashMap做法的BitSet()就快的多了。

Solution HashSet
public class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set set1 = new HashSet();
        Set set2 = new HashSet();
        List ans = new ArrayList();
        for (int i = 0; i < nums1.length; i++) set1.add(nums1[i]);
        for (int i = 0; i < nums2.length; i++) {
            if (set1.contains(nums2[i])) set2.add(nums2[i]);
        }
        int[] res = new int[set2.size()];
        int index = 0;
        for (Integer i: set2) res[index++] = i;
        return res;
    }
}
BitSet
public class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        int[] res = new int[nums1.length];
        if (nums1.length == 0 || nums2.length == 0) return new int[0];
        int index = 0;
        BitSet set = new BitSet();
        for (int i = 0; i < nums1.length; i++) {
            set.set(nums1[i]);
        }
        for (int i = 0; i < nums2.length; i++) {
            if (set.get(nums2[i]) == true) {
                res[index++] = nums2[i];
                set.set(nums2[i], false);
            }
        }
        return Arrays.copyOfRange(res, 0, index);
    }
}
Follow Up

如果是找出所有包括重復(fù)的截距呢?-- Intersection of Two Arrays II

public class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        int k = 0, l1 = nums1.length, l2 = nums2.length;
        int[] result = new int[l1];
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        int i = 0, j = 0;
        while (i < l1 && j < l2)
            if (nums1[i] < nums2[j]) i++;
            else if (nums1[i] == nums2[j++]) result[k++] = nums1[i++];
        return Arrays.copyOf(result, k);
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/64776.html

相關(guān)文章

  • [LintCode/LeetCode] Intersection of Two Linked Lis

    Problem Write a program to find the node at which the intersection of two singly linked lists begins. Example The following two linked lists: A: a1 → a2 ↘ ...

    OldPanda 評(píng)論0 收藏0
  • [LintCode/LeetCode] Median of two Sorted Arrays

    摘要:由于要求的時(shí)間,所以選擇二分法。思路是找到兩個(gè)數(shù)組合并起來(lái)的第個(gè)元素。這樣只需計(jì)算兩個(gè)數(shù)組的中位數(shù)是第幾個(gè)元素,代入功能函數(shù)即可。據(jù)此,根據(jù)二分法的性質(zhì),我們?cè)谶f歸時(shí)可以將前即個(gè)元素排除。 Problem There are two sorted arrays A and B of size m and n respectively. Find the median of the tw...

    vvpvvp 評(píng)論0 收藏0
  • [LintCode/LeetCode] Scramble String

    摘要:首先將兩個(gè)字符串化成字符數(shù)組,排序后逐位比較,確定它們等長(zhǎng)且具有相同數(shù)量的相同字符。然后,從第一個(gè)字符開(kāi)始向后遍歷,判斷和中以這個(gè)坐標(biāo)為中點(diǎn)的左右兩個(gè)子字符串是否滿足第一步中互為的條件設(shè)分為和,分為和。 Problem Given a string s1, we may represent it as a binary tree by partitioning it to two no...

    MASAILA 評(píng)論0 收藏0
  • [LintCode/LeetCode] Two Strings are Anagrams/Valid

    摘要:建立一個(gè)長(zhǎng)度為的數(shù)組,統(tǒng)計(jì)所有個(gè)字符在出現(xiàn)的次數(shù),然后減去這些字符在中出現(xiàn)的次數(shù)。否則,循環(huán)結(jié)束,說(shuō)明所有字符在和中出現(xiàn)的次數(shù)一致,返回。 Program Write a method anagram(s,t) to decide if two strings are anagrams or not. Example Given s=abcd, t=dcab, return true....

    vslam 評(píng)論0 收藏0
  • [LeetCode] Intersection of Two Arrays I & II

    Intersection of Two Arrays I Problem Given two arrays, write a function to compute their intersection. Example Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note Each element in the result m...

    lucas 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<