LeetCode(279)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.
For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.
分析
完美平方数,给定任意数n,它可表示为多个平方数(如1,4,9,16…)的和,求出表示出任意数n所需的平方数的最少个数。
考察动态规划,
如果一个数x可以表示为一个任意数a加上一个平方数b∗b,也就是x=a+b∗b,那么能组成这个数x最少的平方数个数,就是能组成a最少的平方数个数加上1(因为b∗b已经是平方数了)。
AC代码
class Solution {
public:
/* 如果一个数x可以表示为一个任意数a加上一个平方数bxb,也就是x = a + bxb,
* 那么能组成这个数x最少的平方数个数,就是能组成a最少的平方数个数加上1(因为b*b已经是平方数了)。
*/
int numSquares(int n) {
// 将所有非平方数的结果置最大,保证之后比较的时候不被选中
vector<int> nums(n + 1, INT_MAX);
// 将所有整平方数的结果置1
for (int i = 0; i*i <= n; ++i)
{
nums[i*i] = 1;
}//for
// 从小到大找任意数a
for (int a = 0; a <= n; ++a)
{
// 从小到大找平方数b*b
for (int b = 0; a + b*b <= n; ++b)
{
// 因为a+b*b可能本身就是平方数,所以我们要取两个中较小的
nums[a + b*b] = min(nums[a] + 1, nums[a + b*b]);
}//for
}//for
return nums[n];
}
};
LeetCode(279)Perfect Squares的更多相关文章
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- 关于float和double类型能表示的数据范围和精度分析
来自教材<计算机组成原理>p16 float:6--7位 double:15--16位 意思就是double类型的数据,你确实能表达出很大的数字,但是其只有15位是精确的. 1.计算机中, ...
- 洛谷P1057 传球游戏
f[i][j]表示第i轮j拿到球的方案数 转移:f[i][j]=f[i-1][j+1] +f[i-1][j+-1].注意: 边界f[0][1]=1; 还有当j=1或N时 #include<ios ...
- EF 记录执行的sql语句
最近做了个中等的项目,数据不会很多,开发时间比较紧迫,所以用了EF的框架. 在使用过程中,发现有时候执行的结果不如预期,想看看执行的sql语句为何,遍查找资料,在网上找到了相关辅助类,拿来使用,部署到 ...
- ecshop分类页把分类描述改成FCKeditor编辑器
最近放一个网站 http://www.macklin.cn/productline/35 有个产品分类页面需要添加分类缩略图和图文的描述 一.首先说下添加分类缩略图的步骤吧 1,依葫芦画瓢,参照的是e ...
- If people in the communications only think about gains and losses of interest, then the pleasure of knowing each other will cease to exist.
If people in the communications only think about gains and losses of interest, then the pleasure of ...
- MapReduce的编程思想(1)
MapReduce的编程思想(1) MapReduce的过程(2) 1. MapReduce采用分而治之的思想,将数据处理拆分为主要的Map(映射)与Reduce(化简)两步,MapReduce操作数 ...
- SharePoint 2016 功能比较
SharePoint 2016中有很多功能.我们经常和客户谈论SharePoint安装时,我问他们是否计划安装SharePoint Server 2016 Standard或Enterprise.通常 ...
- vuex的state,mutation,getter,action
开始!正常的简单的拆分下是这样的文件当然module可以在store下面新建一个文件夹用来处理单独模块的vuex管理比较合适. 1.index.js下面 import Vue from 'vue' i ...
- PostgreSQL: epoch 新纪元时间的使用
新纪元时间 Epoch 是以 1970-01-01 00:00:00 UTC 为标准的时间,将目标时间与 1970-01-01 00:00:00时间的差值以秒来计算 ,单位是秒,可以是负值; 有些应用 ...
- jsp另外五大内置对象之response-操作cookie
responseo3.jsp <%@ page language="java" contentType="text/html; charset=utf-8" ...