Skip to main content
deleted 25 characters in body
Source Link
mdfst13
  • 21.7k
  • 6
  • 33
  • 68
public static int solution(int N) {
    int binaryGap = 0;

    // Special case
    if (N == 0) {
        return 0;
    }

    // remove trailing zeroes if not counted; credit to Peter Taylor
    while (N % 2 == 0) {
        N /= 2;
    }

    for (int j = 0; N > 0; N /= 2) {
        if (N % 2 == 0) {
            j++;
        } else {
            if (j > binaryGap) {
                binaryGap = j;
            }

            j = 0;
        }
    }

    return binaryGap;
}
public static int solution(int N) {
    int binaryGap = 0;

    // Special case
    if (N == 0) {
        return 0;
    }

    // remove trailing zeroes if not counted; credit to Peter Taylor
    while (N % 2 == 0) {
        N /= 2;
    }

    for (int j = 0; N > 0; N /= 2) {
        if (N % 2 == 0) {
            j++;
        } else {
            if (j > binaryGap) {
                binaryGap = j;
            }

            j = 0;
        }
    }

    return binaryGap;
}
public static int solution(int N) {
    int binaryGap = 0;

    if (N == 0) {
        return 0;
    }

    // remove trailing zeroes if not counted; credit to Peter Taylor
    while (N % 2 == 0) {
        N /= 2;
    }

    for (int j = 0; N > 0; N /= 2) {
        if (N % 2 == 0) {
            j++;
        } else {
            if (j > binaryGap) {
                binaryGap = j;
            }

            j = 0;
        }
    }

    return binaryGap;
}
public static int solution(int N) {
    int binaryGap = 0;

    // Special case
    if (N == 0) {
        return 0;
    }

    // remove trailing zeroes if not counted; credit to Peter Taylor
    while (N % 2 == 0) {
        N /= 2;
    }

    for (int j = 0; N > 0; N /= 2) {
        if (N % 2 == 0) {
            j++;
        } else {
            if (j > binaryGap) {
                binaryGap = j;
            }

            j = 0;
        }
    }

    return binaryGap;
}
public static int solution(int N) {
    int binaryGap = 0;

    // remove trailing zeroes if not counted; credit to Peter Taylor
    while (N % 2 == 0) {
        N /= 2;
    }

    for (int j = 0; N > 0; N /= 2) {
        if (N % 2 == 0) {
            j++;
        } else {
            if (j > binaryGap) {
                binaryGap = j;
            }

            j = 0;
        }
    }

    return binaryGap;
}
public static int solution(int N) {
    int binaryGap = 0;

    // Special case
    if (N == 0) {
        return 0;
    }

    // remove trailing zeroes if not counted; credit to Peter Taylor
    while (N % 2 == 0) {
        N /= 2;
    }

    for (int j = 0; N > 0; N /= 2) {
        if (N % 2 == 0) {
            j++;
        } else {
            if (j > binaryGap) {
                binaryGap = j;
            }

            j = 0;
        }
    }

    return binaryGap;
}
added 287 characters in body
Source Link
mdfst13
  • 21.7k
  • 6
  • 33
  • 68

You don't need to use equals with a character, as it reduces to a primitive type (char). You can do so here, but you don't have to do so.

I'd move the println outside the method. Then you can choose to output or not.

public static int solution(int N) {
    int binaryGap = 0;

    // remove trailing zeroes if not counted; credit to Peter Taylor
    while (N % 2 == 0) {
        N /= 2;
    }

    for (int j = 0; N > 0; N /= 2) {
        if (N % 2 == 0) {
            j++;
        } else {
            if (j > binaryGap) {
                binaryGap = j;
            }

            j = 0;
        }
    }

    return binaryGap;
}

I'd move the println outside the method. Then you can choose to output or not.

public static int solution(int N) {
    int binaryGap = 0;

    for (int j = 0; N > 0; N /= 2) {
        if (N % 2 == 0) {
            j++;
        } else {
            if (j > binaryGap){
                binaryGap = j;
            }

            j = 0;
        }
    }

    return binaryGap;
}

You don't need to use equals with a character, as it reduces to a primitive type (char). You can do so here, but you don't have to do so.

I'd move the println outside the method. Then you can choose to output or not.

public static int solution(int N) {
    int binaryGap = 0;

    // remove trailing zeroes if not counted; credit to Peter Taylor
    while (N % 2 == 0) {
        N /= 2;
    }

    for (int j = 0; N > 0; N /= 2) {
        if (N % 2 == 0) {
            j++;
        } else {
            if (j > binaryGap) {
                binaryGap = j;
            }

            j = 0;
        }
    }

    return binaryGap;
}
Source Link
mdfst13
  • 21.7k
  • 6
  • 33
  • 68
Loading