350. Intersection of Two Arrays II

Given two arrays, write a function to compute their intersection.
Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].

Note:
Each element in the result should appear as many times as it shows in both arrays.
The result can be in any order.

Follow up:
What if the given array is already sorted? How would you optimize your algorithm?
What if nums1's size is small compared to nums2's size? Which algorithm is better?
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?

Solution:

Two-pointer walk.
感覺代碼有些地方寫得太啰嗦了,有空優(yōu)化一下:

public class Solution {
    public int[] intersect(int[] nums1, int[] nums2)
    {
        List<Integer> result = new ArrayList<>();

        Arrays.sort(nums1);
        Arrays.sort(nums2);

        int i = 0, j = 0;
        while(i < nums1.length && j < nums2.length)
        {
            while(nums1[i] < nums2[j])
            {
                i++;
                if(i == nums1.length) break;
            }
            if(i >= nums1.length || j >= nums2.length) break;

            while(nums1[i] > nums2[j])
            {
                j++;
                if(j == nums2.length) break;
            }
            if(i >= nums1.length || j >= nums2.length) break;

            if(nums1[i] == nums2[j])
            {
                result.add(nums1[i]);
                i++; j++;
            }
        }

        int size = result.size();
        int[] resultArray = new int[size];

        for(int cao = 0; cao < size; cao++)
        {
            resultArray[cao] = result.get(cao);
        }
        return resultArray;
    }
}

看了 tag酸员,除了 two-pointer walk 之外,還可以用 HashTable(HashMap)……厲害了我的 Map!

嘗試一下:

public class Solution 
{
    public int[] intersect(int[] nums1, int[] nums2) 
    {
        List<Integer> result = new ArrayList<>();
        HashMap<Integer, Integer> hm = new HashMap<>();
        for(int a : nums1)
        {
            int newCount = hm.getOrDefault(a, 0);
            newCount++;
            hm.put(a, newCount);
        }
        for(int b : nums2)
        {
            int newCount = hm.getOrDefault(b, 0);
            newCount--;
            if(newCount >= 0)
                result.add(b);
            hm.put(b, newCount);
        }
        
        int[] resultArray = new int[result.size()];
        for(int i = 0; i < result.size(); i++)
        {
            resultArray[i] = result.get(i);
        }
        
        return resultArray;
    }
}

HashMap (HashTable) 爸爸好掠河!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末项炼,一起剝皮案震驚了整個(gè)濱河市鸽凶,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌尖飞,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件店雅,死亡現(xiàn)場離奇詭異政基,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)闹啦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進(jìn)店門沮明,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人窍奋,你說我怎么就攤上這事荐健。” “怎么了琳袄?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵江场,是天一觀的道長。 經(jīng)常有香客問我窖逗,道長址否,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任碎紊,我火速辦了婚禮佑附,結(jié)果婚禮上樊诺,老公的妹妹穿的比我還像新娘。我一直安慰自己帮匾,他們只是感情好啄骇,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著瘟斜,像睡著了一般缸夹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上螺句,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天虽惭,我揣著相機(jī)與錄音,去河邊找鬼蛇尚。 笑死芽唇,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的取劫。 我是一名探鬼主播匆笤,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼谱邪!你這毒婦竟也來了炮捧?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤惦银,失蹤者是張志新(化名)和其女友劉穎咆课,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體扯俱,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡书蚪,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了迅栅。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片殊校。...
    茶點(diǎn)故事閱讀 39,841評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖库继,靈堂內(nèi)的尸體忽然破棺而出箩艺,到底是詐尸還是另有隱情,我是刑警寧澤宪萄,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布艺谆,位于F島的核電站,受9級特大地震影響拜英,放射性物質(zhì)發(fā)生泄漏静汤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望虫给。 院中可真熱鬧藤抡,春花似錦、人聲如沸抹估。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽药蜻。三九已至瓷式,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間语泽,已是汗流浹背贸典。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留踱卵,地道東北人廊驼。 一個(gè)月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像惋砂,于是被迫代替她去往敵國和親妒挎。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容