(BFS) leetcode 279. Perfect Squares
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的更多相关文章
- leetcode@ [279]Perfect Squares
https://leetcode.com/problems/perfect-squares/ Given a positive integer n, find the least number of ...
- [LeetCode] 279. Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- [leetcode] #279 Perfect Squares (medium)
原题链接 题意: 给一个非整数,算出其最少可以由几个完全平方数组成(1,4,9,16……) 思路: 可以得到一个状态转移方程 dp[i] = min(dp[i], dp[i - j * j] + ) ...
- [LeetCode 279.] Perfect Squres
LeetCode 279. Perfect Squres DP 是笨办法中的高效办法,又是一道可以被好办法打败的 DP 题. 题目描述 Given a positive integer n, find ...
- [LeetCode] 0279. Perfect Squares 完全平方数
题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...
- 【LeetCode】279. Perfect Squares 解题报告(C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 四平方和定理 动态规划 日期 题目地址:https: ...
- 【leetcode】Perfect Squares (#279)
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- 279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- 279. Perfect Squares(动态规划)
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
随机推荐
- 会话固定攻击 - yxcms session固定漏洞
目录 会话固定攻击 e.g. yxcms session固定攻击 分析 了解更多 会话固定攻击 Session fixation attack(会话固定攻击)是利用服务器的session不变机制,借他 ...
- 关于 MongoDB 与 SQL Server 通过本身自带工具实现数据快速迁移 及 注意事项 的探究
背景介绍 随着业务的发展.需求的变化,促使我们追求使用不同类型的数据库,充分发挥其各自特性.如果决定采用新类型的数据库,就需要将既有的数据迁移到新的数据库中.在这类需求中,将SQL Server中的数 ...
- selenium-确定找到的element唯一(三)
在python + selenium 中经常会遇到找到的元素不唯一,导致定位到的元素不是预期的或者定位不到元素 解决方法:只要在页面进行确认找到的元素唯一后,再进行操作 页面确认方法: 1.通过htm ...
- 【原】Java学习笔记004 - 运算符
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 运算符:对常量 或 ...
- c/c++ 重载运算符 ==和!=的重载
重载运算符 ==和!=的重载 问题:假如有一个类似于vector的类,这个类只能存放string,当有2个这个类的对象时,如何比较这2个对象. 自己重载==和!= 代码(重载==,!=) #inclu ...
- 进程命令(taskkill)
taskkill 命令: // 描述: 结束一个或多个任务或流程. // 语法: taskkill [/s <computer> [/u [<Domain>\]<User ...
- 前端面试必备的css盒子模型
今天同学发给了我一份前端基础的面试题,第一道便是对css盒子模型的理解,我看到的第一眼想到的是div,然后就...懵逼了,知其然不知其所以然.所以打算写一写盒子模型的概念理解啥的,如有写的不当的地方, ...
- 【Linux基础】iconv命令详解(编码转换)
对于给定文件把它的内容从一种编码转换成另一种编码. iconv -f GBK -t UTF- file1 -o file2 //将GBK转换为UTF8,输出到file2.没-o那么会输出到标准输出 i ...
- C#基础知识之字符串比较方法:“==”操作符;RefernceEquals;String.Equals方法;String.Compare方法;String.CompareOrdinal方法。
一.“==”操作符:String.Equals:ReferenceEquals 方法 1.在编程中实际上我们只需要这两种比较,c#中类型也就这两种 (1)值类型的比较:一般我们就是判断两个值类型实例各 ...
- .NET CORE学习笔记系列(5)——ASP.NET CORE的运行原理解析
一.概述 在ASP.NET Core之前,ASP.NET Framework应用程序由IIS加载.Web应用程序的入口点由InetMgr.exe创建并调用托管,初始化过程中触发HttpApplicat ...