一、Perfect Squares 完全平方数
一原题
Given a positive integer n, find the least number of perfect square numbers (for example, , , , , ...) which sum to n. For example, given n = , return because = + + ; given n = , return because = + .
二、中文讲解
、这道题说是给我们一个正整数,求它最少能由几个完全平方数组成。这道题是考察四平方和定理
、根据四平方和定理,任意一个正整数均可表示为4个整数的平方和,其实是可以表示为4个以内的平方数之和,
那么就是说返回结果只有1,,3或4其中的一个,首先我们将数字化简一下,由于一个数如果含有因子4,那么我们
可以把4都去掉,并不影响结果,比如2和8,3和12等等,返回的结果都相同
、还有一个可以化简的地方就是,如果一个数除以8余7的话,那么肯定是由4个完全平方数组成
、那么做完两步后,一个很大的数有可能就会变得很小了,大大减少了运算时间,下面我们就来尝试的将其拆为
两个平方数之和,如果拆成功了那么就会返回1或2,因为其中一个平方数可能为0. (注:由于输入的n是正整数,所以不存在两个平方数均为0的情况)。注意下面的!!a + !!b这个表达式,可能很多人不太理解这个的意思,其实很简单,感叹号!表示逻辑取反,那么一个正整数逻辑取反为0,再取反为1,所以用两个感叹号!!的作用就是看a和b是否为正整数,都为正整数的话返回2,只有一个是正整数的话返回1
三、代码
class Solution:
# @param {int} n a positive integer
# @return {int} an integer
def numSquares(self, n):
# Write your code here
while n % 4 == 0:
n /= 4
if n % 8 == 7:
return 4 for i in xrange(n+1):
temp = i * i
if temp <= n:
if int((n - temp)** 0.5 ) ** 2 + temp == n:
return 1 + (0 if temp == 0 else 1)
else:
break
return 3
其他代码解法:
class Solution {
public:
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 ;
}
};
一、Perfect Squares 完全平方数的更多相关文章
- [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 ...
- [LeetCode] Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- [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 ...
- 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 ...
- LeetCode Perfect Squares
原题链接在这里:https://leetcode.com/problems/perfect-squares/ 题目: Given a positive integer n, find the leas ...
随机推荐
- 调用iPhone的短信
不传递短信内容,可以调用下面的方法: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://4664 ...
- nginx 使用ngx_cache_purge清除缓存
location ~ ^/myclear(/.*) { allow 10.0.0.0/8; allow 10.28.100.0/24; allow 127.0.0.1; deny all; pro ...
- maven初始搭建一个基础项目(spring mvc+spring+jdbc mysql+jstl)
技术选型: 一.项目搭建: 1)创建maven项目 (我博客里面有介绍) 选择aptach的maven-archetype-webapp 填入groupIDhe artifactId等 确认项目名称 ...
- 自动清理DataGuard备机日志
>> from zhuhaiqing.info #!/usr/bin/bash #删除DataGuard备机归档日志备份 export ORACLE_HOME=/opt/oracle/pr ...
- redis+node.js
1.什么的cache 是一种更快的记忆存储数据集 存储空间有限 储存一部分重要数据 是一种相对的概念,只要比原本数据存储更快的介质就能作为cache 2.caching 策略 有限的存储空间,只能存储 ...
- 很全的php数组操作方法
一.数组操作的基本函数 数组的键名和值 array_values($arr);获得数组的值 array_keys($arr);获得数组的键名 array_flip($arr);数组中的值与键名互换(如 ...
- Mac下python连接mysql数据库
一.下载Mysql官方connector驱动 地址:https://dev.mysql.com/downloads/connector/python/ 根据提示安装.dmg文件即可. 二.验证是否安装 ...
- 什么是bin文件?
知道多问bin文件几个为什么.是在出现下面这个问题时引发的. 出现这种问题:未能载入文件或程序集"DAL"或它的某一个依赖项. 系统找不到指定的文件 ...
- 图片剪裁控件——ClipImageView
这段时间在做自己的项目时,须要使用到图片剪裁功能,当时大概的思考了一些需求.想到了比較简单的实现方法.因此就抽了点时间做了这个图片剪裁控件--ClipImageView 这里先贴上ClipImageV ...
- TP框架的修改,删除
先把数据库的素具显示出来 public function xiugai() { $code= "n001";//修改的主键值 $n = M("nation"); ...