416. Partition Equal Subset Sum
题目:
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的更多相关文章
- LN : leetcode 416 Partition Equal Subset Sum
		
lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...
 - [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割
		
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
 - Leetcode 416. Partition Equal Subset Sum
		
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
 - [leetcode]416. Partition Equal Subset Sum分割数组的和相同子集
		
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
 - LC 416. Partition Equal Subset Sum
		
题目 Given a non-empty array containing only positive integers, find if the array can be partitioned i ...
 - 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...
 - 416 Partition Equal Subset Sum 分割相同子集和
		
详见:https://leetcode.com/problems/partition-equal-subset-sum/description/ C++: class Solution { publi ...
 - 【leetcode】416. Partition Equal Subset Sum
		
题目如下: 解题思路:对于这种判断是否的题目,首先看看动态规划能不能解决.本题可以看成是从nums中任选i个元素,判断其和是否为sum(nums)/2,很显然从nums中任选i个元素的和的取值范围是[ ...
 - [刷题] 416 Partition Equal Subset Sum
		
要求 非空数组的所有数字都是正整数,是否可以将这个数组的元素分成两部分,使得每部分的数字和相等 最多200个数字,每个数字最大为100 示例 [1,5,11,5],返回 true [1,2,3,5], ...
 
随机推荐
- nyoj 44  子串和 简单动态规划
			
子串和 时间限制:5000 ms | 内存限制:65535 KB 难度:3 描述 给定一整型数列{a1,a2...,an},找出连续非空子串{ax,ax+1,...,ay},使得该子序列的和最 ...
 - TP数据访问
			
重点学习了: 1,ThinkPHP查询数据 2.ThinkPHP添加数据 LianXiController.class.php <?php namespace Home\Controller; ...
 - Mac Pro 开机自启动 PHP-FPM,Nginx,MySql 等软件
			
在Mac下安装好了PHP开发环境(PHP-FPM,Nginx,MySql), 想设置成开机自启动,原来以为和一般的Linux系统一样,也是在rc.d这样目录放置启动脚本.在网上查了一些资料,发现苹果应 ...
 - ubutu之mysql emma中文乱码问题解决
			
emma默认用apt-get 安装的话,emma是不支持中文的,配置文件或直接修改emma程序源文件(python).apt-get安装emmasudo apt-get install emma ...
 - MyEclipse 自动提示设置
			
window --> Perferences--> General--> keys Content Assist默认的是Ctrl +space Content Assist快捷键设置 ...
 - js图文讲解
			
 - unity3d 安卓IOS推送
			
https://github.com/jpush/jpush-unity3d-plugin
 - web服务器页面错误代码集
			
HTTP 1xx-信息提示 这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个1xx响应. 100-继续. 101-切换协议. 2xx-成功 这类状态代码表明服务器成功地接受了 ...
 - linux 权限操作
			
添加用户 命令格式:useradd 选项 用户名 -g 缺省组(不写则默认为用户名组) -G 所属组(添加到多个用户组,逗号隔开) -c 描述信息 -d 指定家目录 useradd –g aa bb ...
 - .oi 小游戏
			
http://agar.io/ http://diep.io/ http://slither.io/ http://splix.io/ http://wilds.io/ http://kingz.io ...