This is one of those problems where the clean idea matters more than the amount of code. For 367. Valid Perfect Square, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are isPerfectSquare.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Valid Perfect Square.
02//Memory Usage: 8 MB, less than 83.44% of C++ online submissions for Valid Perfect Square.
03
04class Solution {
05public:
06 bool isPerfectSquare(int num) {
07 long long lnum = num;
08 for(long long i = 1; i * i <= lnum; i++){
09 if(i * i == lnum) return true;
10 }
11 return false;
12 }
13};
14
15//https://leetcode.com/problems/valid-perfect-square/discuss/83874/A-square-number-is-1%2B3%2B5%2B7%2B...-JAVA-code
16/**
17Approach 1 :A square number is 1+3+5+7+...
18O(sqrt(n))
19**/
20
21//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Valid Perfect Square.
22//Memory Usage: 8 MB, less than 64.42% of C++ online submissions for Valid Perfect Square.
23
24class Solution {
25public:
26 bool isPerfectSquare(int num) {
27 int i = 1;
28 while(num > 0){
29 num -= i;
30 i += 2;
31 }
32 return num == 0;
33 }
34};
35
36/**
37Approach 2 : binary search
38O(logn)
39**/
40
41//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Valid Perfect Square.
42//Memory Usage: 8.1 MB, less than 55.21% of C++ online submissions for Valid Perfect Square.
43
44class Solution {
45public:
46 bool isPerfectSquare(int num) {
47 long long low = 1, high = num;
48 while(low <= high){
49 long long mid = (low+high) >> 1;
50 if(mid * mid == num){
51 return true;
52 }else if(mid * mid < num){
53 low = mid + 1;
54 }else{
55 high = mid - 1;
56 }
57 }
58 return false;
59 }
60};
61
62/**
63Approach 3 : Newtown method
64**/
65
66/Runtime: 4 ms, faster than 100.00% of C++ online submissions for Valid Perfect Square.
67//Memory Usage: 8.1 MB, less than 50.92% of C++ online submissions for Valid Perfect Square.
68
69class Solution {
70public:
71 bool isPerfectSquare(int num) {
72 long long x = num;
73 while(x * x > num){
74 x = (x + num/x) >> 1;
75 }
76 return x*x == num;
77 }
78};
Cost