Skip to content

Commit 76279a4

Browse files
committed
check in source code
1 parent 5274639 commit 76279a4

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Leetcode231_PowerOfTwo
8+
{
9+
/// <summary>
10+
/// Leetcode 231: Power of two
11+
/// https://leetcode.com/problems/power-of-two/description/
12+
/// </summary>
13+
class Program
14+
{
15+
static void Main(string[] args)
16+
{
17+
}
18+
19+
/// <summary>
20+
/// 0 - false
21+
/// 1 - true
22+
/// 2 - true
23+
/// 3 - false
24+
/// 4 - true
25+
/// </summary>
26+
/// <param name="n"></param>
27+
/// <returns></returns>
28+
public bool IsPowerOfTwo(int n)
29+
{
30+
if( n <= 0 )
31+
return false;
32+
33+
if( n == 1 )
34+
return true;
35+
36+
if (n % 2 == 1)
37+
return false;
38+
39+
return IsPowerOfTwo(n / 2);
40+
}
41+
}
42+
}

Leetcode 231 Power of two.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Leetcode231_PowerOfTwo
8+
{
9+
/// <summary>
10+
/// Leetcode 231: Power of two
11+
/// https://leetcode.com/problems/power-of-two/description/
12+
/// </summary>
13+
class Program
14+
{
15+
static void Main(string[] args)
16+
{
17+
}
18+
19+
/// <summary>
20+
/// 0 - false
21+
/// 1 - true
22+
/// 2 - true
23+
/// 3 - false
24+
/// 4 - true
25+
/// </summary>
26+
/// <param name="n"></param>
27+
/// <returns></returns>
28+
public bool IsPowerOfTwo(int n)
29+
{
30+
if( n <= 0 )
31+
return false;
32+
33+
if( n == 1 )
34+
return true;
35+
36+
if (n % 2 == 1)
37+
return false;
38+
39+
return IsPowerOfTwo(n / 2);
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)