当前位置:网站首页>数的奥秘之幂数与完全平方数
数的奥秘之幂数与完全平方数
2022-06-10 23:10:00 【InfoQ】
️Part1.幂数️
️2 的幂️
题目详情
输入:n = 1
输出:true
解释:20 = 1
输入:n = 16
输出:true
解释:24 = 16
输入:n = 3
输出:false
解题思路
10111111101nnn-111n&(n-1)nn=01101源代码
//具体写
class Solution {
public boolean isPowerOfTwo(int n) {
int count = 0;
if (n < 0){
return false;
}
while (n != 0){
n &= n - 1;
count++;
}
if(count == 1){
return true;
}
else{
return false;
}
}
}
//简略写
class Solution {
public boolean isPowerOfTwo(int n) {
return n > 0 && ((n & (n - 1)) == 0);
}
}
bool isPowerOfTwo(int n){
return n > 0 && ((n & (n - 1)) == 0);
}
class Solution {
public:
bool isPowerOfTwo(int n) {
return n > 0 && ((n & (n - 1)) == 0);
}
};
️3 的幂️
题目详情
输入:n = 27
输出:true
输入:n = 45
输出:false
解题思路
3311313133131源代码
class Solution {
public boolean isPowerOfThree(int n) {
if (n <= 0) {
return false;
}
int m = n;
while (m % 3 == 0) {
m /= 3;
}
if (m == 1) {
return true;
}
else {
return false;
}
}
}
bool isPowerOfThree(int n){
if (n <= 0)
{
return 0;
}
int m = n;
while(m % 3 == 0)
{
m /= 3;
}
if (m == 1)
{
return true;
}
return false;
}
class Solution {
public:
bool isPowerOfThree(int n) {
if (n <= 0)
{
return 0;
}
int m = n;
while(m % 3 == 0)
{
m /= 3;
}
if (m == 1)
{
return true;
}
return false;
}
};
️4 的幂️
题目详情
输入:n = 16
输出:true
输入:n = 5
输出:false
解题思路
1源代码
class Solution {
public boolean isPowerOfFour(int n) {
return n > 0 && ((n &(n-1)) == 0) && n % 3 == 1;
}
}
bool isPowerOfFour(int n){
return n > 0 && ((n &(n-1)) == 0) && n % 3 == 1;
}
class Solution {
public:
bool isPowerOfFour(int n) {
return n > 0 && ((n &(n-1)) == 0) && n % 3 == 1;
}
};
️Part2.完全平方数️
️完全平方数️
题目详情
输入:num = 16
输出:true
输入:num = 14
输出:false
解题思路
源代码
class Solution {
public boolean isPerfectSquare(int num) {
for (int i = 1; (long)(i * i) <= (long)num && i <= num / 2 + 1; i++) {
if (i * i == num) {
return true;
}
}
return false;
}
}
bool isPerfectSquare(int num){
int i = 1;
long sum = 0;
while (sum <= num) {
if (sum == num) {
return true;
}
sum += i;
i += 2;
}
return false;
}
class Solution {
public boolean isPerfectSquare(int num) {
int left = 1;
int right = num;
while (left <= right) {
int mid = left + (right - left) / 2;
int div = num / mid;
if (mid == div) {
if (mid * div == num) {
return true;
}
left = mid + 1;
}else if (mid > div) {
right = mid - 1;
}else {
left = mid + 1;
}
}
return false;
}
}
总结
- 的幂的二进制序列中只有一个
1并且大于0。
- 的幂由因子
3与1组成,所以不断除以3,最后得到的数一定是另一个因子1。
- 一个大于
0的数,该数的二进制只有一位是1且该数模3的结果为1,则该数为的幂。
- 计算完全平方数公式:
边栏推荐
- [MVC&Core]ASP.NET Core MVC 视图传值入门
- From the perspective of Confucius Temple IP crossover, we can see how the six walnuts become "butterflies" for the second time
- Exemple VTK - - trois plans qui se croisent
- 452. detonate the balloon with the minimum number of arrows
- 【数据库】Nosql数据库的种类
- [JVM] memory model
- Leetcode-15 sum of three numbers
- VTK example -- three intersecting planes
- 文件缓存和session怎么处理?
- Database table structure
猜你喜欢

How to check the variable waveform when debugging the program? Look here

【数据库】Mysql索引面试题

Leetcode-713 subarray with product less than k

【无标题】6666666

VTK example -- three intersecting planes

Go语言Channel理解使用

Installation of phpstudy

Njuptn Nanyou Discrete Mathematics_ Experiment 4
![[turtle confessions collection]](/img/81/b4bacc23691e58e403f1330d0ca7cf.jpg)
[turtle confessions collection] "the moon at the bottom of the sea is the moon in the sky, and the person in front of us is the sweetheart." Be happy for the rest of your life, and be safe for ever ~

Leetcode-209 minimum length subarray
随机推荐
测试下吧先
[JVM] class loading mechanism
[pyGame games] interesting puzzle game: how many hamsters can you play? (source code attached)
[JVM] thread
上海炒股开户是安全的吗?
【JVM】内存模型
第一章 总论-会计基础
市值215亿,这个四川80后会让电视机成为历史?
LeetCode 1996. Number of weak characters in the game*
[pyGame] when the "coolest snake in history" arrives, fun will play it (no money for fun)
[appearance detection artifact] come on, please show me your unique skill (is this appearance worthy of the audience?)
富文本活动测试1
Website online customer service system Gofly source code development log - 5 Gin framework integration daemon
Multipass Chinese documentation - Tutorial
Canvas drawing line break
MD5Util
Blog recommendation | building IOT applications -- Introduction to flip technology stack
array_ column() expects parameter 1 to be array, array given
Excel单元格
Qt客户端套接字QTcpSocket通过bind指定本地ip