LeetCode(279)Perfect Squares
题目
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.
For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.
分析
完美平方数,给定任意数n,它可表示为多个平方数(如1,4,9,16…)的和,求出表示出任意数n所需的平方数的最少个数。
考察动态规划,
如果一个数x可以表示为一个任意数a加上一个平方数b∗b,也就是x=a+b∗b,那么能组成这个数x最少的平方数个数,就是能组成a最少的平方数个数加上1(因为b∗b已经是平方数了)。
AC代码
class Solution {
public:
/* 如果一个数x可以表示为一个任意数a加上一个平方数bxb,也就是x = a + bxb,
* 那么能组成这个数x最少的平方数个数,就是能组成a最少的平方数个数加上1(因为b*b已经是平方数了)。
*/
int numSquares(int n) {
// 将所有非平方数的结果置最大,保证之后比较的时候不被选中
vector<int> nums(n + 1, INT_MAX);
// 将所有整平方数的结果置1
for (int i = 0; i*i <= n; ++i)
{
nums[i*i] = 1;
}//for
// 从小到大找任意数a
for (int a = 0; a <= n; ++a)
{
// 从小到大找平方数b*b
for (int b = 0; a + b*b <= n; ++b)
{
// 因为a+b*b可能本身就是平方数,所以我们要取两个中较小的
nums[a + b*b] = min(nums[a] + 1, nums[a + b*b]);
}//for
}//for
return nums[n];
}
};
LeetCode(279)Perfect Squares的更多相关文章
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- net core 认证及简单集群
net core 认证及简单集群 在Asp.net WebAPI中,认证是通过AuthenticationFilter过滤器实现的,我们通常的做法是自定义AuthenticationFilter,实现 ...
- 二开获取yigo设计器里查询集合里中的某个SQL
package com.bokesoft.lrp_v3.mid.dongming.service; import java.math.BigDecimal; import java.util.Arra ...
- awk单引号处理
awk中使用单引号,常规字符串,'\''即可,但如果像下面在$4变量用单引号,则还需要加上双引号才行. cat 2.txt | awk '{ print $1, $2, $3, "'\''& ...
- 【转】如何学习Javascript
首先要说明的是,咱现在不是高手,最多还是一个半桶水,算是入了JS的门. 谈不上经验,都是一些教训. 这个时候有人要说,“靠,你丫半桶水,凭啥教我们”.您先别急着骂,先听我说. 你叫一个大学生去教小学数 ...
- babel7中 corejs 和 corejs2 的区别
babel7中 corejs 和 corejs2 的区别 最近在给项目升级 webpack4 和 babel7,有一些改变但是变化不大.具体过程可以参考这篇文章 webpack4:连奏中的进化.只是文 ...
- Ubuntu批量修改权限
Ubuntu中有两个修改命令可以用到,「change mode」&「change owner」 即chmod以及chown,其中可以用递归参数-R来实现更改所有子文件和子目录的权限. 1.利用 ...
- CRC检错技术原理
一.题外话 说来惭愧,一开始是考虑写关于CRC检错技术更深层次数学原理的,然而在翻看<Basic Algebra>后,我果断放弃了这种不切实际的想法.个人觉得不是因为本人数学水平差或者能力 ...
- SharePoint Online和SharePoint 2016 导出到Excel 表错误
导出到Excel是一个有用的SharePoint功能.偶尔您可能会遇到该功能无法正常工作的情况.有几个原因可能导致此功能无法正常工作. Problem #1 使用非32位Internet Explor ...
- MovieReview—NINE LIVES(九条命)
Struggle & Family A successful middle-aged man in the movie became a cat by falling from ...
- Cocos2d-x——导入Cocostudio资源
(搬运自我在SegmentFault的博客) 目前正在和实训的小组成员一起做一款手机2D游戏,我们采用了Cocos2d-x进行开发.之前虽然早有耳闻,这次却是第一次认真地学习和使用Cocos2d-x. ...