zoukankan      html  css  js  c++  java
  • 0416. Partition Equal Subset Sum (M)

    Partition Equal Subset Sum (M)

    题目

    Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

    Example 1:

    Input: nums = [1,5,11,5]
    Output: true
    Explanation: The array can be partitioned as [1, 5, 5] and [11].
    

    Example 2:

    Input: nums = [1,2,3,5]
    Output: false
    Explanation: The array cannot be partitioned into equal sum subsets.
    

    Constraints:

    • 1 <= nums.length <= 200
    • 1 <= nums[i] <= 100

    题意

    在一个给定数组中找到一个子数组,使其和为指定值。

    思路

    使用记忆化搜索容易实现。


    代码实现

    Java

    class Solution {
        public boolean canPartition(int[] nums) {
            int total = 0;
            int left = 0, right = nums.length - 1;
            
            for (int num : nums) {
                total += num;
            }
    
            if (total % 2 == 1) {
                return false;
            }
            
            return dfs(total / 2, nums, 0, new Boolean[total / 2 + 1][nums.length]);
        }
        
        private boolean dfs(int sum, int[] nums, int start, Boolean[][] record) {
            if (start == nums.length) {
                return sum == 0;
            }
            if (sum < 0) {
                return false;
            }
            if (record[sum][start] != null) {
                return record[sum][start];
            }
            
            boolean found = dfs(sum, nums, start + 1, record) || dfs(sum - nums[start], nums, start + 1, record);
            record[sum][start] = found;
            return found;
        }
    }
    ```# Partition Equal Subset Sum (M)
    
    ## 题目
    
    Given a **non-empty** array `nums` containing **only positive integers**, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
    
    **Example 1:**
    
    

    Input: nums = [1,5,11,5]
    Output: true
    Explanation: The array can be partitioned as [1, 5, 5] and [11].

    
    **Example 2:**
    
    

    Input: nums = [1,2,3,5]
    Output: false
    Explanation: The array cannot be partitioned into equal sum subsets.

    
    **Constraints:**
    
    - `1 <= nums.length <= 200`
    - `1 <= nums[i] <= 100`
    
    ---
    
    ## 题意
    
    在一个给定数组中找到一个子数组,使其和为指定值。
    
    ## 思路
    
    使用记忆化搜索容易实现。
    
    ---
    
    ## 代码实现
    
    ### Java
    
    ```java
    class Solution {
        public boolean canPartition(int[] nums) {
            int total = 0;
            int left = 0, right = nums.length - 1;
            
            for (int num : nums) {
                total += num;
            }
    
            if (total % 2 == 1) {
                return false;
            }
            
            return dfs(total / 2, nums, 0, new Boolean[total / 2 + 1][nums.length]);
        }
        
        private boolean dfs(int sum, int[] nums, int start, Boolean[][] record) {
            if (start == nums.length) {
                return sum == 0;
            }
            if (sum < 0) {
                return false;
            }
            if (record[sum][start] != null) {
                return record[sum][start];
            }
            
            boolean found = dfs(sum, nums, start + 1, record) || dfs(sum - nums[start], nums, start + 1, record);
            record[sum][start] = found;
            return found;
        }
    }
    
  • 相关阅读:
    Building Java Projects with Gradle
    Vert.x简介
    Spring及Spring Boot 国内快速开发框架
    dip vs di vs ioc
    Tools (StExBar vs Cmder)which can switch to command line window on context menu in windows OS
    SSO的定义、原理、组件及应用
    ModSecurity is an open source, cross-platform web application firewall (WAF) module.
    TDD中测试替身学习总结
    Spring事务银行转账示例
    台式机(华硕主板)前面板音频接口(耳机和麦克风)均无声的解决办法
  • 原文地址:https://www.cnblogs.com/mapoos/p/14050202.html
Copyright © 2011-2022 走看看