涓€鍜岄浂锛圠eetCode-474锛夈€?/span>        缁欏畾m涓?"/>

绠楁硶鍔ㄦ€佽鍒掞紙浜旓級-鑳屽寘闂2

Posted Coding鐨勫摂鍝斿彣鍙?/a>

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绠楁硶鍔ㄦ€佽鍒掞紙浜旓級-鑳屽寘闂2相关的知识,希望对你有一定的参考价值。

        鈿狅笍浠婂ぉ缁х画鎴戜滑鐨?-1鑳屽寘闂銆?/span>

        馃憠馃憖馃挰浠婃棩缁冧範锛堜竴锛?span class="mq-9">涓€鍜岄浂锛圠eetCode-474锛夈€?/span>

        缁欏畾m涓?銆乶涓?鍜屼竴涓彧鐢?銆?鏋勬垚瀛楃涓茬殑鏁扮粍锛屼娇鐢ㄧ粰瀹氱殑m涓?鍜宯涓?鎷兼垚鏁扮粍涓殑瀛楃涓茬殑鏈€澶ф暟閲忥紝姣忎釜0鍜?鏈€澶氫娇鐢ㄤ竴娆?/span>銆備妇渚嬶細

杈撳叆: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3杈撳嚭: 4瑙i噴: 鎬诲叡 4 涓瓧绗︿覆鍙互閫氳繃 5 涓?nbsp;0 鍜?nbsp;3 涓?nbsp;1 鎷煎嚭锛屽嵆 "10","0001","1","0" 銆?/span>
杈撳叆: Array = {"10", "0", "1"}, m = 1, n = 1杈撳嚭: 2瑙i噴: 鎴戜滑鍙互鎷煎嚭 "10"锛屼絾涔嬪悗灏辨病鏈夊墿浣欐暟瀛椾簡銆傛洿濂界殑閫夋嫨鏄嫾鍑?nbsp;"0" 鍜?nbsp;"1" 銆?/span>

馃檱鎬濊矾锛?/span>

        姝ら渚濈劧鏄?-1鑳屽寘闂銆?br>

鐘舵€佸畾涔夛細dp[i][j][k]琛ㄧず锛屽瓧绗︿覆鏁扮粍[0 , i]鍖洪棿鍐呯殑瀛楃涓诧紝鍙互鐢╦涓?銆乲涓?琛ㄧず鐨勬渶澶ф暟閲忋€?/span>

杞€佽浆绉?/span>锛氭暟缁勪腑绗琲涓厓绱犻€夋嫨鎴栦笉閫夋嫨涓ょ鐘舵€併€?/span>

  1. 涓嶉€夈€?span class="mq-32">dp[i][j][k] = dp[i-1][j][k] 鍓峣-1涓暟缁勫厓绱犲彲浠ョ敤j銆乲涓?鎴?鍙互琛ㄧず鐨勬渶澶ф暟閲忋€?/p>

  2. 閫夋嫨銆傜敤cn[]鏁扮粍鏉ヨ〃绀虹i涓瓧绗︿覆涓?銆?鐨勪釜鏁帮紝鐘舵€佽浆绉绘柟绋嬩负锛?span class="mq-49">dp[i][j][k] = dp[i-1][j-cn[0]][k-cn[1]] + 1 鍓峣-1涓暟缁勫厓绱狅紝鍙兘鐢ㄥ墿涓嬬殑(j-cn[0])涓?鍜?span class="mq-68">(k-cn[1])涓?鍙互琛ㄧず鍑虹殑鏈€澶ф暟閲忋€?/p>

  3. 缁煎悎锛屾渶鍚庣殑鐘舵€佽浆绉绘柟绋嬭〃绀轰负锛?br>dp[i][j][k] = max(dp[i-1][j][k], dp[i-1][j-cn[0]][k-cn[1]] + 1)

鏈€缁堣緭鍑?/span>锛歞p[len-1][m][n]

浠g爜锛?br>

