1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實戰(zhàn)118:楊輝三角

        共 705字,需瀏覽 2分鐘

         ·

        2020-12-11 03:30

        算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

        今天和大家聊的問題叫做?楊輝三角,我們先來看題面:
        https://leetcode-cn.com/problems/pascals-triangle/

        Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.

        題意

        給定一個非負整數(shù)?numRows,生成楊輝三角的前?numRows?行。
        在楊輝三角中,每個數(shù)是它左上方和右上方的數(shù)的和。

        樣例


        輸入: 5
        輸出:
        [
        ?????[1
        ],
        ????[1,1],
        ???[1,2,1],
        ??[1,3,3,1],
        ?[1,4,6,4,1]
        ]


        解題



        class?Solution?{
        public:
        ??vector<vector<int>> generate(int?numRows) {
        ????vector<vector<int>> result;
        ????if?(numRows == 0) {
        ??????return?{};
        ????}
        ????vector<int> tempRes = { 1?};//第一行,初始行
        ????result.push_back(tempRes);
        ????for?(int?index = 2; index <= numRows; ++index) {//利用result的最后一行進行迭代
        ??????tempRes = vector<int>(index, 1);//重新設(shè)定tempRes
        ??????for?(int?i = 1; i < index - 1; ++i) {//利用上一行迭代下一行
        ????????????????//result[index - 2][i - 1]上一行的第i-1個位置,圖中的左上方
        ????????????????//result[index - 2][i]是表示上一行第i個位置,圖中的右上方
        ????????tempRes[i] = result[index - 2][i - 1] + result[index - 2][i];
        ??????}
        ??????result.push_back(tempRes);//此行迭代完畢放入結(jié)果
        ????}
        ????return?result;
        ??}
        };



        好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力。

        上期推文:
        LeetCode1-100題匯總,希望對你有點幫助!
        LeetCode刷題實戰(zhàn)101:對稱二叉樹
        LeetCode刷題實戰(zhàn)102:二叉樹的層序遍歷
        LeetCode刷題實戰(zhàn)103:二叉樹的鋸齒形層次遍歷
        LeetCode刷題實戰(zhàn)104:二叉樹的最大深度
        LeetCode刷題實戰(zhàn)105:從前序與中序遍歷序列構(gòu)造二叉樹
        LeetCode刷題實戰(zhàn)106:從中序與后序遍歷序列構(gòu)造二叉樹
        LeetCode刷題實戰(zhàn)107:二叉樹的層次遍歷 II
        LeetCode刷題實戰(zhàn)108:將有序數(shù)組轉(zhuǎn)換為二叉搜索樹
        LeetCode刷題實戰(zhàn)109:有序鏈表轉(zhuǎn)換二叉搜索樹
        LeetCode刷題實戰(zhàn)110:平衡二叉樹
        LeetCode刷題實戰(zhàn)111:二叉樹的最小深度
        LeetCode刷題實戰(zhàn)112:路徑總和

        LeetCode刷題實戰(zhàn)113:路徑總和 II

        LeetCode刷題實戰(zhàn)114:二叉樹展開為鏈表

        LeetCode刷題實戰(zhàn)115:不同的子序列

        LeetCode刷題實戰(zhàn)116:填充每個節(jié)點的下一個右側(cè)節(jié)點指針

        LeetCode刷題實戰(zhàn)117:填充每個節(jié)點的下一個右側(cè)節(jié)點指針 II



        瀏覽 43
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            豆花视频A片无码资源 | 日韩免费黄色片 | 欧洲久久电影 | 色哟哟网址 | 五月丁香激情婷婷 | 黄色一级片段 | 色视频www在线播放国产人成 | 91国自产在线观看 | gogo高清无码 | 欧美一级aaa |