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.

题目大意:给一个正整数,找出可以由最少个平方数构成的数量是多少。

解题思路:dynamic programming,递推公式F[n]=min{F[n-1]+1,F[n-4]+2,F[n-9]+3,...};

public class Solution {
int[] f;
public int numSquares(int n) {
f=new int[n+1];
Arrays.fill(f,Integer.MAX_VALUE);
f[1]=1;
for(int i=2;i<=n;i++){
int r = (int)Math.sqrt(i);
if(i==r*r){
f[i]=1;
continue;
}
for(int j=1;j<=r;j++){
f[i]=Math.min(f[i],f[i-j*j]+1);
}
}
return f[n];
}
}

Perfect Squares——Leetcode的更多相关文章

  1. LeetCode Perfect Squares

    原题链接在这里:https://leetcode.com/problems/perfect-squares/ 题目: Given a positive integer n, find the leas ...

  2. LeetCode 279. 完全平方数(Perfect Squares) 7

    279. 完全平方数 279. Perfect Squares 题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数 ...

  3. Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares)

    Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...

  4. 花式求解 LeetCode 279题-Perfect Squares

    原文地址 https://www.jianshu.com/p/2925f4d7511b 迫于就业的压力,不得不先放下 iOS 开发的学习,开始走上漫漫刷题路. 今天我想聊聊 LeetCode 上的第2 ...

  5. [LeetCode] 0279. Perfect Squares 完全平方数

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

  6. [LintCode] Perfect Squares 完全平方数

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

  7. Perfect Squares

    Perfect Squares Total Accepted: 18854 Total Submissions: 63048 Difficulty: Medium Given a positive i ...

  8. CF914A Perfect Squares

    CF914A Perfect Squares 题意翻译 给定一组有n个整数的数组a1,a2,…,an.找出这组数中的最大非完全平方数. 完全平方数是指有这样的一个数x,存在整数y,使得x=y^2y2  ...

  9. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

随机推荐

  1. C#语法糖之第六篇: 泛型委托- Predicate<T>、Func<T>

    今天继续分享泛型委托的Predicate<T>,上篇文章讲了Action委托,这个比Action委托功不一样的地方就是委托引用方法是Bool返回值的方法,Action为无返回值.首先我们看 ...

  2. CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\d29b5393\123c3a1c\App_Code.odl3w4o6.dll”--“拒绝访问。 ”

    IIS部署网站或者Webservice时,出现以下问题: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Tempor ...

  3. 需要 了解 j2ee 框架

    只要你是用java来做WEB应用,绝对少不了使用j2ee框架,目前流行的有 l\5qa_{z   Y(/VW&K&:   )zt*am;   1)struts   2)spring   ...

  4. Linux系统update-alternatives命令使用

    个人博客地址:http://www.cnblogs.com/wdfwolf3/ update-alternatives是ubuntu系统用来进行软件版本切换的命令.比如系统中有几个版本的jdk,把这些 ...

  5. 半质数的个数 csdn 英雄会 高校俱乐部

    2·14 情人&元宵节专题:半质数的个数. 题目:质数是大家熟知的概念,我们定义一个半质数的概念:如果一个数恰好是两个质数的乘积(可以相同),则称它为半质数.前几个半质数是 4, 6, 9, ...

  6. 利用toString做类型的判断

    //利用toString做类型的判断 : /*var arr = []; alert( Object.prototype.toString.call(arr) == '[object Array]' ...

  7. 上传图片带预览功能兼容IE和火狐等主流浏览器

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. adb的logcat使用

    预备:安装刷机精灵,实用工具->adb命令行 1. 对于多机设备,首先使用adb devices来获知设备名称: 2. 将log输出到电脑:adb –s [设备名称] shell logcat ...

  9. C语言怎么计算程序所花时间

    在函数之前和之后取得系统的时间,然后相减就是函数执行时间,不过在取得系统时间的时候,最小单位是微秒 具体代码如下: #include<stdio.h> #include<iostre ...

  10. 【转】转移Package Cache文件夹,转移Windows Installer文件夹

    详见http://blogs.msdn.com/b/heaths/archive/2014/02/11/how-to-relocate-the-package-cache.aspx (注意:若Wind ...