当前位置:网站首页>Leetcode skimming ---367
Leetcode skimming ---367
2022-07-03 10:35:00 【Long time no see 0327】
subject : Given a Positive integer num , Write a function , If num It's a complete square , Then return to true , Otherwise return to false .
Input :num = 16
Output :true
Method 1 : Built in library functions
class Solution {
public:
bool isPerfectSquare(int num) {
int x = (int) sqrt(num);
return x * x == num;
}
};Method 2 : violence
class Solution {
public:
bool isPerfectSquare(int num) {
long x = 1, square = 1;
while (square <= num) {
if (square == num) {
return true;
}
++x;
square = x * x;
}
return false;
}
};Complexity analysis
Time complexity :O(
)
Spatial complexity :O(1)
Method 3 : Two points search
class Solution {
public:
bool isPerfectSquare(int num) {
int left = 0, right = num;
while (left <= right) {
int mid = (right - left) / 2 + left;
long square = (long) mid * mid;
if (square < num) {
left = mid + 1;
} else if (square > num) {
right = mid - 1;
} else {
return true;
}
}
return false;
}
};Complexity analysis
Time complexity :O(logn)
Spatial complexity :O(1)
Method four : Newton's iteration
class Solution {
public:
bool isPerfectSquare(int num) {
double x0 = num;
while (true) {
double x1 = (x0 + num / x0) / 2;
if (x0 - x1 < 1e-6) {
break;
}
x0 = x1;
}
int x = (int) x0;
return x * x == num;
}
};Complexity analysis
Time complexity :O(logn)
Spatial complexity :O(1)
边栏推荐
- Several problems encountered in installing MySQL under MAC system
- Tensorflow—Image segmentation
- Secure in mysql8.0 under Windows_ file_ Priv is null solution
- Standard library header file
- 深度学习入门之线性回归(PyTorch)
- 20220603 Mathematics: pow (x, n)
- High imitation bosom friend manke comic app
- [LZY learning notes dive into deep learning] 3.4 3.6 3.7 softmax principle and Implementation
- Leetcode刷题---704
- 20220602 Mathematics: Excel table column serial number
猜你喜欢

MySQL报错“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法

Data classification: support vector machine

ThreadLocal原理及使用场景

丢弃法Dropout(Pytorch)

Leetcode - 706 design hash mapping (Design)*

Ut2016 learning notes

Raspberry pie 4B installs yolov5 to achieve real-time target detection

Leetcode - 705 design hash set (Design)

2018 Lenovo y7000 black apple external display scheme

Policy Gradient Methods of Deep Reinforcement Learning (Part Two)
随机推荐
Model evaluation and selection
Judging the connectivity of undirected graphs by the method of similar Union and set search
【SQL】一篇带你掌握SQL数据库的查询与修改相关操作
Leetcode - 705 design hash set (Design)
Numpy Foundation
Hands on deep learning pytorch version exercise solution - 2.5 automatic differentiation
The imitation of jd.com e-commerce project is coming
Leetcode刷题---10
Hands on deep learning pytorch version exercise solution - 3.1 linear regression
Julia1.0
Out of the box high color background system
[LZY learning notes dive into deep learning] 3.1-3.3 principle and implementation of linear regression
七、MySQL之数据定义语言(二)
R language classification
Multi-Task Feature Learning for Knowledge Graph Enhanced Recommendation
Leetcode刷题---202
[LZY learning notes -dive into deep learning] math preparation 2.5-2.7
Leetcode skimming ---217
20220601 Mathematics: zero after factorial
Neural Network Fundamentals (1)