[LeetCode] 0279. 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.
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 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的题。
class Solution {
public:
int numSquares(int n) {
if (n == 0) {
return 0;
}
vector<int> steps(n + 1, INT_MAX);
steps[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j * j <= i; ++j) {
steps[i] = min(steps[i], steps[i - j * j] + 1);
}
}
return steps[n];
}
};
解法二
最短路径,BFS
参考资料:https://blog.csdn.net/qq_17550379/article/details/80875782
思路:寻找最短路径嘛,肯定是用到广度优先搜索了,不断拆解队列中的数字,得到答案后直接返回就是最短路径了。具体的做法很简单,以下是具体BFS思路:
把n推入队列,然后取出来进行拆解(-1,-4,-9…),减掉后如果不等于0,就将余下来的值推入队列,等待第二轮搜索,如果减掉后等于0就是直接找到答案了,返回step+1即可,不需要再搜索下去了(记得要有visited昂,因为是个循环图)。
class Solution {
public:
int numSquares(int n) {
queue<pair<int, int>> q;
// pair.first 代表将n拆解后的数字,pair.second代表所走的步数
// 刚开始推入未拆解的n,步数为0
q.push(make_pair(n, 0));
bool visited[n + 1];
memset(visited, 0, sizeof(visited));
visited[n] = true;
// 开始广度优先搜索
while (!q.empty()) {
// 取队头,进行拆解
auto pair = q.front();
q.pop();
int i = 1;
int next_num = pair.first - i * i;
// 在推入队列前先看看能不能解答
while (next_num >= 0) {
if (next_num == 0) {
return pair.second + 1;
}
// 还有余数没扣完,就将可以的下一步都推入队列
if (!visited[next_num]) {
q.push(make_pair(next_num, pair.second + 1));
visited[next_num] = true;
}
// 计算下一步
i++;
next_num = pair.first - i * i;
}
}
return 0;
}
};
运行结果:
Runtime: 16 ms, faster than 86.01% of C++ online submissions for Perfect Squares.
Memory Usage: 11.6 MB, less than 24.62% of C++ online submissions for Perfect Squares.
解法三
Lagrange四平方定理:任何一个正整数都可以表示成不超过四个整数的平方之和。 推论:满足四数平方和定理的数n(四个整数的情况),必定满足 n=4^a(8b+7)。
做计算机的数学也要好啊,这个定理告诉我们,这道题的答案无非只有1、2、3、4,而且如果满足上述公式,那么答案就是4,如果不能被1个或2个完全平方数组成,那么就返回3。
(真是暴力啊,逃……
class Solution {
public:
int numSquares(int n) {
// Lagrange四平方定理:任何一个正整数都可以表示成不超过四个整数的平方之和。
// 推论:满足四数平方和定理的数n(四个整数的情况),必定满足 n=4^a(8b+7)。
while (n % 4 == 0)
n /= 4;
if (n % 8 == 7)
return 4;
int a = 0;
while (a * a <= n) {
int b = (int)sqrt(n - a * a);
if (a * a + b * b == n) {
return (a ==0 || b == 0) ? 1 : 2;
}
a++;
}
return 3;
}
};
运行结果:
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Perfect Squares.
Memory Usage: 8.4 MB, less than 90.15% of C++ online submissions for Perfect Squares.
[LeetCode] 0279. Perfect Squares 完全平方数的更多相关文章
- [LeetCode] 279. Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- [LintCode] Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- [LeetCode] 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
https://leetcode.com/problems/perfect-squares/ Given a positive integer n, find the least number of ...
- 一、Perfect Squares 完全平方数
一原题 Given a positive integer n, find the least number of perfect square numbers (, , , , ...) which ...
- Leetcode279. Perfect Squares完全平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示例 1: 输入: n = 12 输出: 3 解释: 12 ...
- 【leetcode】Perfect Squares (#279)
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- (BFS) 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] + ) ...
随机推荐
- Python 安装 MySQL-python ImportError: No module named 'ConfigParser'
系统: CentOS-6.4-x86_64 Python : Python 3.4.5 和 Python 3.5.2 安装 MySQL-python ,结果出错: ImportError: No mo ...
- nginx+consul-template+consul实现自动负载均衡
所需工具:工具 下载地址 本文使用版本consul https://www.consul.io/downloads.html consul_1.0.7_linux_amd64.zipconsul-te ...
- Laravel 数据库实例教程 —— 使用查询构建器对数据库进行增删改查
原文地址:https://blog.csdn.net/lmy_love_/article/details/72832259 获取查询构建器很简单,还是要依赖DB门面,我们使用DB门面的table方法, ...
- 最新 淘友天下java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.淘友天下等10家互联网公司的校招Offer,因为某些自身原因最终选择了淘友天下.6.7月主要是做系统复习.项目复盘.Leet ...
- TCP/IP学习笔记5--网络的构成要素
人的灵魂来自一个完美的家园,那里没有任何污秽和丑陋,只有纯净和美丽.----大鱼海棠 1.通信媒介与数据链路 计算机之间通过各种电缆相互连接. 2.网卡 任何一台计算机接入网络都需要网卡,又称网络适配 ...
- log4j测试示例
引入依赖 <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId> ...
- Feign输出Info级别日志
背景 spring cloud netfix组件中,feign相关的日志默认是不会输出的,需要自定义配置才能输出,并且Feign只对Debug基本的日志做出响应, 实际业务需要输出Info级别的日志, ...
- get merge --no-ff和git merge区别、git fetch和git pull的区别
get merge --no-ff和git merge区别 git merge -–no-ff可以保存你之前的分支历史.能够更好的查看 merge历史,以及branch 状态. git merge则不 ...
- resnet的理解-- 面试笔记
上周参加了XX大学研究生推免的面试,面试老爷问到了resnet主要解决了什么问题,我下意识的回答到解决了当网络加深的时候会出现的vanishing/exploding gradients,然后面试老爷 ...
- c# 基于RTMP推流 PC+移动端 拉流播放
网上关于直播相关的文章很多,但是讲解还是不够系统,对于刚刚接触直播开发的朋友实施起来会浪费不少时间.下面结合我自己的经验, 介绍一下直播方面的实战经验. 分成两个部分 第一部分是标题中介绍的基于RTM ...