Perfect Squares——Leetcode
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...
) which sum to n.
For example, given n = 12
, return 3
because 12 = 4 + 4 + 4
; given n = 13
, return 2
because 13 = 4 + 9
.
题目大意:给一个正整数,找出可以由最少个平方数构成的数量是多少。
解题思路:dynamic programming,递推公式F[n]=min{F[n-1]+1,F[n-4]+2,F[n-9]+3,...};
public class Solution {
int[] f;
public int numSquares(int n) {
f=new int[n+1];
Arrays.fill(f,Integer.MAX_VALUE);
f[1]=1;
for(int i=2;i<=n;i++){
int r = (int)Math.sqrt(i);
if(i==r*r){
f[i]=1;
continue;
}
for(int j=1;j<=r;j++){
f[i]=Math.min(f[i],f[i-j*j]+1);
}
}
return f[n];
}
}
Perfect Squares——Leetcode的更多相关文章
- LeetCode Perfect Squares
原题链接在这里:https://leetcode.com/problems/perfect-squares/ 题目: Given a positive integer n, find the leas ...
- 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 ...
- 花式求解 LeetCode 279题-Perfect Squares
原文地址 https://www.jianshu.com/p/2925f4d7511b 迫于就业的压力,不得不先放下 iOS 开发的学习,开始走上漫漫刷题路. 今天我想聊聊 LeetCode 上的第2 ...
- [LeetCode] 0279. Perfect Squares 完全平方数
题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...
- [LintCode] Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- Perfect Squares
Perfect Squares Total Accepted: 18854 Total Submissions: 63048 Difficulty: Medium Given a positive i ...
- CF914A Perfect Squares
CF914A Perfect Squares 题意翻译 给定一组有n个整数的数组a1,a2,…,an.找出这组数中的最大非完全平方数. 完全平方数是指有这样的一个数x,存在整数y,使得x=y^2y2 ...
- Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩
题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...
随机推荐
- CCProcxy代理服务器的配置使用
资源准备及设置 1.资源:http://www.ccproxy.com/ 下载官方正式版本. 2.解压之后打开,界面如下: 打开“设置”,如图做设置,点击确定: 打开“账号”: 点击新建,在ip地址/ ...
- Windows 7 + Visual Studio 2012 + cocos2d-x 2.1.5
下载cocos2d-x google code : http://code.google.com/p/cocos2d-x/downloads/list cocos2d 官网: http://cocos ...
- IOS-图片上传到服务器
//获取document 路径- (NSString *)getDocumentPath{ NSArray *paths = NSSearchPathForDirectoriesInDomain ...
- 在ASP.NET 中调用RSACryptoServiceProvider失败,提示未找到文件
在本地开发环境调试下,调试这个RSA加密是没问题的,但是部署到IIS就会报错. 原来是,在本地vs调试与IIS上运行是存在权限差异的.本地调试权限最大,IIS 次之. 所以在我们声明CspParame ...
- 数据库(学习整理)----5--Oracle常用的组函数
其他: 1.oracle中下标是从1开始的,Java下标是从0开始的 函数分类: 日期函数 字符函数 转换函数 数学函数 系统函数 ---在当前月份上面:增加.减少月份 select add_mont ...
- dx环境搭建
我使用的是vs2012+DXSDK_Jun10 DXSDK_Jun10下载地址http://download.microsoft.com/download/A/E/7/AE743F1F-632B-48 ...
- python模块学习 logging
1.简单的将日志打印到屏幕 import logging logging.debug('This is debug message') logging.info('This is info messa ...
- 百度地图Api 根据两个坐标点计算距离
百度地图Android Sdk的Api里面,没有现成的直接获取两个坐标点之间距离的方法,但是,在jsapi里面,有直接计算距离的方法. class Point: pass def max(a,b): ...
- javascript图片预先加载
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- OpenCV例程实现人脸检测
前段时间看的OpenCV,其实有很多的例子程序,参考代码值得我们学习,对图像特征提取三大法宝:HOG特征,LBP特征,Haar特征有一定了解后. 对本文中的例子程序刚开始没有调通,今晚上调通了,试了试 ...