funccanPartition(nums []int)bool { n := len(nums) if n<2 { returnfalse } sum, max := 0, 0 for _, v := range nums { sum += v if v > max { max = v } } // 不能均分一半 if sum%2 != 0 { returnfalse } tar := sum / 2 // 如果有个元素比一半和还大,那么不可能找到 if max > tar { returnfalse } dp := make([][]bool, n) for i:= range dp { dp[i] = make([]bool, tar + 1) dp[i][0] = true } dp[0][nums[0]] = true for i:=1; i<n; i++ { v := nums[i] for j:=1; j<=tar; j++ { if j >= v { dp[i][j] = dp[i-1][j] || dp[i-1][j-v] } else { dp[i][j] = dp[i-1][j] } } } return dp[n-1][tar] }