Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 65589bb

Browse files
committed
Add C++ solution for leetcode 367.
1 parent 0e5a51f commit 65589bb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<vector>
2+
#include<algorithm>
3+
#include<iostream>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
bool isPerfectSquare(int num) {
9+
int i = 1; // i: 表示奇数, 1是用于下面循环的奇数初始值
10+
while (num > 0)
11+
{
12+
num -= i;
13+
i += 2;
14+
}
15+
if (num == 0) return true;
16+
return false;
17+
}
18+
};
19+
20+
// Test
21+
int main()
22+
{
23+
Solution sol;
24+
int num = 14;
25+
auto res = sol.isPerfectSquare(num);
26+
cout << (res ? "True" : "False") << endl;
27+
28+
return 0;
29+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /