feat: usage of __builtin_clz

This commit is contained in:
Lam Haoyin 2022-01-02 11:28:22 +08:00
parent 706548af13
commit 08e310e19d
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
1 changed files with 3 additions and 2 deletions

View File

@ -4,8 +4,9 @@ class Solution {
public:
// O(log(n))
static int lastRemaining(int n) {
int cnt = 0, ret = 1;
for (int i = n; i >>= 1; ++cnt);
// int cnt = 0, ret = 1;
int ret = 1, cnt = __builtin_clz(n) ^ 31;
// for (int i = n; i >>= 1; ++cnt);
for (int i = 0; i < cnt; ++i, n >>= 1)
ret += !(1 & i) || (1 & n) ? (1 << i) : 0;
return ret;