题目:

Given a non-empty array 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.

Note:
Both the array size and each of the array element will not exceed 100.

Example 1:

Input: [1, 5, 11, 5]

Output: true

Explanation: The array can be partitioned as [1, 5, 5] and [11].

Example 2:

Input: [1, 2, 3, 5]

Output: false

Explanation: The array cannot be partitioned into equal sum subsets.

代码:

今天赶上周末,LeetCode又提示contest开始了,一直没有勇气去做,今天看了下,第一题,好简单,两个字符串转换成数字相加。

瞬间有了信心,于是开始研究这个题目,判断两个数组是否可以分成和相同的两部分,看起来貌似不难哦。

首先当然是所有元素相加除2了,为偶数才有可能嘛,当然,必须有足够的元素和为这个值。

想想,穷举一下,遍历所有情况不就可以了吗?

可是写啊写啊,循环、递归,逻辑总是想不清楚,折腾到contest结束。

无奈百度了一下,大神早已完成博客,python下竟然只用了6、7行代码!!!

参考:http://bookshadow.com/weblog/2016/10/09/leetcode-partition-equal-subset-sum/

贴过来供大家一起学习:动态规划的思想

sums = sum(nums)
        if sums & 1: return False
        nset = set([0])
        for n in nums:
            for m in nset.copy():
                print 'n:'+str(n)+' '+'m:'+str(m)
                nset.add(m + n)
                print nset
        return sums / 2 in nset

大神果然是大神!还有很长路要走喽!

416. Partition Equal Subset Sum的更多相关文章

  1. LN : leetcode 416 Partition Equal Subset Sum

    lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...

  2. [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  3. Leetcode 416. Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  4. [leetcode]416. Partition Equal Subset Sum分割数组的和相同子集

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  5. LC 416. Partition Equal Subset Sum

    题目 Given a non-empty array containing only positive integers, find if the array can be partitioned i ...

  6. 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...

  7. 416 Partition Equal Subset Sum 分割相同子集和

    详见:https://leetcode.com/problems/partition-equal-subset-sum/description/ C++: class Solution { publi ...

  8. 【leetcode】416. Partition Equal Subset Sum

    题目如下: 解题思路:对于这种判断是否的题目,首先看看动态规划能不能解决.本题可以看成是从nums中任选i个元素,判断其和是否为sum(nums)/2,很显然从nums中任选i个元素的和的取值范围是[ ...

  9. [刷题] 416 Partition Equal Subset Sum

    要求 非空数组的所有数字都是正整数,是否可以将这个数组的元素分成两部分,使得每部分的数字和相等 最多200个数字,每个数字最大为100 示例 [1,5,11,5],返回 true [1,2,3,5], ...

随机推荐

  1. PHP基础之 继承(一)

    ========================================= *                 继承 extends *============================ ...

  2. php补充

    PHP 教程 echo 和 print 之间的差异:echo - 能够输出一个以上的字符串print - 只能输出一个字符串,并始终返回 1提示:echo 比 print 稍快,因为它不返回任何值. ...

  3. STM32F10xx CAN BUS相关库文件"stm32f10x_can.c"内的库函数解析

    一.背景: 还是继续CAN通信,要节省开发时间,使用库函数可大大降低开发周期,并且还能确保寄存器的配置几 乎是万无一失,所以,在此就STM32F10xx的CAN操作库函数的使用做个简析. STM32有 ...

  4. java web 相对路径中已/开头和不已/开头的区别

    通俗的讲,有/会从跟目录开始算,没有会从当前目录开始算 1.前台页面 ​页面中向服务器页面请求静态资源且没有指定<base href="<%=basePath%>" ...

  5. .apache2 设置多个虚拟域名

    <VirtualHost 127.0.0.2:80> ServerName www.xylilun.cn DocumentRoot E:/www/ylll <Directory E: ...

  6. Web项目,F12调试的说明

    sessionstorage,localstorage和cookie之间的区别 区别:cookie数据始终在同源的http请求中携带(即使不需要),即cookie在浏览器和服务器间来回传递.而sess ...

  7. Opencv CamShift+Kalman目标跟踪

    #include "stdio.h" #include "string.h" #include "iostream" #include &q ...

  8. 浅谈VBA

    VBA,全称Visual Basic for Applications,其中的一些专业性的解释可以自行搜索,这里就不一一介绍.半年以前,我是不知道VBA的,当我听到VBA的时候,我却迷糊了.VBA是什 ...

  9. 36 网络相关函数(四)——live555源码阅读(四)网络

    36 网络相关函数(四)——live555源码阅读(四)网络 36 网络相关函数(四)——live555源码阅读(四)网络 简介 7)createSocket创建socket方法 8)closeSoc ...

  10. flask 链接 url_for()

    通常html的文件都放在template里面,那么静态的文件放在哪呢?staitc里面 调用 url_for('static', filename='css/styles.css', _externa ...