[LintCode] 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
Given n = 12, return 3 because 12 = 4 + 4 + 4
Given n = 13, return 2 because 13 = 4 + 9
LeetCode上的原题,请参见我之前的博客Perfect Squares。
解法一:
class Solution {
public:
/**
* @param n a positive integer
* @return an integer
*/
int numSquares(int n) {
while (n % == ) n /= ;
if (n % == ) return ;
for (int a = ; a * a <= n; ++a) {
int b = sqrt(n - a * a);
if (a * a + b * b == n) {
return !!a + !!b;
}
}
return ;
}
};
解法二:
class Solution {
public:
/**
* @param n a positive integer
* @return an integer
*/
int numSquares(int n) {
while (n % == ) n /= ;
if (n % == ) return ;
vector<int> dp(n + , INT_MAX);
dp[] = ;
for (int i = ; i < n; ++i) {
for (int j = ; i + j * j <= n; ++j) {
dp[i + j * j] = min(dp[i + j * j], dp[i] + );
}
}
return dp.back();
}
};
解法三:
class Solution {
public:
/**
* @param n a positive integer
* @return an integer
*/
int numSquares(int n) {
while (n > && n % == ) n /= ;
if (n % == ) return ;
int res = n, i = ;
while (i * i <= n) {
int a = n / (i * i), b = n % (i * i);
res = min(res, a + numSquares(b));
++i;
}
return res;
}
};
[LintCode] Perfect Squares 完全平方数的更多相关文章
- [LeetCode] 0279. Perfect Squares 完全平方数
题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...
- [LeetCode] Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- 一、Perfect Squares 完全平方数
一原题 Given a positive integer n, find the least number of perfect square numbers (, , , , ...) which ...
- [LeetCode] 279. Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- Leetcode279. Perfect Squares完全平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示例 1: 输入: n = 12 输出: 3 解释: 12 ...
- Lintcode Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example,1, 4, 9, 16 ...
- LeetCode 279. 完全平方数(Perfect Squares) 7
279. 完全平方数 279. Perfect Squares 题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数 ...
- Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares)
Leetcode之广度优先搜索(BFS)专题-279. 完全平方数(Perfect Squares) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ar ...
- CF914A Perfect Squares
CF914A Perfect Squares 题意翻译 给定一组有n个整数的数组a1,a2,…,an.找出这组数中的最大非完全平方数. 完全平方数是指有这样的一个数x,存在整数y,使得x=y^2y2 ...
随机推荐
- Windows Path设置
win7系统环境变量path的两种设置方法 环境变量Path 环境变量是在操作系统中一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息.例如Windows和DOS操作系统中 ...
- Request和Response对象
Request 和 Response 对象起到了服务器与客户机之间的信息传递作用.Request 对象用于接收客户端浏览器提交的数据,而 Response 对象的功能则是将服务器端的数据发送到客户端浏 ...
- 模板模式与策略模式/template模式与strategy模式/行为型模式
模板模式 模版模式,又被称为模版方法模式,它可以将工作流程进行封装,并且对外提供了个性化的控制,但主流程外界不能修改,也就是说,模版方法模式中,将工作的主体架构规定好,具体类可以根据自己的需要,各自去 ...
- VPS -Digital Ocean -搭建一个最简单的web服务器
简单的也是美的 在一个目录放自己的几个showcase网页方便和别人分享,最简单的方式是什么 创建文件夹,放入自己的网页文件 在目录下执行 $ nohup python -m SimpleHTTPSe ...
- 通讯录(ios自带有界面)
1.添加AddressBookUI.framework框架 2控制器中实现 #import "ViewController.h" #import <AddressBookUI ...
- python学习第三天
小结: 总体上,python是解释型语言,开源比较好,速度较慢,装逼神器,解释器较常用的是CPython,安装后python进入运行环境 exit()退出 第一个hello world : print ...
- hbase伪分布式安装(转)
原文地址:http://blog.csdn.net/yonghutwo/article/details/24555103 本机环境: ubuntu 12.4 Hadoop 1.1.2 安装hbase版 ...
- MapReduce中的分区方法Partitioner
在进行MapReduce计算时,有时候需要把最终的输出数据分到不同的文件中,比如按照省份划分的话,需要把同一省份的数据放到一个文件中:按照性别划分的话,需要把同一性别的数据放到一个文件中.我们知道最终 ...
- MPAndroidChart饼图属性及相关设置
公司最近在做统计功能,所以用到了饼图,在网上查了一些资料最终决定使用MPAndroidChart,使用起来非常方便,还有一些问题通过各种查找,终于解决...废话不多说,先看下效果图: 布局文件: &l ...
- Centos系统上安装VNC view
一.安装 VNC 默认情况下,CentOS 6.4 是没有安装的,检查是否安装,输入: [root@localhost ~]# rpm -q vnc vnc-server package vnc is ...