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

首頁

新聞資訊

論壇

溫哥華地產

大溫餐館點評

溫哥華汽車

溫哥華教育

黃頁/二手

旅游
搜索:  

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

論壇首頁 -> IT人生

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

分頁: 1, 2, 3 ... 11, 12, 13  下一頁  



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




文章 時間: 2014-9-23 12: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 !

第一部分 (1/5)
 
花籃
分享
_________________
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 09:57修改,總共修改了4次
樓主 | 電梯直達
閱讀會員資料 發送站內短信 主題 User photo gallery 禮物  
webdriver
(只看此人)




文章 時間: 2014-9-23 12:19 引用回復
If you can finish most of these questions you're ready for a new level ...
 
花籃
分享
_________________
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-23 12:23 引用回復
If you want to be successfully in technique interview, try solve the problem by yourself. icon_wink.gif
 
花籃
分享
_________________
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-23 12:23 引用回復
Problem Name: 3Sum

Description:

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?

Find all unique triplets in the array which gives the sum of zero.

Note:

Elements in a triplet (a,b,c) must be in non-descending order. (ie, a <= b <= c)

The solution set must not contain duplicate triplets.

For example, given array S = {-1 0 1 2 -1 -4},

A solution set is:

(-1, 0, 1)

(-1, -1, 2)
 
花籃
分享
_________________
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-23 12:25 引用回復
Now comes the solution (in C++) ...

Simplify '3sum' to '2sum' O(n^2). en.wikipedia.org/wiki/3SUM

