1 解题思想

这道题的意思就是卖火柴的小女孩有一堆长度不等的火柴,他希望拼成一个正方形,不知道可以不可以?

所谓可以就是:
1、用了所有火柴,一根不多,一根不少,当然一根用一次
2、四条边长度一样

所以这道题做法,我用了DFS,就是不停的搜索就好。。。控制好边界时间就好。。。我也不知道有什么很特别的做法

2 原题

Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.Your input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you can save this little girl or not.Example 1:
Input: [1,1,2,2,2]
Output: trueExplanation: You can form a square with length 2, one side of the square came two sticks with length 1.
Example 2:
Input: [3,3,3,3,4]
Output: falseExplanation: You cannot find a way to form a square with all the matchsticks.
Note:
The length sum of the given matchsticks is in the range of 0 to 10^9.
The length of the given matchstick array will not exceed 15.

3 AC解

public class Solution {public boolean makesquare(int[] nums) {if (nums == null || nums.length < 4) return false;int sum = 0;for (int num : nums) sum += num;if (sum % 4 != 0) return false;return dfs(nums, new int[4], 0, sum/4);}private boolean dfs(int[] nums, int[] sums, int index, int target) {if (index == nums.length){if(sums[0] == target && sums[1] == target && sums[2] == target )return true;return true;}for(int i=0;i<4;i++){if(sums[i] + nums[index] <= target){sums[i] += nums[index];if(dfs(nums,sums,index+1,target)) return true;sums[i] -= nums[index];}}return false;}
}

Leetcode 473. Matchsticks to Square 卖火柴的小女孩画框框 解题报告相关推荐

  1. leetcode 473. Matchsticks to Square | 473. 火柴拼正方形(递归)

    题目 https://leetcode.com/problems/matchsticks-to-square/ 题解 看了 hint 之后,才有思路. 讨论区有个人说得好: This solution ...

  2. 《卖火柴的小女孩》告诉我们什么道理?主要内容讲什么故事?

    安徒生通过这个童话,表达了对穷苦人民悲惨遭遇的深刻同情,和对当时社会的不满,无情地揭示了资本主义社会的黑暗和罪恶. <卖火柴的小女孩>告诉我们什么道理 <卖火柴的小女孩>告诉我 ...

  3. 练习题目-卖火柴的小女孩

    Description 卖火柴的小女孩是一个很贫穷的人,她以卖火柴为生,但是她做的火柴长短不一.有一位好心的富豪觉得她很可怜,想给她一笔钱帮助他.为了照顾小女孩的自尊心,富豪决定设置一个小问题考验她, ...

  4. 家电行业的寒冬中,“卖火柴的小女孩”一个接一个

    文 |佘凯文 来源|智能相对论(aixdlun) 安徒生童话中"卖火柴的小女孩"想必大家都很熟知,在寒冬中卖着火柴,最终饥寒交迫地倒在了街头. 在如今全球经济萎靡的之时,许多&qu ...

  5. マッチ売りの少女(卖火柴的小女孩)

    マッチ売りの少女 少女はやさしかったおばあさんのことを思い出しました. 「ああ.おばあさんに会いたいなー」 少女はまた.マッチを擦りました. ぱあーっと.辺りが明るくなり.その光の中で大好きなおばあさ ...

  6. [leetcode] 309. Best Time to Buy and Sell Stock with Cooldown 解题报告

    题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ Say you have an ar ...

  7. LeetCode - 413. Arithmetic Slices - 含中文题意解释 - O(n) - ( C++ ) - 解题报告

    1.题目大意 A sequence of number is called arithmetic if it consists of at least three elements and if th ...

  8. [Swift]LeetCode473. 火柴拼正方形 | Matchsticks to Square

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  9. LeetCode 473. 火柴拼正方形(回溯)

    文章目录 1. 题目 2. 解题 1. 题目 还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法. 不能折断火柴,可以把火柴连接起 ...

最新文章

  1. Groovy - 基本特性
  2. 权重可以当做概率幅吗?---用神经网络的收敛模拟机械波的波动
  3. linux进程号为一,一步步探究linux进程中的用户ID
  4. MSN网盘SkyDrive
  5. 坚果pro2官方rom_锤子坚果Pro2刷机包_原版升级包 锤子坚果Pro2线刷包_官方固件rom包下载...
  6. 数据库表文档生成工具screw (螺丝钉)
  7. 批处理为win7桌面添加计算机图标,win7桌面图标不见了图文解决方案
  8. CLion输出中文乱码解决方案
  9. 计算机开机选择用户界面,Windows10每次开机都会出现选择操作系统界面的解决方法...
  10. 阴阳师辅助(基于按键精灵)
  11. “杜拉拉思维模式”之五:面试礼仪速成
  12. 域账户或者本地用户,每次开机登陆都重置桌面
  13. 地球内部热量从而何来?其中50%热量来源不明
  14. Python日历模块| 使用示例的weekday()方法
  15. 《Deep Modular Co-Attention Networks for Visual Question Answering》论文翻译
  16. zz一篇很赞同的文章:2008年以后房价会降?想都不要想
  17. C# winform如何时时获取当前系统时间
  18. CancerSubtypes包的介绍(根据生信技能树Jimmy老师分享的乳腺癌分子分型包资料整理)
  19. ROS探索总结(九)——操作杆控制
  20. 将扩散模型应用到文本领域

热门文章

  1. 总结|图像分割5大经典方法
  2. uniapp 安卓多个音频_Win10发布新版,可从PC直接访问安卓手机APP,你会重启升级吗?...
  3. Unity 之 OnGUI实时显示游戏FPS...
  4. 详细SpringBoot教程之缓存开发
  5. 哔哩哔哩直播,B站up主直播间数据如何监测?
  6. 计算机组成原理奇偶校验位,奇偶校验码的实现原理_奇偶校验位的奇偶校验块...
  7. ssh 登录Petalinux
  8. 深入理解 JavaScript 函数的特性与最佳实践
  9. 移动开发免费收费api及网站收藏
  10. 数据挖掘复习资料2021.12.15