当前位置:网站首页>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)
边栏推荐
- ThreadLocal原理及使用场景
- Leetcode刷题---217
- High imitation Netease cloud music
- Ind yff first week
- Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported
- Training effects of different data sets (yolov5)
- Policy gradient Method of Deep Reinforcement learning (Part One)
- Drop out (pytoch)
- Leetcode - the k-th element in 703 data flow (design priority queue)
- Content type ‘application/x-www-form-urlencoded; charset=UTF-8‘ not supported
猜你喜欢

深度学习入门之线性代数(PyTorch)

Install yolov3 (Anaconda)

重写波士顿房价预测任务(使用飞桨paddlepaddle)

A super cool background permission management system

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

8、 Transaction control language of MySQL

mysql5.7安装和配置教程(图文超详细版)

Ut2015 learning notes

What did I read in order to understand the to do list

Ut2016 learning notes
随机推荐
20220606 Mathematics: fraction to decimal
Leetcode skimming ---10
Hands on deep learning pytorch version exercise solution - 2.3 linear algebra
Linear regression of introduction to deep learning (pytorch)
Yolov5 creates and trains its own data set to realize mask wearing detection
Softmax 回归(PyTorch)
[LZY learning notes -dive into deep learning] math preparation 2.5-2.7
20220604 Mathematics: square root of X
Matrix calculation of Neural Network Introduction (pytoch)
Powshell's set location: unable to find a solution to the problem of accepting actual parameters
Implementation of "quick start electronic" window dragging
EFFICIENT PROBABILISTIC LOGIC REASONING WITH GRAPH NEURAL NETWORKS
20220601 Mathematics: zero after factorial
Leetcode skimming ---44
Model selection for neural network introduction (pytorch)
A complete answer sheet recognition system
The imitation of jd.com e-commerce project is coming
Data classification: support vector machine
Leetcode-106: construct a binary tree according to the sequence of middle and later traversal
20220531 Mathematics: Happy numbers