public int findMaxForm(String[] strs, int m, int n) { int len = strs.length;    int[][][] dp = new int[len+1][m+1][n+1]; // i 浠?寮€濮嬶紝淇濊瘉瑙勫垝涓笂琛屾槸瀛樺湪鐨勩€?/span> for(int i=1;i<=len;i++){ int[] cn = count(strs[i-1]); for(int j=0;j<=m;j++){ for(int k=0;k<=n;k++){ // 涓嶈涔嬪悗鐨勫垎鏋愭槸鍟ワ紝鑷冲皯鏄笂涓€琛岀殑澶撮《鐨勮繖涓暟閲忋€?/span> dp[i][j][k] = dp[i-1][j][k]; if(j>=cn[0] && k>=cn[1]){ dp[i][j][k]=Math.max(dp[i-1][j][k],dp[i-1][j-cn[0]][k-cn[1]]+1); } } } } return dp[len][m][n];}private int[] count(String str){ int[] cn = new int[2]; for(char c:str.toCharArray()){ cn[c-'0']++; } return cn;}

馃檵绌洪棿浼樺寲

馃檱鎬濊矾锛?/span>

       鍦ㄤ笂闈㈣В娉曚腑锛屾垜浠緷鏃ф槸鍙弬鑰冧簡褰撳墠琛岀殑涓婁竴琛岋紝鎵€浠ヨ繖鍎挎垜浠浜庣┖闂寸殑浼樺寲灏辨槸涓€涓€濊矾闄嶇淮銆傞噰鐢ㄤ簩缁存暟缁勬潵杩涜閫嗗悜濉〃銆?/span>

浠g爜锛?/span>

public int findMaxForm(String[] strs, int m, int n) { int len = strs.length;    int[][] dp = new int[m+1][n+1]; // i 浠?寮€濮嬶紝淇濊瘉瑙勫垝涓笂琛屾槸瀛樺湪鐨勩€?/span> for(int i=1;i<=len;i++){ int[] cn = count(strs[i-1]); for(int j=m;j>=cn[0];j--){ for(int k=n;k>=cn[1];k--){ dp[j][k]= Math.max(dp[j][k],dp[j-cn[0]][k-cn[1]]+1); } } } return dp[m][n];}private int[] count(String str){ int[] cn = new int[2]; for(char c:str.toCharArray()){ cn[c-'0']++; } return cn;}


      馃憠馃憖馃挰浠婃棩缁冧範锛堜簩锛?span>鐩爣鍜?/strong>锛圠eetCode-494锛夈€?/span>

        缁欏畾涓€涓潪璐熸暣鏁版暟缁刵ums锛屽拰涓€涓洰鏍囨暟S锛屾暟缁勪腑姣忎竴涓暟鍓嶉兘鍙互娣诲姞+鎴?绗﹀彿锛屾坊鍔犲畬鍚庝娇寰楁暟缁勭殑鍜屼负S锛屾壘鍑哄彲浠ユ坊鍔犵鍙风殑缁勫悎鏁般€?/span>涓句緥锛?/span>

杈撳叆锛?span class="code-snippet__selector-tag">nums[1,2,3,4,5] S=3杈撳嚭锛?瑙i噴锛?/span>鍙互娣诲姞绗﹀彿鐨勬儏鍐垫湁浠ヤ笅3绉?/span>-1+2+3+4-5=3-1-2-3+4+5=31-2+3-4+5=3
dp[i][j]=dp[i-1][j-nums[i]]+dp[i-1][j+nums[i]]

馃檱鎬濊矾锛?/span>

閿欒鎬濊矾锛?br>

        涓€寮€濮嬫垜鐨勬€濊矾鏄i涓暟閫夋嫨-鎴栬€呴€夋嫨+锛岀姸鎬佹柟绋嬩负锛?span class="mq-134">dp[i][j]=dp[i-1][j-nums[i]]+dp[i-1][j+nums[i]]涓嶉毦鐪嬪嚭锛岀姸鎬佹柟绋嬩腑j+nums[i]鐨勮竟鐣屾垜浠棤娉曡幏鐭ヤ簡銆?/span>

姝g‘鐨勬€濊矾锛?br>

        浠旂粏鐪侀锛屾垜浠笉闅惧彂鐜伴鐩腑鏃犻潪灏辨槸鏈塵涓鏁板姞n涓礋鏁帮紝鏈€鍚庣殑缁撴灉涓篠锛岃繖閬撻鍏跺疄鍜屾垜浠箣鍓嶅仛杩囩殑鍒嗗壊绛夊拰瀛愰泦锛圠eetCode-416锛?/span>寰堝儚銆?16鏄鎵惧嚭涓や釜绛夊拰瀛愰泦锛岃€岃繖閬撻鎹釜鎬濊矾灏辨槸锛屾壘鍑轰袱涓瓙闆嗕娇寰楀拰涓篠銆傛墍浠ワ紝杩欏効鎴戜滑灏嗙鍙蜂负+鐨勫閲忓畾涓篨锛岀鍙蜂负璐熺殑瀹归噺瀹氫负Y锛屾暟缁勭殑鎬诲拰涓簊um锛屾垜浠彲浠ュ緱鍒?/span>X+Y=sum锛沊-Y=S銆傚熀浜庤繖涓や釜绛夊紡锛屾垜浠彲浠ュ緱鍒癤=(S+sum)/2锛涗笖S+sum蹇呴』涓哄伓鏁般€?/span>

        閫氳繃棰樼洰鐨勬€濊矾杞崲锛屾渶鍚庢垜浠氨灏嗘湰棰樿浆鎹㈡垚涓€涓畝鍗曠殑0-1鑳屽寘闂锛屽嵆浠庢暟缁刵ums涓壘鍑轰竴浜涙暟锛屼娇寰楀拰涓篨锛屾壘鍑烘墍鏈夌殑缁勫悎鏂瑰紡銆?br>

鐘舵€佸畾涔夛細dp[i][j]琛ㄧず锛屾暟缁刐0 , i]鍖洪棿鍐呭彲浠ユ壘鍒板拰涓簀鐨勭粍鍚堟暟銆?/span>

杞€佽浆绉?/span>锛氭暟缁勪腑绗琲涓厓绱犻€夋嫨鎴栦笉閫夋嫨涓ょ鐘舵€併€?/span>

  1. 涓嶉€夈€?span class="mq-163">dp[i][j] = dp[i-1][j]鍓峣-1涓暟鍙互鎵惧埌鍜屼负j鐨勬暟閲忋€?/p>

  2. 閫夋嫨銆?span class="mq-175">dp[i][j] = dp[i-1][j-nums[i]]銆傚墠i-1涓暟鍙互鎵惧埌鍜屼负(j-nums[i])鐨勬暟閲忋€?/p>

  3. 缁煎悎锛屾渶鍚庣殑鐘舵€佽浆绉绘柟绋嬭〃绀轰负锛?br>dp[i][j] = dp[i-1][j]+dp[i-1][j-nums[i]]銆?/span>

鏈€缁堣緭鍑?/span>锛歞p[len][X]銆?/span>

浠g爜锛堟垜浠繖鍎垮氨鐩存帴灏遍€夋嫨绌洪棿浼樺寲鐨勫仛娉曪級锛?/span>

public int findTargetSumWays(int[] nums, int S) { int len = nums.length; int sum = 0; for(int num:nums){ sum+=num; } if(((S+sum)&1) == 1){ return 0; } // 鐩爣鍜屼笉鍙兘澶т簬鎬诲拰 if (S>sum){ return 0; } int X = (S+sum)/2; int[] dp = new int[X+1]; dp[0]=1; for(int i=0;i<len;i++){ for(int j=X;j>=nums[i];j--){ dp[j]+=dp[j-nums[i]]; } } return dp[X];}

绠楁硶鍔ㄦ€佽鍒掞紙浜旓級-鑳屽寘闂2涓嶇Н璺锛屾棤浠ヨ嚦鍗冮噷銆?/strong>

鏂囩珷鏈夊府鍔╃殑璇濓紝鐐逛釜杞彂銆佸湪鐪嬪憲绠楁硶鍔ㄦ€佽鍒掞紙浜旓級-鑳屽寘闂2銆?/strong>

璋㈣阿鏀寔鍝?(*^__^*)

END


绠楁硶鍔ㄦ€佽鍒掞紙浜旓級-鑳屽寘闂2馃憞

以上是关于绠楁硶鍔ㄦ€佽鍒掞紙浜旓級-鑳屽寘闂2的主要内容,如果未能解决你的问题,请参考以下文章

绠楁硶-鍔ㄦ€佽鍒掞紙鍥涳級

绠楁硶-鍔ㄦ€佽鍒掞紙浜岋級

绠楁硶-鍔ㄦ€佽鍒掞紙涓夛級

鍔ㄦ€佽鍒掕繘闃剁瘒1---鑳屽寘闂

鍔ㄦ€佽鍒掞紙浜岋級涔嬫墦瀹跺姭鑸?/h1>

濡備綍鍚戜竴涓洓宀佸皬瀛╄В閲婂姩鎬佽鍒掞紵