![JAR search and dependency download from the Maven repository](/logo.png)
g0801_0900.s0868_binary_gap.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0801_0900.s0868_binary_gap
// #Easy #Bit_Manipulation #2023_04_05_Time_142_ms_(100.00%)_Space_33.8_MB_(50.00%)
@Suppress("NAME_SHADOWING")
class Solution {
fun binaryGap(n: Int): Int {
var n = n
var max = 0
var pos = 0
var lastPos = -1
while (n != 0) {
pos++
if (n and 1 == 1) {
if (lastPos != -1) {
max = max.coerceAtLeast(pos - lastPos)
}
lastPos = pos
}
n = n shr 1
}
return max
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy