learn from

  • DP
class Solution {
public:
int numSquares(int n) {
if(n<=0)return 0;
int * dp = new int[n+1]();
for(int i=1;i<=n;i++){
dp[i]= INT_MAX;
for(int j =1;j*j <=i;j++){
int tmp = dp[i-j*j]+1;
dp[i]=dp[i]<tmp?dp[i]:tmp;
}
}
//for(int i=0;i<=n;i++)cout<< dp[i]<<' ';
return dp[n];
}
};
  • BFS
class Solution {
public:
int numSquares(int n) {
if (n <= 0)return 0;
else if(pow((int)sqrt(n),2) == n)return 1;
vector<int> perfectSquares;//candinates
vector<int> cntPerfectSquares(n);//count_index
for (int i = 1; i*i <= n; i++)
{
perfectSquares.push_back(i*i);
cntPerfectSquares[i*i - 1] = 1; }
queue<int> searchQ;
for (auto& i : perfectSquares) searchQ.push(i);
int currCntPerfectSquares = 1;
while (!searchQ.empty())//BFS
{
currCntPerfectSquares++; int searchQSize = searchQ.size();
for (int i = 0; i < searchQSize; i++)
{
int tmp = searchQ.front();
for (auto& j : perfectSquares)
{
if (tmp + j == n)return currCntPerfectSquares;
else if ((tmp + j < n) && (cntPerfectSquares[tmp + j - 1] == 0))
{
cntPerfectSquares[tmp + j - 1] = currCntPerfectSquares;
searchQ.push(tmp + j);
}
else if (tmp + j > n)break;
}
searchQ.pop();
}
}
return 0;
}
};

leetcode279. Perfect Squares的更多相关文章

  1. Leetcode279. Perfect Squares完全平方数

    给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示例 1: 输入: n = 12 输出: 3 解释: 12 ...

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

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

  3. LeetCode Perfect Squares

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

  4. Perfect Squares

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

  5. CF914A Perfect Squares

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

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

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

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

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

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

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

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

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

随机推荐

  1. PLSQL_基础系列06_判断操作NVL / NULLIF / COALESCE / NVL2(案例)

    2014-12-08 Created By BaoXinjian

  2. PLSQL_闪回删除FlashBack Delete表误删除如何进行恢复(案例)

    2014-07-02 Created By BaoXinjian

  3. Form_Form页面跳转的四种方式(open_form, call_form, new_form, fnd_function)详解(汇总)

    2014-06-29 Created By BaoXinjian

  4. sizeof 和 strlen 区别

    Sizeof与Strlen的区别与联系 一.sizeof    sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型. ...

  5. PHP每日签到时怎么实现

    以淘宝网领取淘金币的签到系统为例:目标:第一天签到增加5个积分:第二天连续签到则增加8个积分:第三天连续签到,增加11个积分,第 四天连续签到,增加15个积分:第五天连续签到,增加19个积分:第六天连 ...

  6. Android 2.3 NFC简介

    Android 2.3加入了NFC(近场通讯)的支持.官网developer.android.com的英文介绍如下:Near Field Communications (NFC)Android 2.3 ...

  7. expdp impdp终极教学

    源地址:http://blog.csdn.net/giianhui/article/details/7788550

  8. expdp和impdp的用法

    源地址:http://blog.chinaunix.net/uid-23622436-id-2394094.html

  9. channelartlist标签调用实例

    channelartlist标签,大家都知道在DedeCMS的系统中,我们可以用这个标签进行循环子栏目及其栏目的文档数据,这也是DedeCMS系统中,唯一一个支持标签嵌套的调用标签,以DedeV5.6 ...

  10. 认识与学习BASH(中)

    1.在设置变量中:单引号与双引号的最大不同:双引号能保有变量的内容,单引号仅能是一般字符 2.反单引号(`)作用:在一串指令中,在‘之内的指令将会被先执行,其结果将作为外部的输入信息. locate指 ...