8.25 more arrays (c++ set)

- Find Minimum in Rotated Sorted Array

  • it counts when it's not roated
  • note the while loop condition to avoid basecase embedded within loop body
    int findMin(vector<int>& nums) {
        int n = nums.size();
        if (!n) return 0;
        if (n==1) return nums[0];
        int left = 0, right = n-1;
        //not rotated at all
        if (nums[left]<nums[right]) return nums[left]; 
        
        while (left < right-1) { // trick: stop when left == right-1
            int mid = left + (right-left)/2;
            if (nums[left]<=nums[mid]) left = mid;
            else right = mid;
        }
        return min(nums[left], nums[right]);
    }

- Find Minimum in Rotated Sorted Array II

  • note the array may become "unrotated" during processing in this case
    int findMin(vector<int>& nums) {
        int n = nums.size();
        if (!n) return INT_MIN;
        if (n==1) return nums[0];
        int left = 0, right = n-1;
        
        while (left < right-1) {
            if (nums[left]<nums[right]) return nums[left];
            int mid = left + (right-left)/2;
            if (nums[left]<nums[mid]) left = mid;
            else if (nums[left]==nums[mid]) ++left;
            else right = mid;
        }
        return min(nums[left], nums[right]);
    }

- Shuffle an Array

  • random_shuffle()用來對一個元素序列重新排序
  • no need to specify the last gen random seed func, just for fun
class Solution {
public:
    Solution(vector<int> nums) {
        original = curr = nums;
    }
    
    /** Resets the array to its original configuration and return it. */
    vector<int> reset() {
        curr = original;
        return curr;
    }
    
    /** Returns a random shuffling of the array. */
    vector<int> shuffle() {
        random_shuffle(curr.begin(), curr.end(), [](int i){ return rand()%i; });
        return curr;
    }
private: 
    vector<int> original;
    vector<int> curr;
};

- intersection-of-two-arrays

Set vs. unordered_set

  • store unique elements following a specific order
  • where the value of an element also identifies it (the value is itself the key, of type T). Elements cannot be modified, but can be deleted/added
  • internally, the elements in a set are always sorted following a specific strict weak ordering
  • set containers are generally slower than unordered_set containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.Sets are typically implemented as binary search trees.
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> set1(nums1.begin(), nums1.end());
        vector<int> ret;
        for (int i : nums2) 
            if (set1.erase(i)) // return # of erased els
                ret.push_back(i);
            
        return ret;
    }

- Intersection of Two Arrays II

1. hash table (Time: O(m+n); Space: O(m+n))

    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int, int> map;
        for (int i :nums1) ++map[i];
        vector<int> ret;
        for (int i :nums2) {
            if (map.find(i)!=map.end()) {
                if (--map[i]==0) map.erase(i);
                ret.push_back(i);
            }
        }
        return ret;
    }

2.

map fun fact..
if a key is not exist, access the key will assign a default value to the key. Means if you simply test if map[key] is 0 or not by using if (map[key]) without testing if the key is in the map, you will consume extra space

    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int, int> map;
        for (int i :nums1) ++map[i];
        vector<int> ret;
        for (int i :nums2) {
            if (map.find(i)!=map.end()) {
                if (--map[i]==0) map.erase(i);
                ret.push_back(i);
            }
        }
        return ret;
    }

mark: do w/ 2 ptrs

- Product of Array Except Self

  • consider edge case..

