Leetcode: Perfect Squares
2015-12-26 04:57
323 查看
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.
public class Solution { public int numSquares(int n) { int[] dp = new int[n+1]; Arrays.fill(dp, Integer.MAX_VALUE); dp[0] = 0; for (int i=1; i<=n; i++) { int sqrt = (int)Math.sqrt(i); for (int j=1; j<=sqrt; j++) { dp[i] = Math.min(dp[i], dp[i-j*j]+1); } } return dp ; } }
相关文章推荐
- Effective Java 阅读笔记——方法
- Golang之bytes.buffer
- web标准化设计:常用的CSS命名规则手册
- 《JavaScript设计模式与开发实践》-面向对象的JavaScript
- Javascript观察者模式
- Visual Studio 编译错误【error C4996: 'scanf': This function or variable may be unsafe. 】的解决方案
- js数组
- JavaScript动态设置div的样式的方法
- JavaScript位置与大小(1)之正确理解和运用与尺寸大小相关的DOM属性
- 戏说HTML5
- HTML中段落首字下沉的实现方法
- JSON 解析
- JSP中的EL表达式详细介绍 - Mr. David
- HTML 视频(Videos)
- HTML 实例
- HTML 参考手册
- HTML 事件属性
- html中的布局div
- html中书写表单
- js简易版二级城市联动