feat: usage of __builtin_clz
This commit is contained in:
parent
706548af13
commit
08e310e19d
|
|
@ -4,8 +4,9 @@ class Solution {
|
||||||
public:
|
public:
|
||||||
// O(log(n))
|
// O(log(n))
|
||||||
static int lastRemaining(int n) {
|
static int lastRemaining(int n) {
|
||||||
int cnt = 0, ret = 1;
|
// int cnt = 0, ret = 1;
|
||||||
for (int i = n; i >>= 1; ++cnt);
|
int ret = 1, cnt = __builtin_clz(n) ^ 31;
|
||||||
|
// for (int i = n; i >>= 1; ++cnt);
|
||||||
for (int i = 0; i < cnt; ++i, n >>= 1)
|
for (int i = 0; i < cnt; ++i, n >>= 1)
|
||||||
ret += !(1 & i) || (1 & n) ? (1 << i) : 0;
|
ret += !(1 & i) || (1 & n) ? (1 << i) : 0;
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue