当前位置:网站首页>Day367: valid complete square
Day367: valid complete square
2022-06-23 01:22:00 【Shallow look】
List of articles
problem : Effective complete square number
Given a positive integer num , Write a function , If num It's a complete square , Then return to true , Otherwise return to false .
Advanced : Don't Use any built-in library functions , Such as sqrt .
source : Power button (LeetCode)
Train of thought : Brute force ergodic solution
For less than num Integer of from 1 To traverse the , Determine whether the square is equal to num, If equal to, return true, Otherwise return to false.
class Solution {
public boolean isPerfectSquare(int num) {
boolean result = false;
if (num==1) result = true;
for(int i=2; i < num/2+1; i++){
if(i*i==num){
result = true;
break;
}
}
return result;
}
}
shortcoming : When the integer is too large , Time limit exceeded .
Train of thought two : Dichotomy
- The root of a perfect square number x, Must be satisfied with 1 ≤ x ≤ n u m 1 \le x \le num 1≤x≤num, We use this interval as the search interval .
- left Start from the left side of the section ,right Start from the right side of the section , Calculate the median m i d = ( l e f t + r i g h t ) 2 mid=\frac{(left+right)}{2} mid=2(left+right)
- Perfect square heel x Must satisfy one of the three relationships , m i d = = x , m i d < x , m i d > x mid ==x ,mid <x, mid>x mid==x,mid<x,mid>x, So as left or right Update conditions for .
- The dichotomy is compared to the violent solution , The time complexity is reduced to some extent .
class Solution {
public boolean isPerfectSquare(int num) {
int left = 0;
int right = num;
boolean result = false;
while (left <= right){
int mid = (left + right)/2;
long mid_square = (long)mid*mid;
if(mid_square < num){
left = mid + 1;
}else if(mid_square > num){
right = mid - 1;
}
else{
result = true;
return result;
}
}
return result;
}
}
Train of thought three : Newton method
Newton's method is an approximate method to solve the zero point of a function , The iterative idea is as follows .
For the function f ( x ) f(x) f(x), Take any starting point ( x 0 , f ( x 0 ) ) (x0,f(x0)) (x0,f(x0)), Do the function at this point f ( x ) f(x) f(x) The tangent of , Tangent and x Axis intersection point x 1 x1 x1 Is the abscissa of the point to be updated next time , comparison x 0 x0 x0 for , x 1 x1 x1 Closer to zero , After many iterations , It can be approximated as point x 1 x1 x1 Approaching the zero of a function .
Ideas :
In this case f ( x ) = x 2 − n u m f(x)=x^2-num f(x)=x2−num
Update formula x i + 1 = ( x i + n u m / x i ) / 2 x_{i+1}=(x_i+num/{x_i})/2 xi+1=(xi+num/xi)/2
The end condition : We think that when the results of two adjacent iterations are similar , It can be considered close enough to zero .
Code :
class Solution {
public boolean isPerfectSquare(int num) {
double x0 = num;
double x1 = 0;
while (true){
x1 = (x0 + num/x0)/2;
if (x0 - x1 < 1e-1)
break;
x0 = x1;
}
x1 = (int)x1;
return x1*x1 == num;
}
}
Reference resources : Try to answer the official questions
边栏推荐
- Is it safe to invest in funds through daily funds? I intend to open an account to buy funds
- The longest child sequence of the 2019 Blue Bridge Cup
- You can also do NLP (classification)
- 層次選擇器
- 3D打印微组织
- 數據庫中數據的儲存結構和方式是什麼?
- Dig three feet to solve the data consistency problem between redis and MySQL
- Software construction course ADT and OOP understanding
- Development status of full color LED display
- LeetCode 206. Reverse linked list (iteration + recursion)
猜你喜欢

SQL programming task02 job - basic query and sorting

B tree and b+ tree
![[sliding window] leetcode992 Subarrays with K Different Integers](/img/69/1ac0c54d33af0f7a9e3db3e82d076b.png)
[sliding window] leetcode992 Subarrays with K Different Integers

SFOD:无源域适配升级优化,让检测模型更容易适应新数据

Ros2 summer school 2022 transfer-

Tidb monitoring upgrade: a long way to solve panic

E-R diagram

Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization

SAP ui5 application development tutorial 103 - how to consume third-party libraries in SAP ui5 applications

OSPF comprehensive experiment
随机推荐
A blog allows you to understand the use of material design
What is the storage structure and mode of data in the database?
Xiaobai operates win10 to expand Disk C (allocate disk D memory to Disk C) and the test is valid for many times
Webdriver and selenium Usage Summary
【滑动窗口】leetcode992. Subarrays with K Different Integers
[sliding window] leetcode992 Subarrays with K Different Integers
Graphite statsd interface data format description
How about China International Futures Co., Ltd.? Is it a regular futures company? Is it safe to open an account online?
人民币的单位的大写
Installation record of ros1noetic in Win 11
SAP ui5 application development tutorial 103 - how to consume the trial version of the third-party library in SAP ui5 applications
Add expiration time for localstorage
Local deployment and problem solving of IIS in ArcGIS JS 4.23
LINQ 查詢
Overview of visual object detection technology based on deep learning
B tree and b+ tree
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
工程目录导航
OSPF comprehensive experiment
Analysis on the wallet system architecture of Baidu trading platform