mark: quick try

    vector<int> productExceptSelf(vector<int>& nums) {
        int n = nums.size();
        vector<int> ret(n, 1);
        for (int i=1; i<n; ++i) {
            ret[i] = ret[i-1]*nums[i-1];
        }
        int acc = 1;
        for (int i=n-2; i>=0; --i) {
            acc *= nums[i+1];
            ret[i] *= acc;
        }
        return ret;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市扶叉,隨后出現(xiàn)的幾起案子障簿,更是在濱河造成了極大的恐慌善榛,老刑警劉巖连躏,帶你破解...
    沈念sama閱讀 216,919評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件碧注,死亡現(xiàn)場離奇詭異世囊,居然都是意外死亡该抒,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,567評論 3 392
  • 文/潘曉璐 我一進店門策严,熙熙樓的掌柜王于貴愁眉苦臉地迎上來穗慕,“玉大人,你說我怎么就攤上這事妻导」涿啵” “怎么了?”我有些...
    開封第一講書人閱讀 163,316評論 0 353
  • 文/不壞的土叔 我叫張陵倔韭,是天一觀的道長术浪。 經(jīng)常有香客問我,道長寿酌,這世上最難降的妖魔是什么胰苏? 我笑而不...
    開封第一講書人閱讀 58,294評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮醇疼,結(jié)果婚禮上硕并,老公的妹妹穿的比我還像新娘。我一直安慰自己秧荆,他們只是感情好殷费,可當(dāng)我...
    茶點故事閱讀 67,318評論 6 390
  • 文/花漫 我一把揭開白布难审。 她就那樣靜靜地躺著歌径,像睡著了一般责循。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上琉兜,一...
    開封第一講書人閱讀 51,245評論 1 299
  • 那天,我揣著相機與錄音毙玻,去河邊找鬼豌蟋。 笑死,一個胖子當(dāng)著我的面吹牛桑滩,可吹牛的內(nèi)容都是我干的梧疲。 我是一名探鬼主播允睹,決...
    沈念sama閱讀 40,120評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼幌氮!你這毒婦竟也來了缭受?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,964評論 0 275
  • 序言:老撾萬榮一對情侶失蹤该互,失蹤者是張志新(化名)和其女友劉穎米者,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宇智,經(jīng)...
    沈念sama閱讀 45,376評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡蔓搞,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,592評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了随橘。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片喂分。...
    茶點故事閱讀 39,764評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖机蔗,靈堂內(nèi)的尸體忽然破棺而出蒲祈,到底是詐尸還是另有隱情,我是刑警寧澤萝嘁,帶...
    沈念sama閱讀 35,460評論 5 344
  • 正文 年R本政府宣布梆掸,位于F島的核電站,受9級特大地震影響酿愧,放射性物質(zhì)發(fā)生泄漏沥潭。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,070評論 3 327
  • 文/蒙蒙 一嬉挡、第九天 我趴在偏房一處隱蔽的房頂上張望钝鸽。 院中可真熱鬧,春花似錦庞钢、人聲如沸拔恰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,697評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽颜懊。三九已至,卻和暖如春风皿,著一層夾襖步出監(jiān)牢的瞬間河爹,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,846評論 1 269
  • 我被黑心中介騙來泰國打工桐款, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留咸这,地道東北人。 一個月前我還...
    沈念sama閱讀 47,819評論 2 370
  • 正文 我出身青樓魔眨,卻偏偏與公主長得像媳维,于是被迫代替她去往敵國和親酿雪。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,665評論 2 354

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

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗侄刽。 張土汪:刷leetcod...
    土汪閱讀 12,744評論 0 33
  • 手機是現(xiàn)代生活中的不可缺少的工具指黎,但他也像毒藥一樣浸透著我們生活的全部。為此州丹,很多人搭上了自己的眼睛醋安。也成就了不少...
    王俏勻卓筒酒業(yè)閱讀 500評論 0 0
  • 今天是2017年6月30日,這一年已經(jīng)過半了当叭,而在這半年中我有對生活茬故,對工作有無數(shù)強烈的想法充斥于自己的腦海中,有...
    起個名字真難999閱讀 172評論 0 0
  • 每次重新看培姐的講課蚁鳖,我都特別舒緩磺芭,而且自己就笑出來了,騎車時想到自己曾經(jīng)的想法醉箕、那么傻钾腺,自己也笑,然后輕輕的搖頭...
    可可可鑫閱讀 265評論 0 2