A trickier DFS, with a little bit complex recursion param tweak, and what's more important is pruning strategies.. or your code will TLE.

  1. class Solution {
  2. bool go(vector<bool> &m, int edge, int eleft, int etgt, vector<int> &in, int taken)
  3. {
  4. if(edge > ) return false;
  5. if(edge == && eleft == etgt && in.size() == taken)
  6. {
  7. return true;
  8. }
  9. int last = -;
  10. for(int i = in.size()-; i >=; i --)
  11. {
  12. if(m[i]) continue;
  13. if(in[i] > eleft) continue; // because it is sorted
  14. if(in[i] == last) continue; // important: critical pruning.
  15. if(in[i] <= eleft)
  16. {
  17. m[i] = true;
  18. bool fit = in[i] == eleft;
  19. if(go(m, edge + (fit?:), fit?etgt:(eleft-in[i]), etgt, in, taken + ))
  20. {
  21. return true;
  22. }
  23. m[i] = false;
  24. last = in[i];
  25. }
  26. }
  27.  
  28. return false;
  29. }
  30. public:
  31. bool makesquare(vector<int>& nums) {
  32. if(nums.size() < ) return false;
  33.  
  34. int sum = accumulate(nums.begin(), nums.end(), );
  35. if(!sum || (sum % )) return false;
  36.  
  37. sort(nums.begin(), nums.end());
  38. if (nums.back() > (sum/)) return false;
  39.  
  40. vector<bool> m(nums.size(), false);
  41.  
  42. int tgt = sum / ;
  43. return go(m, , tgt, tgt, nums, );
  44. }
  45. };

LeetCode "473. Matchsticks to Square"的更多相关文章

  1. 【LeetCode】473. Matchsticks to Square 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  2. 【leetcode】473. Matchsticks to Square

    题目如下: 解题思路:居然把卖火柴的小女孩都搬出来了.题目的意思是输入一个数组,判断能否把数组分成四个子数组,使得每个子数组的和相等.首先我们可以很容易的求出每个子数组的和应该是avg = sum(n ...

  3. 473. Matchsticks to Square

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  4. 473 Matchsticks to Square 火柴拼正方形

    还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到.输入为小女孩拥有火柴的 ...

  5. Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square)

    Leetcode之深度优先搜索(DFS)专题-473. 火柴拼正方形(Matchsticks to Square) 深度优先搜索的解题详细介绍,点击 还记得童话<卖火柴的小女孩>吗?现在, ...

  6. LeetCode 题解 593. Valid Square (Medium)

    LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...

  7. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  8. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  9. Leetcode: Matchsticks to Square && Grammar: reverse an primative array

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

随机推荐

  1. 将NSString转换成UTF8编码的NSString

    在使用网络地址时,一般要先将url进行encode成UTF8格式的编码,否则在使用时可能报告网址不存在的错误,这时就需要进行转换 下面就是转换函数: NSString *urlString= [NSS ...

  2. Windows多网卡上网设置

    http://blog.tianya.cn/blogger/post_show.asp?BlogID=1566293&PostID=12984307

  3. IT之梦

    生活原本不孤单,可惜人们总爱感叹 日子其实很简单,而我却不甘平淡 工作中我尽情的挥洒热汗,只为让成功离自己更近一点 生活中我为人和善,只为人生的路上多一个伙伴 时间不等人,IT尚遥远 我愿舍身敬业,誓 ...

  4. 估计PI——OpenCV&Cpp

    来源:<Learning Image Processing With OpenCV> 算法原理:蒙特卡洛 PI的计算公式: Cpp代码: #include <opencv2/open ...

  5. Centos安装完MariaDB后启动不了 MySQL is not running, but lock file (/var/lock/subsys/mysql) exists

    [root@admin-node subsys]# service mysql startStarting MySQL. ERROR! [root@admin-node subsys]# servic ...

  6. easyUI中datetimebox和combobox的取值方法

    easyUi页面布局中,查询条件放在JS中,如下 <script type="text/javascript"> var columnList = [ [   {    ...

  7. array_unshift() 、

    定义和用法 array_unshift() 函数在数组开头插入一个或多个元素. 被加上的元素作为一个整体添加,这些元素在数组中的顺序和在参数中的顺序一样. 该函数会返回数组中元素的个数. 语法 arr ...

  8. 转发 java数据结构之hashMap详解

    概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...

  9. Python 中的map和reduce学习笔记

    map和reduce都是Python中的内置函数 map函数接受两个参数,第一个参数是函数,第二个参数是列表,将函数依次作用于列表中的元素,并返回一个元素 reduce同样以函数和列表作为参数,区别在 ...

  10. SQL server2008-对象资源管理器

    对象资源管理器:数据库 .安全性.服务器对象.复制.管理 .SQL server代理 六部分组成