From 08e310e19daa6c973eb0db1ae5ea23a002ac0f47 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Sun, 2 Jan 2022 11:28:22 +0800 Subject: [PATCH] feat: usage of __builtin_clz --- 2201/220102-CN.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/2201/220102-CN.cpp b/2201/220102-CN.cpp index e4d3225..edb5c01 100644 --- a/2201/220102-CN.cpp +++ b/2201/220102-CN.cpp @@ -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;