<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)494:目標和

          共 2420字,需瀏覽 5分鐘

           ·

          2022-01-14 01:54

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

          今天和大家聊的問題叫做?目標和,我們先來看題面:
          https://leetcode-cn.com/problems/target-sum/

          You are given an integer array nums and an integer target.


          You want to build an expression out of nums by adding one of the symbols '+' and '-' before each integer in nums and then concatenate all the integers.


          For example, if nums = [2, 1], you can add a '+' before 2 and a '-' before 1 and concatenate them to build the expression "+2-1".

          Return the number of different expressions that you can build, which evaluates to target.


          給你一個整數(shù)數(shù)組 nums 和一個整數(shù) target 。

          向數(shù)組中的每個整數(shù)前添加 '+' 或 '-' ,然后串聯(lián)起所有整數(shù),可以構(gòu)造一個 表達式 :

          例如,nums = [2, 1] ,可以在 2 之前添加 '+' ,在 1 之前添加 '-' ,然后串聯(lián)起來得到表達式 "+2-1" 。
          返回可以通過上述方法構(gòu)造的、運算結(jié)果等于 target 的不同 表達式 的數(shù)目。

          示例? ? ? ? ? ? ? ? ? ? ? ? ?

          示例 1:
          輸入:nums = [1,1,1,1,1], target = 3
          輸出:5
          解釋:一共有 5 種方法讓最終目標和為 3 。
          -1 + 1 + 1 + 1 + 1 = 3
          +1 - 1 + 1 + 1 + 1 = 3
          +1 + 1 - 1 + 1 + 1 = 3
          +1 + 1 + 1 - 1 + 1 = 3
          +1 + 1 + 1 + 1 - 1 = 3

          示例 2:
          輸入:nums = [1], target = 1
          輸出:1


          解題

          https://www.cnblogs.com/thefatcat/p/12872630.html
          思想:動態(tài)規(guī)劃
          借鑒0-1背包,使用dp[i][j]記錄選用nums中0-i個數(shù)字,組成目標為j時的方法數(shù)。
          狀態(tài)轉(zhuǎn)移方程為:dp[i][j] = dp[i-1][j-nums[i] + dp[i-1][j+nums[i]
          下表為dp[len][t]的表格,其中l(wèi)en=nums.size(),t=2*sum+1(sum為nums中所有元素的和),綠色表格為最終所求的方法數(shù)。



          class?Solution?{
          public:
          ????int?findTargetSumWays(vector<int>& nums, int?S)?{
          ????????int?sum = 0;
          ????????for(int?i=0;i????????????sum += nums[i];
          ????????if(abs(S) > abs(sum))
          ????????????return?0;
          ????????int?t = sum * 2?+ 1; //注意t的取值
          ????????int?len = nums.size();
          ????????vector<vector<int>> dp(len,vector<int>(t));
          ????????if(nums[0] == 0)
          ????????????dp[0][sum] = 2;
          ????????else{
          ????????????dp[0][sum + nums[0]] = 1;
          ????????????dp[0][sum - nums[0]] = 1;
          ????????}
          ????????for(int?i = 1;i < nums.size();i++)
          ????????????for(int?j = 0;j < t;j++){
          ????????????????int?l = (j - nums[i]) >= 0?? j-nums[i] : 0;
          ????????????????int?r = (j + nums[i]) < t ? j+nums[i] : 0;
          ????????????????dp[i][j] = dp[i-1][l] + dp[i-1][r];
          ????????????}
          ????????return?dp[nums.size()-1][sum + S];
          ????}
          };


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

          上期推文:


          LeetCode1-480題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)481:神奇字符串

          LeetCode刷題實戰(zhàn)482:密鑰格式化

          LeetCode刷題實戰(zhàn)483:最小好進制

          LeetCode刷題實戰(zhàn)484:尋找排列

          LeetCode刷題實戰(zhàn)485:最大連續(xù) 1 的個數(shù)

          LeetCode刷題實戰(zhàn)486:預(yù)測贏家

          LeetCode刷題實戰(zhàn)487:最大連續(xù)1的個數(shù) II

          LeetCode刷題實戰(zhàn)488:祖瑪游戲

          LeetCode刷題實戰(zhàn)489:掃地機器人

          LeetCode刷題實戰(zhàn)490:迷宮

          LeetCode刷題實戰(zhàn)491:遞增子序列

          LeetCode刷題實戰(zhàn)492:構(gòu)造矩形

          LeetCode刷題實戰(zhàn)493:翻轉(zhuǎn)對


          瀏覽 41
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  AⅤ在线| 午夜在线小视频 | 午夜国产视频 | 亚洲三级先锋影音 | 大黑屄|