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

Example 1:

Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.

Example 2:

Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.
-----------------------------------------------------------------------------
这个题可以用BFS,不过要注意排除重复的。
C++代码:
class Solution {
public:
int numSquares(int n) {
if(n == )
return ;
queue<pair<int,int> > q;
q.push(make_pair(n,));
vector<bool> vis(n+,false); //用这个来避免把重复的数加进队列。
vis[n] = true;
while(!q.empty()){
int num = q.front().first;
int s = q.front().second;
q.pop();
if(num == )
return ;
for(int i = ; num - i*i >= ; i++){
int ans = num - i*i;
if(ans == )
return s;
if(!vis[ans]){
q.push(make_pair(ans,s+));
vis[ans] = true;
}
}
}
return ;
}
};

(BFS) leetcode 279. Perfect Squares的更多相关文章

  1. leetcode@ [279]Perfect Squares

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

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

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

  3. [leetcode] #279 Perfect Squares (medium)

    原题链接 题意: 给一个非整数,算出其最少可以由几个完全平方数组成(1,4,9,16……) 思路: 可以得到一个状态转移方程  dp[i] = min(dp[i], dp[i - j * j] + ) ...

  4. [LeetCode 279.] Perfect Squres

    LeetCode 279. Perfect Squres DP 是笨办法中的高效办法,又是一道可以被好办法打败的 DP 题. 题目描述 Given a positive integer n, find ...

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

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

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

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

  7. 【leetcode】Perfect Squares (#279)

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

  8. 279. Perfect Squares

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

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

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

随机推荐

  1. java中的sql语句中如果有like怎么写

    我先是在SQL server中写了如下语句: 这样是顺利执行的,可是我把这句话复制到Java代码中打出来却报错了, 刚开始我还以为是前端没有传回来值,待我一句一句打印发现,它提示我rs没有next.到 ...

  2. 爬虫基础--IO多路复用单线程异步非阻塞

    最近一直的学习爬虫  ,进行基础的学习 性能相关 参考 https://www.cnblogs.com/wupeiqi/p/6229292.html # 目标:单线程实现并发HTTP请求 # # so ...

  3. C#中Windows窗体工具栏

    标签(Label):用于显示用户不能编辑的文本或图像.常使用它对窗体上的其他各种控件进行标注或说明. 文本框(TextBox):用于获取用户名输入的信息或向用户显示的文本. 组合框(ComboBox) ...

  4. LVS+Keepalived实现mysql的负载均衡

    1 初识LVS:Linux Virtual Server 1.1 LVS是什么       LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起 ...

  5. Postman安装及入门教程

    安装 本文只是基于 Chrome 浏览器的扩展插件来进行的安装,并非单独应用程序. 首先,你要台电脑,其次,安装有 Chrome 浏览器,那你接着往下看吧. 1. 官网安装(别看) 打开官网,http ...

  6. 0109 ubuntu nginx ssl

    1. sudo apt-get install openssl libssl-dev # ./configure --with-http_stub_status_module --with-http_ ...

  7. Apache Flink中的广播状态实用指南

    感谢英文原文作者:https://data-artisans.com/blog/a-practical-guide-to-broadcast-state-in-apache-flink 不过,原文最近 ...

  8. logrotate异常排查

    [root@dev240 logrotate.d]# /usr/sbin/logrotate -v /etc/logrotate.conf reading config file /etc/logro ...

  9. Java之匿名内部类详解

    前言 本文讲解Java中最后一种内部类,叫做匿名内部类.顾名思义,所谓的匿名内部类就是一个没有显式的名字的内部类,在实际开发中,此种内部类用的是非常多的. 匿名内部类 本质:匿名内部类会隐式的继承一个 ...

  10. call()与apply()区别typeof和instanceof的区别

    摘自 http://www.cnblogs.com/qzsonline/archive/2013/03/05/2944367.html 一.方法的定义 call方法: 语法:call(thisObj, ...