给定正整数 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. Toast自定义

    Toast toast=new Toast(MainActivity.this); toast.setView(getLayoutInflater().inflate(R.layout.toast,n ...

  2. ***jQuery使用总结(原创)

    Q: jquery选择器为变量时是怎么办 A: 一个变量我知道可以这样写:$("#"+id) Q: 如何清除单选框的checked属性 A: $("input[type= ...

  3. Codeforces Round #343 (Div. 2)【A,B水题】

    A. Far Relative's Birthday Cake 题意: 求在同一行.同一列的巧克力对数. 分析: 水题~样例搞明白再下笔! 代码: #include<iostream> u ...

  4. 常用的delphi 第三方控件

    Devexpress VCL 这个基本上覆盖了系统界面及数据库展示的方方面面,是做桌面系统必备的一套控件,目前的版本是2011.2.3, 支持win32 及win64. AutoUpgrader 这个 ...

  5. JSTL-SQL标签库

    主页:http://www.cnblogs.com/EasonJim/p/6958992.html的分支页. 本章的前提需要先新建数据表及添加默认数据,脚本如下: -- -- 数据库: `test` ...

  6. Python学习系列之反射

    反射的定义 根据字符串的形式去某个对象中操作成员 根据字符串的形式去某个对象中寻找成员 根据字符串的形式去某个对象中设置成员 根据字符串的形式去某个对象中删除成员 根据字符串的形式去某个对象中判断成员 ...

  7. MapReduce WordCount Combiner程序

    MapReduce WordCount Combiner程序 注意使用Combiner之后的累加情况是不同的: pom.xml <project xmlns="http://maven ...

  8. Mybatis的配置文件和映射文件具体解释

    一.Mybatis的全局配置文件 1.SqlMapConfig.xml是mybatis的全局配置文件.配置内容例如以下: properties(属性) settings(全局配置參数) typeAli ...

  9. Guava ---- EventBus事件驱动模型

    在软件开发过程中, 难免有信息的共享或者对象间的协作. 怎样让对象间信息共享高效, 而且耦合性低. 这是一个难题. 而耦合性高将带来编码改动牵一发而动全身的连锁效应. Spring的风靡正是由于攻克了 ...

  10. nRF52832之硬件I2C

    这几天一直在折腾nRF52832的硬件I2C,到了今天最终出现了成果,在此也印证了那句话:"耕耘就有收获" 52832的硬件I2C尽管官方提供了demo,可是自己对I2C通信理解的 ...