| 廣告聯系 | 簡體版 | 手機版 | 微信 | 微博 | 搜索:
歡迎您 游客 | 登錄 | 免費注冊 | 忘記了密碼 | 社交賬號注冊或登錄

首頁

新聞資訊

論壇

溫哥華地產

大溫餐館點評

溫哥華汽車

溫哥華教育

黃頁/二手

旅游
搜索:  

 論壇通告:  請不要上傳第三方有版權的照片,請尊重版權,謝謝   轉載新聞請務必注明出處,這些媒體請不要轉,謝謝   批評商家需要注意  
 個人空間: 羅蓬特機器人 | XY | 五木森林 | 豬頭看世界 | 亂想 | 細雨飄渺 | rxmei | 客觀中立而實事求是,唯服理據而杜絕辱罵 | 靜觀雲卷雲舒 | 大溫房產和地產研究 | 花隨風 | 顧曉軍 | Invisible world | dwx | lxls | 呱呱叫廚房 | 異鄉的世界 | 呂洪來的個人空間 | Notme | 逸言堂
 最新求助: 請問誰知道哪裡有賣理發的電動推子?   忽然有個疑問:戰爭時期,加拿大拿PR卡未入籍的永久居民會被強制服兵役嗎?   這個銀條   如何修改會員名?
 論壇轉跳:
     發帖回帖獲取加西鎊, 兌換精彩禮物

論壇首頁 -> IT人生

Leetcode Hacking Practice -- Solution in C++ Part 2 (發表於11年前)

分頁: 1, 2, 3, 4, 5  下一頁  



回復主題  圖片幻燈展示  增添帖子到書簽中  給帖子中的發貼者批量贈送獻花或者花籃    |##| -> |=|        發表新主題
閱讀上一個主題 :: 閱讀下一個主題  
作者 正文
webdriver
(只看此人)




文章 時間: 2014-9-24 01:28 引用回復
LeetCode (used to call i has 1337 code) is a social platform for preparing IT technical interviews. We strive to provide you with the best learning experience in preparing interviews for companies in the IT industry.

To be successful in a technical interview, we believe it is mainly repeating these three important steps:

Code. Read. Discuss.

We strive to provide you the LeetCode platform as the ultimate solution for preparing technical interviews.

1. Code -> Code solution using the Online Judge system.
2. Read -> Read high quality article featuring in-depth thought process. Also read other LeetCoders’ code.
3. Discuss -> Discuss your thoughts about the problem with other LeetCoders.

We hope that through our platform, you will grow into a LeetCoder. Not only will you be successful in all of your interviews, and most importantly, you will be a better coder in general !

This is part 2

第一部分: www.westca.com/Forums/...inese.html


 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?


上一次由webdriver於2014-9-25 11:49修改,總共修改了2次
樓主 | 電梯直達
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)




文章 時間: 2014-9-24 01:31 引用回復
Problem Name: Minimum Depth of Binary Tree
Description:
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node
down to the nearest leaf node.
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
沙發 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-24 01:31 引用回復
Now comes the solution (in C++) ...

1. Recursion. Pay attention to cases when the non-leaf node has only one child.
2. Iteration + Queue. Contributed by SUN Mian(瀛欏啎).

代碼:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode *root) {
        return minDepth_1(root);
    }
   
    int minDepth_1(TreeNode *root) {
        if (!root)
            return 0;
       
        if (!root->left && !root->right)
            return 1;
        else if (!root->left)
            return 1 + minDepth_1(root->right);
        else if (!root->right)
            return 1 + minDepth_1(root->left);
        else
            return 1 + min(minDepth_1(root->left), minDepth_1(root->right));
    }
   
    int minDepth_2(TreeNode *root) {
        if (!root) return 0;
        queue<TreeNode *> q;
        q.push(root);
        q.push(NULL);
        int depth = 1;
        while (true)
        {
            TreeNode *node = q.front();
            q.pop();
            if (!node) {
                depth++;
                q.push(NULL);
            } else {
                if (!node->left && !node->right) return depth;
                if (node->left) q.push(node->left);
                if (node->right) q.push(node->right);
            }
        }
    }
};
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?


上一次由webdriver於2014-9-24 01:33修改,總共修改了1次
板凳 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-24 01:32 引用回復
Problem Name: Minimum Path Sum
Description:
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right
which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
地板 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-24 01:32 引用回復
Now comes the solution (in C++) ...

Dynamic Programming. Space O(N).

代碼:

class Solution {
public:
    int minPathSum(vector<vector<int> > &grid) {
        if (grid.empty()) return INT_MIN;
        int M = grid.size(), N = grid[0].size();
        int dp[N];
        dp[0] = grid[0][0];
        for (int i = 1; i < N; ++i)
            dp[i] = grid[0][i] + dp[i-1];
       
        for (int i = 1; i < M; ++i)
        {
            dp[0] += grid[i][0];
            for (int j = 1; j < N; ++j)
                dp[j] = min(dp[j-1], dp[j]) + grid[i][j];
        }
       
        return dp[N-1];
    }
};
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
5 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-24 01:37 引用回復
Problem Name: Max Points On a Line
Description:
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
6 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-24 01:37 引用回復
Now comes the solution (in C++) ...

...

代碼:

class Solution {
public:
    int maxPoints(vector<Point> &points) {
        int N = points.size(), res(0);
        unordered_map<double, int> m;
        for(int i = 0;i < N; ++i){
            m.clear();
            int ss(1), sp(0);                        // ss:points with same slope,starts with 1, sp:same points, starts with 0
            for(int j = i+1;j < N; ++j){
                double slope = std::numeric_limits<double>::infinity();
                if(points[i].x != points[j].x)
                    slope = 1.0*(points[i].y - points[j].y)/(points[i].x - points[j].x);
                else if(points[i].y == points[j].y){
                    sp++; continue;
                }
                int tmp = 0;
                if(m.find(slope) != m.end())
                    tmp = ++m[slope];
                else
                    tmp = m[slope] = 2;
                ss = max(ss,tmp);
            }
            res = max(res,ss+sp );
        }
        return res;
    }
};
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
7 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-24 01:38 引用回復
Problem Name: Median of Two Sorted Arrays
Description:
There are two sorted arrays A and B of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
8 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-24 01:39 引用回復
Now comes the solution (in C++) ...

1. O(m+n)
2. O(log(m+n))
3. O(logm + logn)

代碼:

class Solution {
public:
    double findMedianSortedArrays(int A[], int m, int B[], int n) {
        return findMedianSortedArrays_1(A, m, B, n);
    }

    double findMedianSortedArrays_1(int A[], int m, int B[], int n) {
        int i = 0, j = 0;
        int m1 = 0, m2 = 0;
        int total = m + n;

        for (int k = 0; k <= total / 2; ++k)
        {
            int a = (i == m) ? INT_MAX : A[i];
            int b = (j == n) ? INT_MAX : B[j];

            m1 = m2;
            m2 = min(a, b);

            if (a < b) i++;
            else j++;
        }

        if (total & 0x1) return m2;
        else return (m1 + m2) / 2.0;
    }

    double findMedianSortedArrays_2(int A[], int m, int B[], int n) {
        int total = m + n;
        if (total & 0x1)
            return findKthSortedArrays(A, m, B, n, total / 2 + 1);
        else
            return (findKthSortedArrays(A, m, B, n, total / 2) + findKthSortedArrays(A, m, B, n, total / 2 + 1)) / 2;
    }

    double findKthSortedArrays(int A[], int m, int B[], int n, int k) {
        if (m > n)
            return findKthSortedArrays(B, n, A, m, k);
        if (m == 0) return B[k-1];
        if (k == 1) return min(A[0], B[0]);

        int i = min(k / 2, m);
        int j = k - i;
        int a = A[i-1];
        int b = B[j-1];

        if (a < b) return findKthSortedArrays(A + i, m - i, B, n, k - i);
        else if (a > b) return findKthSortedArrays(A, m, B + j, n - j, k - j);
        else return a;
    }

    double findMedianSortedArrays_3(int A[], int m, int B[], int n) {
        return findMedian(A, m, B, n, max(0, (m + n) / 2 - n), min(m - 1, (m + n) / 2));
    }

    double findMedian(int A[], int m, int B[], int n, int l, int r) {
        if (l > r)
            return findMedian(B, n, A, m, max(0, (m + n) / 2 - m), min(n, (m + n) / 2));

        int i = (l + r) / 2;
        int j = (m + n) / 2 - i;

        int Ai_1 = (i == 0) ? INT_MIN : A[i-1];
        int Bj_1 = (j == 0) ? INT_MIN : B[j-1];
        int Ai = (i == m) ? INT_MAX : A[i];
        int Bj = (j == n) ? INT_MAX : B[j];

        if (Ai < Bj_1) return findMedian(A, m, B, n, i+1, r);
        if (Ai > Bj) return findMedian(A, m, B, n, l, i-1);

        if ((m + n) % 2 == 1) return Ai;
        else return (Ai + max(Ai_1, Bj_1)) / 2.0;
    }
};
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
9 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)



文章 時間: 2014-9-24 01:39 引用回復
Problem Name: Merge Intervals
Description:
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].
 
花籃
分享
_________________
There is no wisdom tree; nor a stand of a mirror bright, Since all is void, where can the dust alight?
10 樓 | 返回頂端
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
 
回復主題     |##| -> |=|     論壇首頁 -> IT人生 所有的時間均為 美國太平洋時間
1頁,共5 分頁: 1, 2, 3, 4, 5  下一頁  


注:
  • 以上論壇所有發言僅代表發帖者個人觀點, 並不代表本站觀點或立場, 加西網對此不負任何責任。
  • 投資理財及買房賣房版面的帖子不構成投資建議。投資有風險,責任請自負
  • 對二手買賣中的虛假信息,買賣中的糾紛等均與本站無關。
  • 黃頁熱門商家 免費個人廣告
    發布商業廣告

    不能在本論壇發表新主題
    不能在本論壇回復主題
    不能在本論壇編輯自己的文章
    不能在本論壇刪除自己的文章
    不能在本論壇發表投票
    不能在這個論壇添加附件
    可以在這個論壇下載文件

    論壇轉跳: 

    webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver
    潛力帖子 精華帖子 熱門帖子
    性無能者的權力扮演:虐待、賴賬與...
    尹科就一詐騙犯,遲早把自己玩死!
    俄烏之戰,古巴導彈危機,和武統台灣。
    中國研究人員在2秒內將一輛噸級磁懸...
    Boxing Day 大家都買什麼好東東了?
    臉書買賣趣事
    搞事早苗沒去拜鬼
    微博網民評斬殺線
    遙想錫悅當年
    堅決不吃這個 死貓
    嘿,又到了他的誕辰紀念日了!
    香港雇傭軍死亡
    彈劾賴清德提案通過!
    為什麼日本只是服了美國,而懼怕俄...
    今天花錢如流水
    邁阿密(四)勞德代爾堡
    邁阿密(三)Key West
    一夜消失! 加拿大這家華人超市突然...
    邁阿密(二)大沼澤地 維資卡亞 海...
    邁阿密(一)南沙灘 小哈瓦那 溫伍...
    加拿大全國各地兌換紀念【無名烈士...
    2025紀念無名烈士加拿大2元流通硬幣
    自藏求精!
    西岸快線30周年紀念品
    天津深度游(二)
    天津深度游
    mint十月新幣 (十一月新幣從25樓開始)
    魁北克 水晶瀑布 加國航拍
    舌尖上的預制菜!
    游了一下多倫多(三)多倫多群島 湖...
    本宮鋼琴彈奏原聲第1彈 一首前奏曲
    謝謝管理員秉公執法廢除reddragon的id
    超級重磅!加拿大要進口中國電動車!
    皮爾今天在溫哥華 - 藍色wave - 保...
    幾分鍾前,中國強硬反擊,征34+50,...
    曼谷高樓直接倒了
    我說我希望特朗普贏,老公氣得眼睛...
    知乎?加西網上為什麼有老流氓劉廳...
    明明有能力統台,大陸為何遲遲不動手?
    貌似ndp稍占上風。。。。。
    今天是感恩節,跟大家道個別,以後...
    咱最後還是投了ndp
    生平第一次被偷車了
    中國會不會武統台灣
    突發:台灣隊戰勝中國隊奧運奪冠,...

    最新新聞 熱門新聞 熱評新聞
    BBC:默多克王朝正在如何重塑權力繼承格局
    "獨身女子逝世,買不買墓碑",是一道中國墓地倫理題
    最火的賽道,獨角獸撐不下去了?(圖
    媒體曝光後仍無解,村民視頻屢遭下架
    郭富城也是狠人 連續10年每天只吃一頓半
    174名北大學生能否考過AI?結果很意外
    紐約州流感病例激增 創單周病例數新高
    《人之初》前4集到底講了個啥故事?
    20家博物館扎堆閉館 徐湖平情人遍布各年齡段
    《老舅》大結局:華為二公主果真帶資進組了,觀眾:史上最強植入
    電視劇《反人類暴行》主創走進北大 歷史正劇"因實而新"
    眾星告別《風與潮》大結局,黃公傑氣到觀眾,喬音婉太感人
    《驕陽似我》莊序算盤打空!原來,這才是林嶼森追到長白山的真相
    中國秋糧收購超2億噸 為近年同期最高水平
    40集諜戰大劇來襲,王凱領銜,吳越祖峰加盟,可以告別劇荒了
    標普500逼近新高 美股2025年有望亮眼收官
    承諾援烏25億加元 加拿大總理卡尼會澤倫斯基
    《尚公主》後,李昀銳下一部劇被指已定,終於成為"一番"男主了
    姜昆遭同行補刀!合唱者承認在美國唱歌
    盲眼龍婆兩預言:2026中國接管台灣,普京完蛋?
    父母愛情:假如張桂蘭沒有出軌,結局會怎樣?答案一目了然
    Costco明年7項重大變革 多倫多已開始試點
    中國養老院禁忌觀察:零食、撫摸與"自由交往"
    習釋放兩個最明確信號!更大風暴即將到來
    唐詭3:女仵作首秀破奇案,宗師慘死藏玄機!
    別只炒黃金!中國真正"賺錢王牌",是這倆東西
    為什麼在"玻璃幕牆豪宅"裡,總是睡不著
    暴跌27.5%!美國碼農,正被"大屠殺"
    美國強烈反對北京因對台軍售而報復美企業和高管
    伊朗總統:美,以色列歐洲正在對伊發動"全面戰爭"
    25億!加拿大總理又宣布大手筆援烏
    張雨生的身亡,其實不是醉駕,飆車那麼簡單
    Costco柳橙為何容易發霉?外媒揭背後致命原因
    冬天常喝它?專家警告:可能更傷骨!
    姜昆視頻拍攝者露臉回應:拍於洛杉磯朋友家中,有人想陷害姜昆
    《尚公主》後,李昀銳下一部劇被指已定,終於成為"一番"男主了
    承諾援烏25億加元 加拿大總理卡尼會澤倫斯基
    盲眼龍婆兩預言:2026中國接管台灣,普京完蛋?
    父母愛情:假如張桂蘭沒有出軌,結局會怎樣?答案一目了然
    姜昆遭同行補刀!合唱者承認在美國唱歌
    Costco明年7項重大變革 多倫多已開始試點
    中國養老院禁忌觀察:零食、撫摸與"自由交往"
    習釋放兩個最明確信號!更大風暴即將到來
    唐詭3:女仵作首秀破奇案,宗師慘死藏玄機!
    別只炒黃金!中國真正"賺錢王牌",是這倆東西

    更多方式閱讀論壇:

    Android: 加西網
    [下載]

    Android: 溫哥華論壇
    [下載]

    PDA版本: 論壇

    加西網微信

    加西網微博


    Powered by phpBB 2.0.8
    Terms & Conditions    Privacy Policy    Political ADs    Activities Agreement    Contact Us    Sitemap    

    加西網為北美中文網傳媒集團旗下網站

    頁面生成: 0.0337 秒 and 6 DB Queries in 0.0013 秒