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

首頁

新聞資訊

論壇

溫哥華地產

大溫餐館點評

溫哥華汽車

溫哥華教育

黃頁/二手

旅游
搜索:  

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

論壇首頁 -> IT人生

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

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



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




文章 時間: 2014-9-24 02:11 引用回復
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 3 (第三部分)

第一部分: 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-24 02:12 引用回復
Problem Name: Path Sum 2
Description:
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
 
花籃
分享
_________________
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 02:13 引用回復
Now comes the solution (in C++) ...

DFS.

代碼:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> > pathSum(TreeNode *root, int sum) {
        vector<vector<int>> res;
        vector<int> path;
        pathSumRe(root, sum, res, path);
        return res;
    }
    void pathSumRe(TreeNode *root, int sum, vector<vector<int>> &res, vector<int> &path)
    {
        if (!root) return;
        if (!root->left && !root->right)
        {
            if (sum == root->val)
            {
                path.push_back(root->val);
                res.push_back(path);
                path.pop_back();
            }
            return;
        }
        path.push_back(root->val);
        pathSumRe(root->left, sum - root->val, res, path);
        pathSumRe(root->right, sum - root->val, res, path);
        path.pop_back();
    }
};
 
花籃
分享
_________________
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 02:13 引用回復
Problem Name: Permutations
Description:
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
 
花籃
分享
_________________
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 02:14 引用回復
Now comes the solution (in C++) ...

dfs...

代碼:

class Solution {
public:
    vector<vector<int>> res;

    vector<vector<int>> permute(vector<int> &num) {
        res.clear();
        vector<bool> avail(num.size(), true);
        vector<int> pum;
        permuteRe(num, avail, pum);
        return res;
    }

    void permuteRe(const vector<int> &num, vector<bool> &avail, vector<int> &pum)
    {
        if (pum.size() == num.size())
        {
            res.push_back(pum);
            return;
        }
        for (int i = 0; i < num.size(); ++i)
        {
            if (avail[i])
            {
                avail[i] = false;
                pum.push_back(num[i]);
                permuteRe(num, avail, pum);
                pum.pop_back();
                avail[i] = true;
            }
        }
    }
};
 
花籃
分享
_________________
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 02:14 引用回復
Now comes the solution (in C++) ...

...

代碼:

class Solution {
public:
    string getPermutation(int n, int k) {
        string num, res;
        int total = 1;
        for (int i = 1; i <= n; ++i)
        {
            num.push_back(i + '0');
            total *= i;
        }
        k--;
        while (n)
        {
            total /= n;
            int i = k / total;
            k %= total;
            res.push_back(num[i]);
            num.erase(i, 1);
            n--;
        }
        return res;
    }
};
 
花籃
分享
_________________
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 02:16 引用回復
Problem Name: Permutations II
Description:
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,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-24 02:17 引用回復
Now comes the solution (in C++) ...

dfs...

代碼:

class Solution {
public:
    vector<vector<int>> res;
    vector<vector<int>> permuteUnique(vector<int> &num) {
        res.clear();
        sort(num.begin(), num.end());
        bool avail[num.size()];
        memset(avail, true, sizeof(avail));
        vector<int> pum;
        permuteUniqueRe(num, pum, avail);
        return res;
    }

    void permuteUniqueRe(vector<int> &num, vector<int> &pum, bool avail[])
    {
        if (pum.size() == num.size())
        {
            res.push_back(pum);
            return;
        }
        int last_index = -1;
        for (int i = 0; i < num.size(); ++i)
        {
            if (!avail[i]) continue;
            if (last_index != -1 && num[i] == num[last_index]) continue;
           
            avail[i] = false;
            pum.push_back(num[i]);
            permuteUniqueRe(num, pum, avail);
            pum.pop_back();
            avail[i] = true;
            last_index = i;
        }
    }
};
 
花籃
分享
_________________
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 02:17 引用回復
Problem Name: Plus One
Description:
Given a number represented as an array of digits, plus one to the number.
 
花籃
分享
_________________
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 02:18 引用回復
Now comes the solution (in C++) ...

...

代碼:

class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        int carry = 1, N = digits.size();
        for (int i = N-1; i >= 0 && carry; --i)
        {
            int sum = carry + digits[i];
            carry = sum / 10;
            digits[i] = sum % 10;
        }
        if (carry == 1)
            digits.insert(digits.begin(), 1);
        return digits;
    }
};
 
