给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...) 使得他们的和等于 n。你需要让平方数的个数最少。
比如 n = 12,返回 3 ,因为 12 = 4 + 4 + 4 ; 给定 n = 13,返回 2 ,因为 13 = 4 + 9。

详见:https://leetcode.com/problems/perfect-squares/description/

Java实现:

方法一:递归实现

class Solution {
public int numSquares(int n) {
int res = n, num = 2;
while (num * num <= n) {
int a = n / (num * num), b = n % (num * num);
res = Math.min(res, a + numSquares(b));
++num;
}
return res;
}
}

方法二:动态规划

如果一个数x可以表示为一个任意数a加上一个平方数bxb,也就是x=a+bxb,那么能组成这个数x最少的平方数个数,就是能组成a最少的平方数个数加上1(因为b*b已经是平方数了)。

class Solution {
public int numSquares(int n) {
int[] dp = new int[n+1];
// 将所有非平方数的结果置最大,保证之后比较的时候不被选中
Arrays.fill(dp, Integer.MAX_VALUE);
// 将所有平方数的结果置1
for(int i = 0; i * i <= n; i++){
dp[i * i] = 1;
}
// 从小到大找任意数a
for(int a = 0; a <= n; a++){
// 从小到大找平方数bxb
for(int b = 0; a + b * b <= n; b++){
// 因为a+b*b可能本身就是平方数,所以我们要取两个中较小的
dp[a + b * b] = Math.min(dp[a] + 1, dp[a + b * b]);
}
}
return dp[n];
}
}

C++实现:

方法一:

class Solution {
public:
int numSquares(int n) {
while(n%4==0)
{
n/=4;
}
if(n%8==7)
{
return 4;
}
for(int a=0;a*a<=n;++a)
{
int b=sqrt(n-a*a);
if(a*a+b*b==n)
{
return !!a+!!b;
}
}
return 3;
}
};

方法二:

class Solution {
public:
int numSquares(int n) {
vector<int> dp(n+1,INT_MAX);
dp[0]=0;
for(int i=0;i<=n;++i)
{
for(int j=1;i+j*j<=n;++j)
{
dp[i+j*j]=min(dp[i+j*j],dp[i]+1);
}
}
return dp.back();
}
};

方法三:

class Solution {
public:
int numSquares(int n) {
vector<int> dp(1, 0);
while (dp.size() <= n)
{
int m = dp.size(), val = INT_MAX;
for (int i = 1; i * i <= m; ++i)
{
val = min(val, dp[m - i * i] + 1);
}
dp.push_back(val);
}
return dp.back();
}
};

参考:https://www.cnblogs.com/grandyang/p/4800552.html

279 Perfect Squares 完美平方数的更多相关文章

  1. 搜索(BFS)---完美平方数

    完美平方数 279. Perfect Squares (Medium) For example, given n = 12, return 3 because 12 = 4 + 4 + 4; give ...

  2. 63.Perfect Squares(完美平方数)

    Level:   Medium 题目描述: Given a positive integer n, find the least number of perfect square numbers (f ...

  3. Project Euler 98:Anagramic squares 重排平方数

    Anagramic squares By replacing each of the letters in the word CARE with 1, 2, 9, and 6 respectively ...

  4. [LeetCode] 279. Perfect Squares 完全平方数

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  5. 【LeetCode】279. Perfect Squares 解题报告(C++ & Java)

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

  6. (BFS) leetcode 279. Perfect Squares

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  7. 279. Perfect Squares

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  8. leetcode@ [279]Perfect Squares

    https://leetcode.com/problems/perfect-squares/ Given a positive integer n, find the least number of ...

  9. 279. Perfect Squares(动态规划)

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

随机推荐

  1. JavaWeb图片显示与存储

    在数据库中存储文件的名称,在存储信息资料里面存下照片,利用文件名称. 代码如下: 其中iamgeFile为 图片存储的路径userImages/ Resultuser.setImageName(Pro ...

  2. Ubuntu 16.04安装SwitchHosts

    下载: https://github.com/oldj/SwitchHosts/releases 解压: unzip SwitchHosts-linux-x64_v3.3.6.5287.zip 移动: ...

  3. linux 用 rsync 快速删除大量小文件

    假设我们在目录 /tmp/to_delete 下有很多小文件 a1 a2 a3 f1 f2 f3 现在我们想快速的删除f 开头的文件. 如果文件量大,用rm 可能会失败,而且会很慢, 所以用rsync ...

  4. 将github上的项目源码导入eclipse详细教程

    将github上的项目源码导入eclipse详细教程 学习了: http://blog.csdn.net/itbiggod/article/details/78462720

  5. Jafka源码分析——LogManager

    在Kafka中,LogManager负责管理broker上全部的Log(每个topic-partition为一个Log). 通过阅读源码可知其详细完毕的功能例如以下: 1. 依照预设规则对消息队列进行 ...

  6. Maven项目中遇到的奇葩问题(续)

    场景描写叙述 开发项目搞环境是一个很蛋疼的问题.总是会遇到各种奇葩的问题,上一篇文章http://blog.csdn.net/gao36951/article/details/50955526中遇到的 ...

  7. Cocos2D-X2.2.3学习笔记12(瞬时动作)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHVjYmxvZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  8. JAVA 学习 IDEA安装及破解

    1.到官网下载安装包 网址:https://www.jetbrains.com/idea/download/#section=windows 选择  “”Ultimate “”这个版本点击“Downl ...

  9. 操作系统学习笔记:CPU调度

    CPU调度的目的在于提高CPU利用率,不让CPU闲着.CPU是宝贵的资源,如果有一个进程,本来在CPU中运行,忽然因为要使用IO资源,于是转而请求IO,这边CPU挂起,造成就绪队列中的其他进程等待,这 ...

  10. busybox的使用

    1 将busybox设置为静态链接,放在文件系统中使用 make menuconfig的时候,Busybox Settings --> Build Options --> Build Bu ...