绠楁硶-鍔ㄦ€佽鍒掞紙鍥涳級
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">涓嬬壒鐐癸細
鏁扮粍鐨勬€诲拰涓哄伓鏁帮紝濡傛灉鎬诲拰涓哄鏁帮紝鎴戜滑涔熷氨娌″繀瑕佸啀寰€涓嬭绠椾簡銆?/span>
涓や釜瀛愰泦鐨勫拰涓€瀹氱瓑浜庢暟缁勬€诲拰鐨勪竴鍗娿€?/span>
鍩轰簬涓婇潰涓や釜鐗圭偣锛屾垜浠杩欓亾棰樹篃灏辩畻鏄渶鍩烘湰鐨勬€濊矾鎵撳紑浜嗭紝涓嬮潰灏辨槸0-1鑳屽寘闂鐨勫垎鏋愪簡銆?br>
0-1鑳屽寘闂涓紝鏁扮粍涓殑銆屾瘡涓暟鍙兘鍙栦竴娆°€嶃€傛墍浠ユ暣浣撴€濊矾灏辨槸锛屼竴涓竴涓殑鍙栨暟锛屼竴鐐逛竴鐐圭殑澧炲姞瀹归噺銆?/span>
瀹氫箟鏁扮粍鐨勯暱搴︿负len锛屾暟缁勬€诲拰鐨勪竴鍗婁负target锛屾棰樺叿浣撳仛娉曞涓嬶細鎴戜滑瀹氫箟涓€涓涓簂en锛屽垪涓簍arget+1鐨勪簩缁存暟缁刣p锛?/span>
鐘舵€佸畾涔夛細dp[i][j]琛ㄧず锛屾垜浠彲浠ヤ粠0~i鍖洪棿鍐呴€夋嫨涓€浜涙暟浣垮緱杩欎簺鏁扮殑鍜屾伆绛変簬j銆?br>
杞€佽浆绉?/span>锛?br>
褰撻€夋嫨nums[i]鏃讹紝鍦╗0 , i-1]杩欎釜鍖洪棿鍐呰兘鎵惧埌涓€浜涙暟锛岃繖浜涙暟鐨勫拰涓簀-nums[i]锛岃繖鏃禿p[i][j]=dp[i-1][j-nums[i]]銆?/span>
褰撲笉閫夋嫨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>
浣嗘槸褰搄==nums[i]鏃讹紝j-nums[i]==0鏄垚绔嬬殑锛宯ums[i]杩欎釜鏁板彲浠ヨ鍗曠嫭鍒掑垎涓轰竴涓瓙闆嗐€傚洜姝ゆ垜浠妸dp[0][0]璁句负true涔熸槸鍚堢悊銆傝櫧鐒朵笉绗﹀悎鐘舵€佸畾涔夛紝浣嗘槸浠庣姸鎬佽浆绉绘潵璇存槸瀹屽叏绗﹀悎鐨勩€?/p>
鏍规嵁鐘舵€佽浆绉绘柟绋嬶紝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>
鎴戜滑鍩轰簬瀵逛笂闈袱涓В娉曠殑鎬濊矾锛屽仛涓€浜涙€濊€冿細
涓婇潰瑙f硶涓紝鎴戜滑姣忔鐨勫惊鐜噷閮藉彧鐢ㄥ埌浜嗕笂闈竴琛岀殑鏁版嵁锛岃€屼笖鍙敤鍒颁簡涓婇潰涓€琛屽悓涓€鍒楃殑鏁版嵁鍜屼笂闈竴琛屽乏杈规煇涓綅缃殑鏁版嵁銆?/p>
鎴戜滑閲囩敤銆屾粴鍔ㄦ暟缁勩€嶇殑鏂瑰紡鏉ヨ繘琛屻€屽~琛ㄣ€嶃€?/p>
杩欏効鎴戜滑瀹氫箟涓€涓竴缁存暟缁刣p锛岄暱搴︿负target+1锛屼粠鍚庡悜鍓嶄緷娆″~琛ㄣ€?/p>
浠庡悗鍚戝墠濉〃鐨勮繃绋嬶紝濡傛灉纰板埌涓嶆弧瓒砵>=nums[i]锛屼篃灏变笉蹇呰缁х画杩涜浜嗭紝鍙互椹笂閫€鍑哄惊鐜紝鍥犱负瑕佷繚璇乨p[j-nums[i]]涓嬫爣鍚堟硶銆?/p>
鎬濊€冿細涓轰粈涔堣浠庡悗鍚戝墠閫嗗簭濉〃锛?/span>
杩欐槸鍥犱负鍦ㄤ竴缁寸殑鎯呭喌涓嬶紝鏍规嵁dp[j] || dp[j-nums[i]]鏉ユ帹dp[j]鐨勫€硷紝鎴戜滑闇€瑕佷繚璇佹暟缁勪腑鍓嶉潰鐨勫€间笉鑳藉鍚庨潰鐨勫€兼湁褰卞搷锛屼妇涓緥瀛愶紝缁欏畾鏁扮粍骞堕噰鐢ㄩ噰鐢ㄦ搴忓~琛細
[6锛?/span> ]锛宼arget=
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
馃憞
以上是关于绠楁硶-鍔ㄦ€佽鍒掞紙鍥涳級的主要内容,如果未能解决你的问题,请参考以下文章