花籃
分享
_________________
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
    潛力帖子 精華帖子 熱門帖子
    紅場閱兵大陸儀仗登熱搜,普京朋友...
    習近平亂扯二戰歷史被台灣打臉;
    印巴空戰最新消息解讀!中國軍備在...
    莫斯科衛國戰爭勝利80周年閱兵實況...
    應該軍事雜志介紹一下
    中俄聯合聲明
    殲十多了個名號
    的確不太好,但是挺逗的!
    中國高鐵牛啊
    飯都快吃不起了
    個人認為很危險的想法
    巴印空戰0比6之啟示
    陰雨天回來了
    今天看眼醫
    從發型看女人
    5月2日換幣盛況
    維達大師,另類收藏,請您欣賞!
    清代福州台伏鈔票
    四川官錢局鈔票
    大漢四川軍政府軍用銀票
    今年新幣發行計劃
    要出一個新的一元
    古董金幣
    mint三月新幣(四月新幣從22樓起,五...
    1999 mule 25分
    2025 蛇年敲幣活動
    加拿大新總理馬克卡尼
    我在小紅書被罵窮得沒錢給孩子買衣服
    美國2025年AWQ(美國婦女25c)發行計劃
    韓國空難FDR黑匣子缺失最後四分鍾關...
    皮爾今天在溫哥華 - 藍色wave - 保...
    幾分鍾前,中國強硬反擊,征34+50,...
    曼谷高樓直接倒了
    我說我希望特朗普贏,老公氣得眼睛...
    知乎?加西網上為什麼有老男人喜歡...
    明明有能力統台,大陸為何遲遲不動手?
    貌似ndp稍占上風。。。。。
    今天是感恩節,跟大家道個別,以後...
    咱最後還是投了ndp
    生平第一次被偷車了
    中國會不會武統台灣
    突發:台灣隊戰勝中國隊奧運奪冠,...
    溫哥華房姐出事了
    有在看總統辯論的嗎?
    退休幾年後的感悟

    最新新聞 熱門新聞 熱評新聞
    海南頂奢酒店,被保潔"外包"毀掉
    全美高校被AI羞恥攻陷:AI必將殺死人文學科
    新任教皇:兩次罵萬斯,一次罵川普
    中美貿易談判前,北京突然出手警告川普
    超糗:情侶海裡愛愛 竟然分不開(圖
    第2個?美商務部長曝這國接棒達成貿易協議
    溫村著名房地產公司Rennie裁31人
    二戰結束80年 "台灣籍日本兵"的雙重身份認同之爭
    克宮:普京川普透過幕僚相互賀 金正恩赴俄使館道賀
    美國上空出現神秘黑圓環 居民:從來沒見過
    溫市居民最近去美國感到不受歡迎
    加州兩大煉油廠宣布關閉 真要全民換電車?
    點名中俄朝 美海軍中將:10年內造新型核導彈
    中國人造太陽發源地已建起中國核聚變博物館
    西瓜甜嗎?中國鑒瓜師3秒判別 收入驚呆網友
    她曾是"中國第一美婦",受克林頓接見,坐過牢
    49歲素顏殺瘋同學會 蔣勤勤憑啥美成時光刺客?
    美軍從越南撤退50年後,中國已"接管"亞洲
    日本秩父春景如畫 徜徉於叢生福祿考花海之中
    鍾楚曦太會穿了 白色吊帶裙下面不穿喇叭褲
    章澤天帶9歲女兒參加音樂節 被誇贊親切又溫柔
    波蘭前模特兒索科拉作證 16歲遭哈維溫斯坦性侵
    重慶大學本科生14篇SCI論文受質疑
    X平台:印度要求封禁8000多個當地賬戶,否則重罰
    松下擬全球裁員1萬人 海外裁員人數占5000人
    "挺陸配"與"護兒"活動10日齊登場 網紅專家都示
    室友電池爆炸燒傷的大學生,絕望深淵裡唱歌
    影後金雅琴患雙癌離世:獨生女兒4婚4離,她氣...
    副總統萬斯:印巴兩國沖突 "根本上不關我們的事"
    特朗普:鮑威爾不降息,可能是因為"他不愛我"
    "疫苗導致的死亡海嘯已經來臨" 中國博主發文
    加國消減留學名額 院校五千人失業
    壓軸登場、長長紅地毯....習在莫斯科享盡榮耀
    英媒:比黃金還珍貴的月塵從中國運抵英國
    租了11年的房子要賣 大溫女子忠告
    "太濕了先暫停一下" 人妻偷吃小王簡訊被發現
    加拿大的國會要重開 這個黨尷尬了
    王丹:中國為何這麼快就與美談判?
    中國體制內人員,欠薪風暴越刮越猛
    紀念蘇聯偉大衛國戰爭勝利80周年,紀念的是什麼?
    加拿大皇家馬戲團素裡列治文開演
    中國憂心"2下場"!選擇對美讓步(圖
    中國"仍然占上風" 語氣相當強硬
    多個大國無視中共紅線! 北京體面遭踐踏
    獨家報道傳 川普最快下周將中國關稅降至…

    更多方式閱讀論壇:

    Android: 加西網
    [下載]

    Android: 溫哥華論壇
    [下載]

    PDA版本: 論壇

    加西網微信

    加西網微博


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

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

    頁面生成: 0.0634 秒 and 11 DB Queries in 0.0047 秒