题目

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 完全平方数的更多相关文章

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

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

  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 完全平方数

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

  4. leetcode@ [279]Perfect Squares

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

  5. 一、Perfect Squares 完全平方数

    一原题 Given a positive integer n, find the least number of perfect square numbers (, , , , ...) which ...

  6. Leetcode279. Perfect Squares完全平方数

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

  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. (BFS) leetcode 279. Perfect Squares

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

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

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

随机推荐

  1. 手工下载php的composer软件包,如何让项目自动加载包里的类

    有的时候需要手工下载php的composer包 1.将下载好的包放到项目的vendor目录下,比如包名:pinguo/php-aop 2.然后查看软件包目录(vendor/pinguo/php-aop ...

  2. Windows 7安装Service Pack 1失败问题

    问题 很多朋友在电脑上安装某些软件时,会发现安装不了,提示信息大多是"本软件只支持 Windows 7 SP1 及更新版本"等等.这说明,你的电脑仍然在运行最早的Windows7版 ...

  3. Python 内置函数--range() xrange()

    >>>xrange(8) xrange(8) >>> list(xrange(8)) [0, 1, 2, 3, 4, 5, 6, 7] >>> r ...

  4. Centos7修改profile错误的解救方案

    在改profile的时候,改出问题了,除了cd以外的命令基本都不能用了.使用root用户执行: # export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11 ...

  5. word 转化为PDF 目录没了

    https://jingyan.baidu.com/article/ca00d56c5d5f8de99febcf54.html 点文件-->导出-->创建PDF-->选项--> ...

  6. Windows下mysql安装配置问题

    下载最新版的mysql: https://dev.mysql.com/downloads/mysql/ 下载完成后解压打开安装包如下 打开cmd以管理员身份运行(一定要以管理员身份运行) 然后输入命令 ...

  7. DP(动态规划)总结

    前言 动态规划是很重要的一个知识点,大大小小的比赛总会有一两道DP题,足以说明动态规划的重要性. 动态规划主要是思想,并没有固定的模板,那么,怎么判断题目是不是动态规划呢? DP题一般都会满足三个条件 ...

  8. python实战项目 — 爬取 校花网图片

    重点: 1.  指定路径创建文件夹,判断是否存在 2. 保存图片文件 # 获得校花网的地址,图片的链接 import re import requests import time import os ...

  9. Java 总结篇2

    第02章:数据类型和运算符 一.概述: 1.数据类型:int.float.char.boolean 2.运算符:算术运算符.赋值运算符.关系运算符.逻辑运算符.位运算符(了解即可).条件运算符 3.基 ...

  10. DFS集训

    2019-07-29 09:01:06 A PARTY A company has n employees numbered from 1 to n. Each employee either has ...