代碼:
class Solution {
public:
    vector<vector<int> > threeSum(vector<int> &num) {
        vector<vector<int>> res;
        sort(num.begin(), num.end());
        int N = num.size();
        for (int i = 0; i < N-2 && num[i] <= 0; ++i)
        {
            if (i > 0 && num[i] == num[i-1])
                continue; // avoid duplicates
            int twosum = 0 - num[i];
            int l = i + 1, r = N - 1;
            while (l < r)
            {
                int sum = num[l] + num[r];
                if (sum < twosum)
                    l++;
                else if (sum > twosum)
                    r--;
                else {
                    vector<int> triplet;
                    triplet.push_back(num[i]);
                    triplet.push_back(num[l]);
                    triplet.push_back(num[r]);
                    res.push_back(triplet);
                    l++; r--;
                    while (l < r && num[l] == num[l-1]) l++;  // avoid duplicates
                    while (l < r && num[r] == num[r+1]) r--;  // avoid duplicates
                }
            }
        }
        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-23 12:27 引用回復
Problem Name: 3Sum Closest

Description:

Given an array S of n integers, find three integers in S such that the sum is closest to

a given number, target. Return the sum of the three integers.

You may assume that each input would have exactly one solution.

For example, given array S = {-1 2 1 -4}, and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
 
花籃
分享
_________________
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-23 12:29 引用回復
Now comes the solution (in C++) ...

Similar to 3Sum, taking O(n^2) time complexity.

代碼:
class Solution {
public:
    int threeSumClosest(vector<int> &num, int target) {
        int res = INT_MAX;
        int N = num.size();
        sort(num.begin(), num.end());
        for (int i = 0; i < N-2; ++i)
        {
            int l = i + 1, r = N - 1;
            while (l < r)
            {
                int threesum = num[i] + num[l] + num[r];
                if (threesum == target) return target;
                else if (threesum < target) l++;
                else r--;
                if (res == INT_MAX || abs(threesum - target) < abs(res - target))
                    res = threesum;
            }
        }
        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-23 12:29 引用回復
Problem Name: 4Sum

Description:

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?

Find all unique triplets in the array which gives the sum of zero.

Note:

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target?

Find all unique quadruplets in the array which gives the sum of target.

Note:

Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a <= b <= c <= d)

The solution set must not contain duplicate quadruplets.

For example, given array S = {1 0 -1 0 -2 2}, and target = 0.

A solution set is:

(-1, 0, 0, 1)

(-2, -1, 1, 2)

(-2, 0, 0, 2)
 
花籃
分享
_________________
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-23 12:30 引用回復
Now comes the solution (in C++) ...

Similar to 3Sum, 2Sum.

代碼:
class Solution {
public:
    vector<vector<int> > fourSum(vector<int> &num, int target) {
        int N = num.size();
        vector<vector<int> > res;
        if (N < 4) return res;
        sort(num.begin(), num.end());
        for (int i = 0; i < N; ++i)
        {
            if (i > 0 && num[i] == num[i-1]) continue; // avoid duplicates
            for (int j = i+1; j < N; ++j)
            {
                if (j > i+1 && num[j] == num[j-1]) continue; // avoid duplicates
                int twosum = target - num[i] - num[j];
                int l = j + 1, r = N - 1;
                while (l < r)
                {
                    int sum = num[l] + num[r];
                    if (sum == twosum) {
                        vector<int> quadruplet(4);
                        quadruplet[0] = num[i];
                        quadruplet[1] = num[j];
                        quadruplet[2] = num[l];
                        quadruplet[3] = num[r];
                        res.push_back(quadruplet);
                        while (l < r && num[l+1] == num[l]) l++; // avoid duplicates
                        while (l < r && num[r-1] == num[r]) r--; // avoid duplicates
                        l++; r--;
                    }
                    else if (sum < twosum) l++;
                    else r--;
                }
            }
        }
        return res;
    }
};
 
花籃
分享
_________________
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-23 12:31 引用回復
Problem Name: Add Binary

Description:

Given two binary strings, return their sum (also a binary string).

For example,

a = "11"

b = "1"

Return "100".
 
花籃
分享
_________________
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頁,共13 分頁: 1, 2, 3 ... 11, 12, 13  下一頁  


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

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

    論壇轉跳: 

    webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver, webdriver
    潛力帖子 精華帖子 熱門帖子
    既然現場有現成的兩個通風豎井,你...
    加國或面臨史上最慘公共服務削減
    包子靠邊站了
    一鍋“大雜燴”才叫漢族
    中國激光武器迎來了多個史詩級訂單...
    明天看煙花去
    俄軍第255摩步團近期又擴充了他們的...
    狗改不了吃屎,斯克又作死
    網上中無比安全,路不拾遺,熱心相...
    心率多少最健康?60歲後心率超過這...
    不作不死!支持亂港的“二世祖”毀...
    台灣男士們已經瞄上蒙古美女了
    中巴劃界談判,中方為何把20000平方...
    最新衛片顯示:床鋪的大坑旁邊出現...
    今年 7 月 5 日 是災難日?
    從阿壩州到甘孜州,穿越川西
    “五到八年後再看,疫情後的2023年...
    又看完一部電視劇
    新疆伊犁 賽裡木湖 三大草原恰西 喀...
    新疆阿勒泰 五彩灘 喀納斯 魔鬼城
    在烏魯木齊看娘娘騎過的汗血寶馬
    一張天主教在華發行紙鈔略考
    5月2日換幣盛況
    維達大師,另類收藏,請您欣賞!
    清代福州台伏鈔票
    四川官錢局鈔票
    大漢四川軍政府軍用銀票
    今年新幣發行計劃
    要出一個新的一元
    古董金幣
    超級重磅!加拿大要進口中國電動車!
    皮爾今天在溫哥華 - 藍色wave - 保...
    幾分鍾前,中國強硬反擊,征34+50,...
    曼谷高樓直接倒了
    我說我希望特朗普贏,老公氣得眼睛...
    知乎?加西網上為什麼有老男人喜歡...
    明明有能力統台,大陸為何遲遲不動手?
    貌似ndp稍占上風。。。。。
    今天是感恩節,跟大家道個別,以後...
    咱最後還是投了ndp
    生平第一次被偷車了
    中國會不會武統台灣
    突發:台灣隊戰勝中國隊奧運奪冠,...
    溫哥華房姐出事了
    有在看總統辯論的嗎?

    最新新聞 熱門新聞 熱評新聞
    7歲男童被長期虐待?生父被刑拘(圖
    小米新車YU7上市第二天就出事了
    泰國清萊白廟 是藝術與宗教的融合
    上海飛東京航班急墜7000米 5天兩起險情
    裝到這種程度,會不會有點太不尊重人了?
    哈薩克斯坦總統簽署法律:禁在公共場所蒙面
    休斯頓火箭休賽季操作贏麻了 成雷霆衛冕最大威脅
    專家:特朗普不太可能登上"總統山",原因是...
    奧巴馬和小布什發聲 批評解散國際開發署
    富爸爸作者最新示警:騙局將結束…
    六月中共政治局會議現三大異常(圖
    榕江洪災號召捐款,網友喊話黃楊鈿甜帶頭捐款
    警方回應"男子帶12歲女孩開房":受害者不止一人
    美議員放話:特朗普同意了,買俄油,中印加稅500%
    劇情憋屈?相比《狂飆》,《以法之名》優秀多了
    "不可能有影院肯排片"的《但願人長久》為啥能得獎?
    旅游勝地海灘喪生的加國男子是他!
    日媒:華為遭制裁後投資中國半導體廠 自建供應鏈
    豪雨來襲 四川重慶發布最高等級紅色山洪預警
    四川涼山上空現奇觀 目擊者:生平從沒見過
    蘋果首款AI眼鏡終極曝光 要引爆整個行業?
    人民幣國際支付連降3個月 排名跌至第6
    英警方對喊"以軍去死"的說唱歌手展開刑事調查
    遼寧一廢棄動物園動物無人照料 官方回應
    美國一嬰兒在母親去世後獨自在房內活數日
    全美震驚:美國男子點燃山火,槍殺2消防員
    揩油之手何以伸向民生工程的"最後一米"?
    中國機場大量沒收充電寶 放二手平台"按噸賣"
    違背承諾 中共海關被曝不放行磁鐵稀土出口
    特朗普對伊朗立場強硬: 不對話、不提供任何東西
    加國或面臨史上最慘公共服務削減
    加國政府國慶前認慫 民眾紛紛怒了
    雷根中心民調:70%美受訪者「挺出兵」協防台灣
    加國認慫 貿易談判重啟各界表歡迎
    Windows電腦用戶流失4億 原因曝光
    國慶日前夕大溫汽油價格保持低位
    壺口公驢戴套:女游客為何對驢鞭顫抖不已
    富翁流入加國降70% 不再位列八強
    真有癌症體質?來看看你是不是癌症偏愛之人
    四川涼山上空現奇觀 目擊者:生平從沒見過
    蘋果首款AI眼鏡終極曝光 要引爆整個行業?
    豪雨來襲 四川重慶發布最高等級紅色山洪預警
    日媒:華為遭制裁後投資中國半導體廠 自建供應鏈
    "不可能有影院肯排片"的《但願人長久》為啥能得獎?
    人民幣國際支付連降3個月 排名跌至第6

    更多方式閱讀論壇:

    Android: 加西網
    [下載]

    Android: 溫哥華論壇
    [下載]

    PDA版本: 論壇

    加西網微信

    加西網微博


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

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

    頁面生成: 0.0574 秒 and 5 DB Queries in 0.0015 秒