给定正整数 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. Python基础之 一列表

    列表模块:listlen(list):列表元素个数max(list):列表元素最大值min(list):列表元素最小值list(seq):将元组转为列表list.append(obj):列表末尾添加新 ...

  2. hdfs是什么?

    参考:https://www.cnblogs.com/shijiaoyun/p/5778025.html hadoop分布式文件系统 1.hdfs是一个分布式文件系统,简单理解就是多台机器组成的一个文 ...

  3. JSP基础教程:tutorialspoint-jsp

    来自turorialspoint的JSP基础教程(英文),官网:https://www.tutorialspoint.com/jsp/index.htm 这个教程在国内已经被翻译成中文(不过是属于机器 ...

  4. Servlet自动刷新页面

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/auto-refresh.html: 假设一个Web页面,显示了现场比赛得分或股票市场状况或货币兑 ...

  5. linux 的硬链接与软连接

    linux 里有硬链接和软连接两种概念.要明白这些概念首先要明白文件在linux 上其实有3个组成部分. data 真正的数据存储区域 inode 一个用来唯一表示data的数据结构 filename ...

  6. 深度学习-theano-windows -cuda-环境搭建

    本文将具体介绍深度学习之cuda的环境搭建 工具:支持CUDA的显卡(安装cuda6.5),VS2013.Anaconda. 步骤: 1.安装cuda6.5 这个不具体介绍,网上有很多文章.注意选择你 ...

  7. Android开源项目SlidingMenu的学习笔记(一)

    SlidingMenu是眼下在应用程序上非常流行的一种UI技术.能够实现一种比較炫的滑动效果,SlidingMenu是Git上托管的一个项目,开源免费的.SlidingMenu作为一个Library的 ...

  8. 2016/1/6 输出菱形 while语句计算阶乘分数之和

    public class LingXing { public static void main(String[] args) { //打印菱形 for (int x=1;x<6;x++){ fo ...

  9. libXdmcp.so.6

    Traceback (most recent call last): File "/data/xiaole_dl_img/product.v.one.banner.fixedperiod.g ...

  10. linux之安装软件出现Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)解决总结

    sudo rm -rf /var/lib/dpkg/lock sudo rm -rf /var/cache/apt/archives/lock