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. ServiceStack.Redis连接阿里云redis服务时使用连接池出现的问题

    创建连接池 private static PooledRedisClientManager prcm = CreateManager(new string[] { "password@ip: ...

  2. 时序数据库InfluxDB安装及使用

    时序数据库InfluxDB安装及使用 1 安装配置 安装 wget https://dl.influxdata.com/influxdb/releases/influxdb-1.3.1.x86_64. ...

  3. 使用Spring.Net

    一:在Asp.net MVC中应该怎样使用Spring.Net? 1:先导入dll文件. 2:将案例中的Config文件夹拷贝到项目中. 3:修改Config文件夹中的相关的配置信息. 4:修改Web ...

  4. Linux内核的冷热缓存

    缓存为什么会有冷热? 究其原因,是因为对于内存的访问,可能是CPU发起的,也可以是DMA设备发起的. 如果是CPU发起的,在CPU的硬件缓存中,就会保存相应的页内容.如果这个页本来没有存在于硬件缓存中 ...

  5. c/c++ 网络编程 UDP 主机网络信息取得

    网络编程 UDP 主机网络信息取得 1,if_nametoindex 通过网卡名字取得网卡编号 2,if_indextoname 通过网卡编号取得网卡名字 #include <stdio.h&g ...

  6. 5.5Python数据处理篇之Sympy系列(五)---解方程

    目录 目录 前言 (一)求解多元一次方程-solve() 1.说明: 2.源代码: 3.输出: (二)解线性方程组-linsolve() 1.说明: 2.源代码: 3.输出: (三)解非线性方程组-n ...

  7. css3新特性合集

    转自:https://www.cnblogs.com/xiaoxie2016/p/5964694.html (若原作者对此转载有疑问,联系删除,谢谢!) animation    IE10 anima ...

  8. Maven的简单使用

    Maven使用 在官网下载maven: http://maven.apache.org/download.cgi 解压到D盘(位置随便) 配置环境变量 打开dos窗口,检测是否成功,出现如下画面表示配 ...

  9. vue 快速入门、常用指令(1)

    1. vue.js的快速入门使用 1.1 vue.js库的下载 vue.js是目前前端web开发最流行的工具库之一,由尤雨溪在2014年2月发布的. 官方网站 中文:https://cn.vuejs. ...

  10. php常用数组array函数实例总结【赋值,拆分,合并,计算,添加,删除,查询,判断,排序】

    本文实例总结了php常用数组array函数.分享给大家供大家参考,具体如下: array_combine 功能:用一个数组的值作为新数组的键名,另一个数组的值作为新数组的值 案例: <?php ...