63.Perfect Squares(完美平方数)
Level:
Medium
题目描述:
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.
Example 1:
Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.
Example 2:
Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.
思路分析:
根据四平方和定理,任意一个正整数均可表示为4个整数的平方和,其实是可以表示为4个以内的平方数之和,那么就是说返回结果只有1,2,3或4其中的一个,首先我们将数字化简一下,由于一个数如果含有因子4,那么我们可以把4都去掉,并不影响结果,比如2和8,3和12等等,返回的结果都相同,读者可自行举更多的栗子。还有一个可以化简的地方就是,如果一个数除以8余7的话,那么肯定是由4个完全平方数组成,这里就不证明了,因为我也不会证明,读者可自行举例验证。那么做完两步后,一个很大的数有可能就会变得很小了,大大减少了运算时间,下面我们就来尝试的将其拆为两个平方数之和,如果拆成功了那么就会返回1或2,因为其中一个平方数可能为0.
代码:
public class Solution{
public int numSquares(int n){
//首先将数字化简
while(n%4==0){
n=n/4;
}
if(n%8==7)
return 4;
for(int a=0;a*a<=n;a++){
int b=(int)Math.sqrt(n-a*a);
if(a*a+b*b==n){
if(a==0||b==0)
return 1;
else
return 2;
}
}
return 3;
}
}
63.Perfect Squares(完美平方数)的更多相关文章
- 279 Perfect Squares 完美平方数
给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...) 使得他们的和等于 n.你需要让平方数的个数最少.比如 n = 12,返回 3 ,因为 12 = 4 + 4 + 4 : ...
- 搜索(BFS)---完美平方数
完美平方数 279. Perfect Squares (Medium) For example, given n = 12, return 3 because 12 = 4 + 4 + 4; give ...
- Project Euler 98:Anagramic squares 重排平方数
Anagramic squares By replacing each of the letters in the word CARE with 1, 2, 9, and 6 respectively ...
- LeetCode(279)Perfect Squares
题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...
- 洛谷P1206 [USACO1.2]回文平方数 Palindromic Squares
P1206 [USACO1.2]回文平方数 Palindromic Squares 271通过 501提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 暂时没有 ...
- Palindromic Squares 回文平方数
1.2.4 Palindromic Squares 回文平方数 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 139 Solved: 66[Submit ...
- 洛谷 P1206 [USACO1.2]回文平方数 Palindromic Squares
P1206 [USACO1.2]回文平方数 Palindromic Squares 题目描述 回文数是指从左向右念和从右向左念都一样的数.如12321就是一个典型的回文数. 给定一个进制B(2< ...
- [LeetCode] Sum of Square Numbers 平方数之和
Given a non-negative integer c, your task is to decide whether there're two integers a and b such th ...
- [LeetCode] Perfect Number 完美数字
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
随机推荐
- ajax axios 下载文件时如何获取进度条 process
最近项目需要做一个下载文件的进度条,看网上上传文件进度条下载,特分享出来方便大家查阅 <!DOCTYPE html> <html> <head> <m ...
- vue-froala-wysiwyg 富文本编辑器
近期需要在vue3项目上做一个富文本编辑器,找了很多插件组件,最终决定用 froala.虽然不是免费的,但是功能实在是太强大了. froala 文档:https://www.froala.com/wy ...
- 初学Java 彩票
import java.util.Scanner; public class Lottery { public static void main(String[] agrs) { ); Scanner ...
- 简单说下cookie,LocalStorage与SessionStorage.md
最近在网上看到有人讨论这三个的一些概念与区别,发现自己也一直没有较好的总结,所以查阅了一些资料来阐述一下. 基本概念 cookie cookie英文意思是小甜饼,是原来的网景公司创造,目前是在客户端存 ...
- shell 函数传递参数的几种方式
1.最近总结了 shell 中 function 的传递变量的几种方式 1.传递单个变量 2.传递数组变量 #!/bin/bash #trying to pass an variable. ...
- spring的统一进行异常处理
public class ExceptionHandler extends SimpleMappingExceptionResolver { private static final Logger l ...
- Calendar日历
Calendar calendar=Calendar.getInstance(); //上一年的今天 calendar.add(Calendar.YEAR,-1); System.out.printl ...
- html5 图片墙
代码实例: <!DOCTYPE html> <html> <head> <style> body,html{ padding:0;margin:0;wi ...
- BZOJ3038 上帝造题的七分钟
Time Limit: 3 Sec Memory Limit: 128 MB Description XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. "第一分钟,X说, ...
- Python 石头 剪刀 布
di = {1: '石头', 2: '剪刀', 3: '布'} def win(x, y): if len({x[0], y[0]}) == 1: print('平局.') else: if {x[0 ...