😂失败1:超出时间限制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { public: int kthGrammar(int N, int K) { vector<vector<int> > v(30, vector<int>()); helper(N , v); return v[N - 1][K - 1]; } void helper(int n, vector<vector<int>> & v){ if(n == 0) return ; if(n == 1) { v[0].push_back(0); return ; } helper(n - 1, v); for(auto x : v[n - 2]) { if(x) { v[n - 1].push_back(1); v[n - 1].push_back(0); }else { v[n - 1].push_back(0); v[n - 1].push_back(1); } } } };
|
Time Limit Exceeded
15/55
cases passed (N/A)
Testcase
30
434991989
😂递归实现
重点在于找规律
1 2 3 4 5 6 7 8 9 10
| class Solution { public: int kthGrammar(int N, int K) { if(N == 1) { return 0; } if(K % 2) return kthGrammar(N - 1, (K + 1) / 2); else return !kthGrammar(N - 1, K / 2); } };
|
Accepted
55/55 cases passed (0 ms)
Your runtime beats 100 % of cpp submissions
Your memory usage beats 5.25 % of cpp submissions (8.5 MB)