当前位置:网站首页>The mystery of number idempotent and perfect square
The mystery of number idempotent and perfect square
2022-06-11 00:34:00 【InfoQ】
️Part1. Idempotent ️
️2 The power of ️
Topic details
Input :n = 1
Output :true
explain :20 = 1
Input :n = 16
Output :true
explain :24 = 16
Input :n = 3
Output :false
Their thinking
10111111101nnn-111n&(n-1)nn=01101Source code
// Specific writing
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;
}
}
}
// Abbreviated
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 The power of ️
Topic details
Input :n = 27
Output :true
Input :n = 45
Output :false
Their thinking
3311313133131Source code
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 The power of ️
Topic details
Input :n = 16
Output :true
Input :n = 5
Output :false
Their thinking
1Source code
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. Complete square ️
️ Complete square ️
Topic details
Input :num = 16
Output :true
Input :num = 14
Output :false
Their thinking
Source code
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;
}
}
summary
- There is only one binary sequence of powers of
1And greater than0.
- The power of is determined by the factor
3And1form , So keep dividing by3, The final number must be another factor1.
- One is greater than
0Number of numbers , Only one binary bit of this number is1And the digital analog3As the result of the1, Then the number isThe power of .
- The formula for calculating the complete square number :
边栏推荐
- Shengteng AI development experience based on target detection and identification of Huawei cloud ECS [Huawei cloud to jianzhiyuan]
- 对接请求方式
- 海贼oj#148.字符串反转
- unity 网格面片生成抛物线,折线
- JVM garbage collection mechanism and common garbage collectors
- Yii2 ActiveRecord 使用表关联出现的 id 自动去重问题
- Excel单元格
- 数的奥秘之幂数与完全平方数
- mysql 数据库 表 备份
- Pirate OJ 148 String inversion
猜你喜欢

The website is harmed by XSS hanging horse

【AcWing】4. Multiple knapsack problem I

Signature verification failed during system application installation

Njupt Nanyou Discrete Mathematics_ Experiment 3

unity 网格面片生成抛物线,折线

How to install mathtype6.9 and related problem solutions in office2016 (word2016)

Unity mesh patch generates parabola and polyline

【无标题】测试下啊

Unity自定义文件夹图标颜色 个性化Unity编译器

From the perspective of Confucius Temple IP crossover, we can see how the six walnuts become "butterflies" for the second time
随机推荐
微信小程序实现OCR扫描识别
哈工大软件构造复习——LSP原则,协变和逆变
[network planning] 2.2.4 Web cache / proxy server
Room first use
How to handle file cache and session?
Multipass Chinese documentation - Tutorial
第一章 总论-会计基础
海贼oj#148.字符串反转
canvas绘画折线段
From the perspective of Confucius Temple IP crossover, we can see how the six walnuts become "butterflies" for the second time
[untitled] test
Mysql database table backup
Wireshake introduction learning notes
Bluetooth development (9) -- A2DP protocol in combination with code
yum源更新
452. 用最少数量的箭引爆气球
array_ column() expects parameter 1 to be array, array given
Njupt Nanyou Discrete Mathematics_ Experiment 1
[JVM] memory model
Signature verification failed during system application installation