All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0801_0900.s0868_binary_gap.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g0801_0900.s0868_binary_gap;

// #Easy #Math #Bit_Manipulation

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public int binaryGap(int n) {
        String bin = Integer.toBinaryString(n);
        List oneIndexes = new ArrayList<>();
        for (int i = 0; i < bin.length(); i++) {
            if (bin.charAt(i) == '1') {
                oneIndexes.add(i);
            }
        }
        int maxGap = 0;
        for (int i = 0; i < oneIndexes.size() - 1; i++) {
            maxGap = Math.max(oneIndexes.get(i + 1) - oneIndexes.get(i), maxGap);
        }
        return maxGap;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy