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

首頁

新聞資訊

論壇

溫哥華地產

大溫餐館點評

溫哥華汽車

溫哥華教育

黃頁/二手

旅游
搜索:  

 論壇通告:  請不要上傳第三方有版權的照片,請尊重版權,謝謝   轉載新聞請務必注明出處,這些媒體請不要轉,謝謝   批評商家需要注意  
 個人空間: NotmeL8 | 五木森林 | 羅蓬特機器人 | 一襲絳襦落鵬城,疑似玄女下九天 | XY | 白龍王許道長 | lxls | 豬頭看世界 | 顧曉軍 | 異鄉的世界 | Amy Yi | 湖裡湖塗 | 靜觀雲卷雲舒 | STEVEN90 | 亂想 | 呂洪來的個人空間 | Invisible world | 客觀中立而實事求是,唯服理據而杜絕辱罵 | 呱呱叫廚房 | 格局
 最新求助: 請問誰知道哪裡有賣理發的電動推子?   忽然有個疑問:戰爭時期,加拿大拿PR卡未入籍的永久居民會被強制服兵役嗎?   這個銀條   如何修改會員名?
 論壇轉跳:
     發帖回帖獲取加西鎊, 兌換精彩禮物

論壇首頁 -> IT人生

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

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



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




文章 時間: 2014-9-25 00:27 引用回復
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 5 (第五部分)

第一部分: www.westca.com/Forums/...inese.html
第二部分: www.westca.com/Forums/...inese.html
第三部分: www.westca.com/Forums/...inese.html
第四部分: 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:50修改,總共修改了2次
樓主 | 電梯直達
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)




文章 時間: 2014-9-25 00:29 引用回復
算法問題: Sudoku Solver
問題描述:
Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells are indicated by the character '.'.
You may assume that there will be only one unique solution.
 
花籃
分享
_________________
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-25 00:31 引用回復
推薦答案來了 (in C++) ...

back-tracking..

代碼:

class Solution {
public:
    typedef vector<vector<char> > BOARDTYPE;
   
    void solveSudoku(BOARDTYPE &board) {
        solveSudokuRe(board, 0, 0);
    }
   
    bool solveSudokuRe(BOARDTYPE &board, int row, int col) {
        getNextEmpty(board, row, col);
        if (row == 9) return true;
        vector<bool> avail(9, true);
        getAvailable(board, avail, row, col);
        for (int i = 0; i < 9; ++i)
        {
            if (!avail[i]) continue;
            board[row][col] = i + '1';
            if (solveSudokuRe(board, row, col)) return true;
        }
        board[row][col] = '.';
        return false;
    }
   
    void getNextEmpty(BOARDTYPE &board, int &row, int &col) {
        do {
            if (board[row][col] == '.') return;
            row = (col == 8) ? row + 1 : row;
            col = (col + 1) % 9;
        } while (row < 9);
    }
   
    void getAvailable(BOARDTYPE &board, vector<bool> &avail, int row, int col) {
        for (int i = 0; i < 9; ++i) {
            if (board[row][i] != '.') avail[board[row][i]-'1'] = false;
            if (board[i][col] != '.') avail[board[i][col]-'1'] = false;
            int box_i = row/3*3 + i/3, box_j = col/3*3 + i%3;
            if (board[box_i][box_j] != '.') avail[board[box_i][box_j]-'1'] = false;
        }
    }
};
 
花籃
分享
_________________
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-25 00:32 引用回復
算法問題: Sum Root to Leaf Numbers
問題描述:
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
For example,
1
/ \
2 3
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Return the sum = 12 + 13 = 25.
 
花籃
分享
_________________
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-25 00:33 引用回復
推薦答案來了 (in C++) ...

1. Recursion (add to sum when reaching the leaf).
2. Iterative solution.

代碼:

/**
 * 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 sumNumbers(TreeNode *root) {
        return sumNumbers_1(root);
    }
   
    int sumNumbers_1(TreeNode *root) {
        int sum = 0;
        sumNumbersRe(root, 0, sum);
        return sum;
    }
   
    void sumNumbersRe(TreeNode *node, int num, int &sum) {
        if (!node) return;
        num = num * 10 + node->val;
        if (!node->left && !node->right) {
            sum += num;
            return;
        }
        sumNumbersRe(node->left, num, sum);
        sumNumbersRe(node->right, num, sum);
    }
   
    int sumNumbers_2(TreeNode *root) {
        if (!root) return 0;
        int res = 0;
        queue<pair<TreeNode *, int>> q;
        q.push(make_pair(root, 0));
        while(!q.empty())
        {
            TreeNode *node = q.front().first;
            int sum = q.front().second * 10 + node->val;
            q.pop();
            if (!node->left && !node->right)
            {
                res += sum;
                continue;
            }
            if (node->left)
                q.push(make_pair(node->left, sum));
            if (node->right)
                q.push(make_pair(node->right, sum));
        }
        return res;
    }
};
 
花籃
分享
_________________
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-25 00:33 引用回復
算法問題: Surrounded Regions
問題描述:
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.
A region is captured by flipping all 'O's into 'X's in that surrounded region .
For example,
X X X X
X O O X
X X O X
X O X X
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
 
花籃
分享
_________________
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-25 00:33 引用回復
推薦答案來了 (in C++) ...

Traverse from the boarder to the inside and mark all the 'O's that are not surrounded by 'X' as 'V' (visited).
1. DFS.
2. BFS (queue).

代碼:

class Solution {
public:
    typedef vector<vector<char> > BOARDTYPE;
   
    void solve(BOARDTYPE &board) {
        if (board.empty() || board[0].empty()) return;
        int N = board.size(), M = board[0].size();
        for (int i = 0; i < N; ++i)
            for (int j = 0; j < M; ++j)
                if (i == 0 || j == 0 || i == N-1 || j == M-1)
                    bfs(board, i, j); // you may call dfs or bfs here!
        for (int i = 0; i < N; ++i)
            for (int j = 0; j < M; ++j)
                board[i][j] = (board[i][j] == 'V') ? 'O' : 'X';
    }
   
    void dfs(BOARDTYPE &board, int row, int col) {
        int N = board.size(), M = board[0].size();
        if (row < 0 || row >= N || col < 0 || col >= M) return;
        if (board[row][col] != 'O') return;
        board[row][col] = 'V';
        dfs(board, row+1, col);
        dfs(board, row-1, col);
        dfs(board, row, col+1);
        dfs(board, row, col-1);
    }

    void bfs(BOARDTYPE &board, int row, int col) {
        if (board[row][col] != 'O') return;
        int N = board.size(), M = board[0].size();
        queue<pair<int, int>> q;
        q.push(make_pair(row, col));
        while (!q.empty())
        {
            int i = q.front().first, j = q.front().second;
            q.pop();
            if (i < 0 || i >= N || j < 0 || j >= M) continue;
            if (board[i][j] != 'O') continue;// important to recheck!
            board[i][j] = 'V';
            q.push(make_pair(i-1, j));
            q.push(make_pair(i+1, j));
            q.push(make_pair(i, j-1));
            q.push(make_pair(i, j+1));
        }
    }
};
 
花籃
分享
_________________
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-25 00:33 引用回復
算法問題: Swap Nodes in Pairs
問題描述:
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
 
花籃
分享
_________________
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-25 00:34 引用回復
推薦答案來了 (in C++) ...

1. Iterative solution with constant space.
2. Recursive solution with O(n) space (for practice).

代碼:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *swapPairs(ListNode *head) {
        return swapPairs_1(head);
    }
   
    ListNode *swapPairs_1(ListNode *head) {
        ListNode dummy(0), *cur = &dummy;
        cur->next = head;
        while (cur->next && cur->next->next)
        {
            ListNode *move = cur->next->next;
            cur->next->next = move->next;
            move->next = cur->next;
            cur->next = move;
            cur = move->next;
        }
        return dummy.next;
    }
   
    ListNode *swapPairs_2(ListNode *head) {
        if (!head || !head->next) return head;
        ListNode *first = head, *second = head->next;
        first->next = second->next;
        second->next = first;
        first->next = swapPairs(first->next);
        return second;
    }
};
 
花籃
分享
_________________
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-25 00:34 引用回復
算法問題: Symmetric Tree
問題描述:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
 
花籃
分享
_________________
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頁,共6 分頁: 1, 2, 3, 4, 5, 6  下一頁  


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

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

    論壇轉跳: 

    webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver
    潛力帖子 精華帖子 熱門帖子
    第一屆內閣成員
    抬杠這個詞。。。多數是bully用
    加拿大搞自己的稀土
    得克薩斯州突發洪災。。。。
    完了!阿根廷GDP增長7%,通貨膨脹從...
    電話忙啊
    達賴曾經說過要終止轉世
    雅利安人有多強悍?如果不是商朝傾...
    日本通縮30年,他們到底靠什麼賺錢...
    義大利米蘭機場驚悚瞬間!男子闖跑...
    美國實現新自由主義之戰臨門一腳射歪了
    美媒7月6日爆料,法國情報部門的一...
    溫哥華的夏天
    咱們d省北部山火怎麼樣了?
    中國旅行團在意大利整車貴重物品被偷
    加拿大海洋三省狂奔游 (三) New Bru...
    加拿大海洋三省狂奔游(二)Nova scotia
    加拿大海洋三省狂奔游(一)PEI
    從阿壩州到甘孜州,穿越川西
    “五到八年後再看,疫情後的2023年...
    又看完一部電視劇
    新疆伊犁 賽裡木湖 三大草原恰西 喀...
    新疆阿勒泰 五彩灘 喀納斯 魔鬼城
    在烏魯木齊看娘娘騎過的汗血寶馬
    一張天主教在華發行紙鈔略考
    5月2日換幣盛況
    維達大師,另類收藏,請您欣賞!
    清代福州台伏鈔票
    四川官錢局鈔票
    大漢四川軍政府軍用銀票
    超級重磅!加拿大要進口中國電動車!
    皮爾今天在溫哥華 - 藍色wave - 保...
    幾分鍾前,中國強硬反擊,征34+50,...
    曼谷高樓直接倒了
    我說我希望特朗普贏,老公氣得眼睛...
    知乎?加西網上為什麼有老男人喜歡...
    明明有能力統台,大陸為何遲遲不動手?
    貌似ndp稍占上風。。。。。
    今天是感恩節,跟大家道個別,以後...
    咱最後還是投了ndp
    生平第一次被偷車了
    中國會不會武統台灣
    突發:台灣隊戰勝中國隊奧運奪冠,...
    溫哥華房姐出事了
    有在看總統辯論的嗎?

    最新新聞 熱門新聞 熱評新聞
    歐洲的困境:全球貿易體系劇變 深陷中美兩國夾擊
    時隔兩個多月,印度終於承認損失慘重
    全球最繁忙機場排名出爐 第一名是它
    美媒披露特朗普承諾立即供烏10枚"愛國者"導彈
    一名誤入歧途的DOGE員工發現政府已經很有效了
    一艘滾裝船在公海失火,燒出國際航運業的困境
    收費21萬!國家衛健委叫停這一手術....
    馬斯克正被趕出中國 美企在華教訓重演
    特朗普與內塔尼亞胡閉門會談,討論加沙局勢
    美中8月初重啟談判 半導體稅率最快這時揭曉
    特朗普:中國對美貿易協議中"非常公平" 良好關系
    全球第一性感女神回歸,先別急著誇
    《以法之名》江敏根本不愛洪亮!1·31案洪亮犯錯,原來是她做的局
    新劇|《掃毒風暴》《利劍·玫瑰》定檔央八
    馬斯克"鐵粉"木頭姐:特斯拉股價將翻十倍
    歐中峰會前 鐵娘子對北京火力全開
    《淬火年代》的硬核工業敘事,能否抵達"東海宇宙"?
    見證無人機時代烏俄戰爭 "願不會發生在台灣"
    最高法院裁決:為川普政府大規模裁員鋪平道路
    川普重申是時候減息 鮑威爾應"立即辭職"
    部分綠卡處理時間飆升1000% 待處理申請創歷史新高
    川普對金磚國加稅?知情人曝:采反美行動才實施
    即使工會撐腰 加國鐵飯碗照樣裁人
    溫村華人密集區大動作 增1.9萬人
    以房養財?看看房東這些血淚的教訓
    加國夫婦因屋頂裝修失敗損失48萬
    驚天巨變!沙漠種水稻 新疆糧食產量超贛遼
    創歷史新高 川普加征關稅 全民日常消費大受影響
    美軍出動300架戰機 規模之最 備戰中美沖突?
    6顆荔枝竟賣298元 超市:這是有故事的荔枝
    即使工會撐腰 加國鐵飯碗照樣裁人
    最高決策機構12成員曝光 元老重返核心層
    俄軍距扎波羅熱僅26公裡 澤連斯基急召軍會
    《碟中諜8》片方迅速變臉,阿湯哥開始丟工作了
    郭富城追子成功 激動發抖 打破四大天王岳父命
    加國夫婦因屋頂裝修失敗損失48萬
    愛潑斯坦案爛尾?FBI和美司法部否認謀殺、勒索
    6顆荔枝竟賣298元 超市:這是有故事的荔枝
    見證無人機時代烏俄戰爭 "願不會發生在台灣"
    最高法院裁決:為川普政府大規模裁員鋪平道路
    川普重申是時候減息 鮑威爾應"立即辭職"
    溫村華人密集區大動作 增1.9萬人
    部分綠卡處理時間飆升1000% 待處理申請創歷史新高
    川普對金磚國加稅?知情人曝:采反美行動才實施
    加國爆疫已造成1人死亡40多人患病

    更多方式閱讀論壇:

    Android: 加西網
    [下載]

    Android: 溫哥華論壇
    [下載]

    PDA版本: 論壇

    加西網微信

    加西網微博


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

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

    頁面生成: 0.0601 秒 and 5 DB Queries in 0.0013 秒