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

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

tags:

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

        鈿狅笍浠婂ぉ寰€鍚庣殑鍑犵瘒鏂囩珷锛屾垜浠細璁ㄨ鍔ㄦ€佽鍒掗噷涓€涓粡鍏哥殑闂銆愯儗鍖呴棶棰樸€戯紝鏈夊叧鑳屽寘闂锛屽笇鏈涘ぇ瀹跺彲浠ヨ嚜琛岀櫨搴︿竴涓嬨€?/span>鑳屽寘涔濋棶銆戝幓浜嗚В鎬濊€冧竴涓嬶紝娑夊強鍏朵腑鐨勮儗鍖呫€佸閲忕瓑鐨勬蹇甸棶棰橈紝鍦ㄧ郴鍒楁枃绔犻噷涓嶄細鍐欙紝鎵€浠ヨ繕鏄笇鏈涘ぇ瀹跺厛鍘讳簡瑙d竴涓嬭繖浜涙蹇点€?/span>

        馃憠馃憖馃挰浠婃棩缁冧範锛堜竴锛?span class="mq-9">鍒嗗壊绛夊拰瀛愰泦锛圠eetCode-416锛夈€?/span>

        缁欏畾涓€涓?/span>鍙寘鍚鏁存暟鐨勯潪绌烘暟缁勩€傛槸鍚﹀彲浠ュ皢杩欎釜鏁扮粍鍒嗗壊鎴愪袱涓瓙闆嗭紝浣垮緱涓や釜瀛愰泦鐨勫厓绱犲拰鐩哥瓑銆備妇渚嬶細

杈撳叆: [1, 5, 11, 5]杈撳嚭: true瑙i噴: 鏁扮粍鍙互鍒嗗壊鎴?[1, 5, 5] 鍜?[11].
杈撳叆: [1, 2, 3, 5]杈撳嚭: false瑙i噴: 鏁扮粍涓嶈兘鍒嗗壊鎴愪袱涓厓绱犲拰鐩哥瓑鐨勫瓙闆?

馃檵瑙f硶涓€锛?/span>

馃檱鎬濊矾锛?/span>

        鎴戜滑鍏堝杩欎釜闂杩涜涓€涓嬬瓑浠疯浆鎹細鏁扮粍瑕佸垎鍓叉垚涓や釜绛夊拰瀛愰泦锛岄偅涔堣繖涓暟缁勪竴瀹氬叿鏈?/span>浠?span class="mq-26">涓嬬壒鐐癸細

  1. 鏁扮粍鐨勬€诲拰涓哄伓鏁帮紝濡傛灉鎬诲拰涓哄鏁帮紝鎴戜滑涔熷氨娌″繀瑕佸啀寰€涓嬭绠椾簡銆?/span>

  2. 涓や釜瀛愰泦鐨勫拰涓€瀹氱瓑浜庢暟缁勬€诲拰鐨勪竴鍗娿€?/span>

        鍩轰簬涓婇潰涓や釜鐗圭偣锛屾垜浠杩欓亾棰樹篃灏辩畻鏄渶鍩烘湰鐨勬€濊矾鎵撳紑浜嗭紝涓嬮潰灏辨槸0-1鑳屽寘闂鐨勫垎鏋愪簡銆?br>

        0-1鑳屽寘闂涓紝鏁扮粍涓殑銆屾瘡涓暟鍙兘鍙栦竴娆°€嶃€傛墍浠ユ暣浣撴€濊矾灏辨槸锛屼竴涓竴涓殑鍙栨暟锛屼竴鐐逛竴鐐圭殑澧炲姞瀹归噺銆?/span>

        瀹氫箟鏁扮粍鐨勯暱搴︿负len锛屾暟缁勬€诲拰鐨勪竴鍗婁负target锛屾棰樺叿浣撳仛娉曞涓嬶細鎴戜滑瀹氫箟涓€涓涓簂en锛屽垪涓簍arget+1鐨勪簩缁存暟缁刣p锛?/span>

鐘舵€佸畾涔夛細dp[i][j]琛ㄧず锛屾垜浠彲浠ヤ粠0~i鍖洪棿鍐呴€夋嫨涓€浜涙暟浣垮緱杩欎簺鏁扮殑鍜屾伆绛変簬j銆?br>

杞€佽浆绉?/span>锛?br>

  1. 褰撻€夋嫨nums[i]鏃讹紝鍦╗0 , i-1]杩欎釜鍖洪棿鍐呰兘鎵惧埌涓€浜涙暟锛岃繖浜涙暟鐨勫拰涓簀-nums[i]锛岃繖鏃禿p[i][j]=dp[i-1][j-nums[i]]銆?/span>

  2. 褰撲笉閫夋嫨nums[i]鏃讹紝鍦╗0 , i-1]杩欎釜鍖洪棿鍐呭凡缁忔湁涓€浜涙暟锛岃繖浜涙暟鐨勫拰涓簀锛岃繖鏃禿p[i][j] = true銆?br>

姝ら涓殑鐘舵€佽浆绉绘柟绋嬶細

dp[i][j]=dp[i-1][j] || dp[i-1][j-nums[i]]

缁х画鍒嗘瀽锛屾湁浜嗘暟缁勶紝閭d箞鏁扮粍涓嬫爣涓€瀹氬緱澶т簬0锛涙墍浠-nums[i]>0锛宯ums[i]<=j銆傚綋nums[i]=j鏃讹紝dp[i][j]=true銆?/p>

浠g爜锛?br>

public boolean canPartition(int[] nums) { int len= nums.length; if(len<1){ return false;        } int sum=0; for(int num:nums){ sum+=num; } //sum涓哄鏁拌繑鍥瀎alse銆?/span> if((sum &1)==1){ return false; } int target = sum/2; boolean[][] dp = new boolean[len][target+1]; if(nums[0]<target){ dp[0][nums[0]]=true; } for(int i=1;i<len;i++){            for(int j=0;j<=target;j++){                dp[i][j]=dp[i-1][j]; if(nums[i]==j){ dp[i][j]=true; continue; } if(nums[i]<j){ dp[i][j] = dp[i-1][j]||dp[i-1][j-nums[i]]; } } } return dp[len-1][target];}

澶嶆潅搴﹀垎鏋?/span>

  • 鏃堕棿澶嶆潅搴︼細O(NC)锛氳繖閲孨 鏄暟缁勫厓绱犵殑涓暟锛孋 鏄暟缁勫厓绱犵殑鍜岀殑涓€鍗娿€?/span>

  • 绌洪棿澶嶆潅搴︼細O(NC)銆?/span>


馃檵瑙f硶浜岋細瀵逛笂闈㈣В娉曚竴杩涜浼樺寲銆?/p>

馃檱鎬濊矾锛?/span>

        鍦ㄤ笂闈㈣В娉曚竴涓紝鏍规嵁鐘舵€佸畾涔夋垜浠彲浠ュ緱鍒癲p[0][0]=false;鍥犱负鎴戜滑鎵句笉鍒颁竴涓鏁存暟鍙互浣垮叾绛変簬0銆?/p>

  1. 浣嗘槸褰搄==nums[i]鏃讹紝j-nums[i]==0鏄垚绔嬬殑锛宯ums[i]杩欎釜鏁板彲浠ヨ鍗曠嫭鍒掑垎涓轰竴涓瓙闆嗐€傚洜姝ゆ垜浠妸dp[0][0]璁句负true涔熸槸鍚堢悊銆傝櫧鐒朵笉绗﹀悎鐘舵€佸畾涔夛紝浣嗘槸浠庣姸鎬佽浆绉绘潵璇存槸瀹屽叏绗﹀悎鐨勩€?/p>

  2. 鏍规嵁鐘舵€佽浆绉绘柟绋嬶紝dp[i][j]=dp[i-1][j] || dp[i-1][j-nums[i]]锛屾垜浠彲浠ュ緱鍒帮紝褰撲簩缁存暟缁刣p鐨勬渶鍚庝竴鍒梔p[i][target]涓簍rue鏃讹紝褰撳墠鍒椾笅闈㈢殑鐨勫€间竴瀹氶兘鏄痶rue锛岃繖鏃跺€欐垜浠氨鍙互鎻愬墠缁撴潫閬嶅巻浜嗭紝鐩存帴杩斿洖true銆?/span>

浠g爜锛?br>

public boolean canPartition_1(int[] nums) { int len = nums.length; if (len < 1) { return false;        } int sum = 0; for (int num : nums) { sum += num; } //sum涓哄鏁拌繑鍥瀎alse銆?/span> if ((sum & 1) == 1) { return false; } int target = sum / 2; boolean[][] dp = new boolean[len][target + 1];//鍒濆鍖栨垚涓?true 铏界劧涓嶇鍚堢姸鎬佸畾涔夛紝浣嗘槸浠庣姸鎬佽浆绉绘潵璇存槸瀹屽叏鍙互鐨?/span>    dp[0][0] = true; if (nums[0] == target) { dp[0][nums[0]] = true;    } for (int i = 1; i < len; i++) { for (int j = 0; j <= target; j++) { dp[i][j] = dp[i - 1][j];            //鍥犱负鏈塪p[0][0]=true鐨剄鍓嶆彁锛屾墍浠ヨ繖鍎垮彲浠ュ皢瑙f硶涓€涓?/span>            //<=鐨剄鎯呭喌鍚堣捣鏉ュ啓锛屼笉蹇呭垎寮€鍒ゆ柇銆?/span> if (nums[i] <= j) { dp[i][j] = dp[i - 1][j] || dp[i - 1][j - nums[i]]; } }        //鏍规嵁鎴戜滑涓婇潰鐨勫垎鏋愶紝杩欏効鍙互鎻愬墠缁撴潫銆?nbsp;     if(dp[i][target]){ return true; } } return dp[len-1][target];}


馃檵瑙f硶涓夛細瀵硅繘琛屼袱涓В娉曠殑绌洪棿浼樺寲銆?/p>

馃檱鎬濊矾锛?/span>

        涓€鑸儏鍐典笅瀵?-1鑳屽寘闂鐨勭┖闂翠紭鍖栧氨鏄皢浜岀淮鏁扮粍闄嶇淮鎴愪竴浣嶆暟缁勩€?/span>

       鎴戜滑鍩轰簬瀵逛笂闈袱涓В娉曠殑鎬濊矾锛屽仛涓€浜涙€濊€冿細

  1.  涓婇潰瑙f硶涓紝鎴戜滑姣忔鐨勫惊鐜噷閮藉彧鐢ㄥ埌浜嗕笂闈竴琛岀殑鏁版嵁锛岃€屼笖鍙敤鍒颁簡涓婇潰涓€琛屽悓涓€鍒楃殑鏁版嵁鍜屼笂闈竴琛屽乏杈规煇涓綅缃殑鏁版嵁銆?/p>

  2. 鎴戜滑閲囩敤銆屾粴鍔ㄦ暟缁勩€嶇殑鏂瑰紡鏉ヨ繘琛屻€屽~琛ㄣ€嶃€?/p>

  3. 杩欏効鎴戜滑瀹氫箟涓€涓竴缁存暟缁刣p锛岄暱搴︿负target+1锛屼粠鍚庡悜鍓嶄緷娆″~琛ㄣ€?/p>

  4. 浠庡悗鍚戝墠濉〃鐨勮繃绋嬶紝濡傛灉纰板埌涓嶆弧瓒砵>=nums[i]锛屼篃灏变笉蹇呰缁х画杩涜浜嗭紝鍙互椹笂閫€鍑哄惊鐜紝鍥犱负瑕佷繚璇乨p[j-nums[i]]涓嬫爣鍚堟硶銆?/p>

绠楁硶-鍔ㄦ€佽鍒掞紙鍥涳級鎬濊€冿細涓轰粈涔堣浠庡悗鍚戝墠閫嗗簭濉〃锛?/span>

        杩欐槸鍥犱负鍦ㄤ竴缁寸殑鎯呭喌涓嬶紝鏍规嵁dp[j] || dp[j-nums[i]]鏉ユ帹dp[j]鐨勫€硷紝鎴戜滑闇€瑕佷繚璇佹暟缁勪腑鍓嶉潰鐨勫€间笉鑳藉鍚庨潰鐨勫€兼湁褰卞搷锛屼妇涓緥瀛愶紝缁欏畾鏁扮粍骞堕噰鐢ㄩ噰鐢ㄦ搴忓~琛細

[2,2,3,5]锛宼arget=6锛?/span>
nums[0]<= 6 ; dp[2]=True褰搃=0, j=4鏃讹紝dp[4] = dp[4] || dp[2] =True;i涓嶅彉锛岃繖鏃剁户缁悜鍚庨亶鍘嗭紝j=6鏃讹紝dp[6] = dp[6]||dp[6-2]= True;杩欐樉鐒跺氨宸茬粡閿欎簡銆俤p涓璹鍓嶉潰鐨勫€煎奖鍝嶄簡鍚庤竟鐨勫€笺€?/span>

鎵€浠ワ紝鍦ㄨ繖鍎挎垜瑕侀噰鐢ㄩ€嗗簭濉〃锛屾秷闄ゅ悓涓€琛屽墠闈㈠鍚庨潰浜х敓鐨勫奖鍝嶃€?br>

浠g爜锛?/p>

public boolean canPartition_2(int[] nums) { int len = nums.length; if (len < 1) { return false;    } int sum = 0; for (int num : nums) { sum += num; } //sum涓哄鏁拌繑鍥瀎alse銆?/span> if ((sum & 1) == 1) { return false; } int target = sum / 2;    boolean[] dp = new boolean[target + 1];    dp[0] = true; if (nums[0] <= target) { dp[nums[0]] = true;    } for (int i = 1; i < len; i++) {        for (int j = target; j >= nums[0]; j--) { if(dp[target]){ return true; } dp[j]=dp[j]||dp[j-nums[i]]; } } return dp[target];}

澶嶆潅搴﹀垎鏋?/span>

  • 鏃堕棿澶嶆潅搴︼細O(NC)锛氳繖閲孨 鏄暟缁勫厓绱犵殑涓暟锛孋 鏄暟缁勫厓绱犵殑鍜岀殑涓€鍗娿€?/span>

  • 绌洪棿澶嶆潅搴︼細O(C)銆?/span>


绠楁硶-鍔ㄦ€佽鍒掞紙鍥涳級涓嶇Н璺锛屾棤浠ヨ嚦鍗冮噷銆?/strong>

鏂囩珷鏈夊府鍔╃殑璇濓紝鐐逛釜杞彂銆佸湪鐪嬪憲绠楁硶-鍔ㄦ€佽鍒掞紙鍥涳級銆?/strong>

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

END


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

以上是关于绠楁硶-鍔ㄦ€佽鍒掞紙鍥涳級的主要内容,如果未能解决你的问题,请参考以下文章

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

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

绠楁硶鍔ㄦ€佽鍒掞紙涓冿級-鑳屽寘闂4

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

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

Jmeter涔婬TTP璇锋眰榛樿鍊煎拰鏌ョ湅缁撴灉鏍戜娇鐢ㄤ粙缁嶏紙鍥涳級