[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 ...
随机推荐
- eclipse项目中关于导入的项目里提示HttpServletRequest 不能引用的解决办法
eclipse项目中关于导入的项目里提示HttpServletRequest 不能引用的解决办法 当使用eclipse导入外部的web工程时,有时会提示HttpServletRequest, Serv ...
- 注解:【有连接表的】Hibernate单向1->1关联
Person与Address关联:单向1->1,[有连接表的] (使用较少!) Person.java package org.crazyit.app.domain; import javax. ...
- MongoDB学习(1)—在Windows系统中安装MongoDB
概述 本文主要介绍在Windows系统安装MongoDB的方法. MongoDB官方网址:http://www.mongodb.org/,最新版本为2.6.7. 注意: 从2.2版本开始,MongoD ...
- WPF标准控件模板查看程序(文件里面)
xaml <Window x:Class="ControlTemplateBrowser.MainWindow" xmlns="http://schemas.mic ...
- python 类型判断-- isinstance函数
判断类型 函数isinstance()可以判断一个变量的类型,既可以用在Python内置的数据类型如str.list.dict,也可以用在我们自定义的类,它们本质上都是数据类型. 假设有如下的 Per ...
- Android 编程下 DP、SP 以及屏幕像素密度
有时需为视图属性指定大小尺寸值(通常以像素为单位,但有时也用点.毫米或英寸).最常见的属性有: 文字大小(Text Size),指设备上显示的文字像素高度: 边距(Margin),指定视图组件间的距离 ...
- 自定义 ActionBar 标题与菜单中的文字样式
自定义标题文字样式 标题样式是 ActionBar 样式的一部分,所以要先定义 ActionBar 的样式 <style name="AppTheme" parent=&qu ...
- BeanShell用法汇总(部分摘抄至网络)【转】
说明:本文部分资料摘抄至 来源: http://www.cnblogs.com/puresoul/p/4915350.html 来源: http://www.cnblogs.com/puresoul/ ...
- 学习资源asp.net
http://www.runoob.com ajax 同一表单,多部分提交.增加,修改,删除 服务器端控件: http://technet.microsoft.com/zh-cn/library/cc ...
- webvnc利器-noVNC集成实战
我们在做一个虚拟机管理平台,底层虚拟化使用KVM,我们希望在网页上操作虚拟机. 一开始用applet嵌在网页内实现webvnc,不过由于applet的在浏览器上运行安全性限制,我们需要一个新的方案.此 ...