2019-10-10 刷題總結(jié) -- Hash Table

今天主要刷hash table的題目宝踪,主要按照frequency從高到低的順序。

  1. two sum: 使用HashMap
  2. 3 sum: 一開始以為是簡單的for loop?two sum的解法碍扔,但實(shí)際上把代碼寫出來后發(fā)現(xiàn)自己完全忽視了duplicates的情況瘩燥。對于有duplicate的情況,一般需要先把a(bǔ)rrray排序不同。

加入重復(fù)性考慮后厉膀,pseudocode變成這樣:

// sort the array
Arrays.sort(nums)
for i = 0 to nums.length-2:
    low = i+1, high = nums.length-1;
    // skip duplicates
    if (i == 0 || (i > 0 && nums[i] == nums[i-1]:
            while (low < high) :
                    sum = nums[i] + nums[low] + nums[high]
                    if (sum == 0):
                            add to the result
                            // doing skipping duplicates here
                            low++; high--;
                    else  if (sum < 0):
                              // doing skipping duplicates here
                             low++
                    else:
                              // doing skipping duplicates here
                              high--;

最后放上完整的代碼:

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < nums.length - 2; i++) {
            // skip duplicates
            if (i == 0 || (i > 0 && nums[i] != nums[i-1])) {
                int low = i+1, high = nums.length-1, sum = 0 - nums[i];
                while (low < high) {
                    if (nums[low] + nums[high] == sum) {
                        res.add(Arrays.asList(new Integer[]{nums[i], nums[low], nums[high]}));
                        // skip duplicates
                        while (low < high && nums[low] == nums[low+1]) {
                            low++;
                        }
                        while (low < high && nums[high] == nums[high-1]) {
                            high--;
                        }
                        low++;
                        high--;
                    } else if (nums[low] + nums[high] < sum) {
                        // skip duplicates
                        while (low < high && nums[low] == nums[low+1]) {
                            low++;
                        }
                            low++;
                    } else {
                        // skip duplicates
                        while (low < high && nums[high] == nums[high-1]) {
                            high--;
                        }
                            high--;
                    }
                }
            }
        }
        return res;
    }
}
  1. 4 Sum: 思路基本和3 Sum相同,就是比3 Sum多了一個(gè)for loop
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < nums.length - 3; i++) {
            if (i == 0 || (i > 0 && nums[i] != nums[i-1])) {
                for (int j = i+1; j < nums.length - 2; j++) {
                    if (j == i+1 || (j > i+1 && nums[j] != nums[j-1])) {
                        int lo = j+1, hi = nums.length-1, sum = target-nums[i]-nums[j];
                        while (lo < hi) {
                            if (nums[lo] + nums[hi] == sum) {
                                res.add(Arrays.asList(new Integer[]{nums[i], nums[j], nums[lo], nums[hi]}));
                                while (lo < hi && nums[lo] == nums[lo+1]) {
                                    lo++;
                                }
                                while (lo < hi && nums[hi] == nums[hi-1]) {
                                    hi--;
                                }
                                lo++;
                                hi--;
                            } else if (nums[lo] + nums[hi] < sum) {
                                while (lo < hi && nums[lo] == nums[lo+1]) {
                                    lo++;
                                }
                                lo++;
                            } else {
                                while (lo < hi && nums[hi] == nums[hi-1]) {
                                    hi--;
                                }
                                hi--;
                            }
                        }
                    }
                    
                }
            }
            
        }
        return res;
    }
}
  1. Jewels and Stones
    把只包含字母的string轉(zhuǎn)化為固定長度的array來表示可以節(jié)省一些時(shí)間二拐。
  2. Single Number
    所有數(shù)都出現(xiàn)兩次逢渔,除了一個(gè)數(shù)只出現(xiàn)一次。
    第一反應(yīng)是a ^ a = 0
class Solution {
    public int singleNumber(int[] nums) {
        int res = 0;
        for (int n: nums) {
            res ^= n;
        }
        return res;
    }
}

但鑒于這是一個(gè)hash table的問題系奉,再用繁瑣一點(diǎn)的hash table解法來做一次矮瘟。但是慢多了。

class Solution {
    public int singleNumber(int[] nums) {
        Map<Integer, Integer> map =  new HashMap<>();
        for (int n: nums) {
            if (map.containsKey(n)) {
                map.remove(n);
            } else {
                map.put(n,1);
            }
        }
        for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
            return entry.getKey();
        }
        return 0;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末饭望,一起剝皮案震驚了整個(gè)濱河市仗哨,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌铅辞,老刑警劉巖厌漂,帶你破解...
    沈念sama閱讀 216,591評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異斟珊,居然都是意外死亡苇倡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評論 3 392
  • 文/潘曉璐 我一進(jìn)店門倍宾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來雏节,“玉大人,你說我怎么就攤上這事高职」痴В” “怎么了?”我有些...
    開封第一講書人閱讀 162,823評論 0 353
  • 文/不壞的土叔 我叫張陵怔锌,是天一觀的道長寥粹。 經(jīng)常有香客問我变过,道長,這世上最難降的妖魔是什么涝涤? 我笑而不...
    開封第一講書人閱讀 58,204評論 1 292
  • 正文 為了忘掉前任媚狰,我火速辦了婚禮,結(jié)果婚禮上阔拳,老公的妹妹穿的比我還像新娘崭孤。我一直安慰自己,他們只是感情好糊肠,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評論 6 388
  • 文/花漫 我一把揭開白布辨宠。 她就那樣靜靜地躺著,像睡著了一般货裹。 火紅的嫁衣襯著肌膚如雪嗤形。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,190評論 1 299
  • 那天弧圆,我揣著相機(jī)與錄音赋兵,去河邊找鬼。 笑死搔预,一個(gè)胖子當(dāng)著我的面吹牛霹期,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播拯田,決...
    沈念sama閱讀 40,078評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼经伙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了勿锅?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,923評論 0 274
  • 序言:老撾萬榮一對情侶失蹤枣氧,失蹤者是張志新(化名)和其女友劉穎溢十,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體达吞,經(jīng)...
    沈念sama閱讀 45,334評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡张弛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了酪劫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吞鸭。...
    茶點(diǎn)故事閱讀 39,727評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖覆糟,靈堂內(nèi)的尸體忽然破棺而出刻剥,到底是詐尸還是另有隱情,我是刑警寧澤滩字,帶...
    沈念sama閱讀 35,428評論 5 343
  • 正文 年R本政府宣布造虏,位于F島的核電站御吞,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏漓藕。R本人自食惡果不足惜陶珠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望享钞。 院中可真熱鬧揍诽,春花似錦、人聲如沸栗竖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽划滋。三九已至饵筑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間处坪,已是汗流浹背根资。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留同窘,地道東北人玄帕。 一個(gè)月前我還...
    沈念sama閱讀 47,734評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像想邦,于是被迫代替她去往敵國和親裤纹。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評論 2 354

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