Skip to content

Commit 1e06940

Browse files
committed
Add C++ solution for leetcode 390.
1 parent 239ced3 commit 1e06940

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include<algorithm>
2+
#include<vector>
3+
#include<iostream>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
int lastRemaining(int n) {
9+
if (n == 1) return 1;
10+
/* 递归: f(n) = 2(n/2 + 1 - f(n/2)) */
11+
return n == 1 ? 1 : 2 * (n/2 + 1 - lastRemaining(n/2));
12+
}
13+
};
14+
15+
// Test
16+
int main()
17+
{
18+
Solution sol;
19+
int n = 4;
20+
auto res = sol.lastRemaining(n);
21+
cout << res << endl;
22+
23+
return 0;
24+
}

0 commit comments

Comments
 (0)