A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.
Write a function:
class Solution { public int solution(int N); }
that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.
For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..2,147,483,647].
solution.swift:10:13: error: use of undeclared type 'int' var a : int = 10 ^~~
solution.swift:10:9: warning: initialization of variable 'a' was never used; consider replacing with assignment to '_' or removing it var a = 10 ~~~~^ _ solution.swift:13:1: error: missing return in a function expected to return 'Int' } ^
solution.swift:10:9: warning: variable 'a' was never mutated; consider changing to 'let' constant var a = 10 ~~~ ^ let solution.swift:15:1: error: missing return in a function expected to return 'Int' } ^
solution.swift:15:1: error: missing return in a function expected to return 'Int' } ^
10
10
10
solution.swift:20:1: error: expressions are not allowed at the top level sloution(20) ^ solution.swift:20:1: error: use of unresolved identifier 'sloution'; did you mean 'solution'? sloution(20) ^~~~~~~~ solution solution.swift:7:13: note: 'solution' declared here public func solution(_ N : Int) -> Int { ^
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = STring(N,radix:2)
print(a)
return a
}
sloution(20)
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = STring(N,radix:2)
print(str)
return a
}
sloution(20)
solution.swift:21:1: error: expressions are not allowed at the top level sloution(20) ^ solution.swift:21:1: error: use of unresolved identifier 'sloution'; did you mean 'solution'? sloution(20) ^~~~~~~~ solution solution.swift:7:13: note: 'solution' declared here public func solution(_ N : Int) -> Int { ^ solution.swift:10:15: error: use of unresolved identifier 'STring'; did you mean 'String'? let str = STring(N,radix:2) ^~~~~~ String Swift.String:1:15: note: 'String' declared here public struct String { ^ solution.swift:16:12: error: use of unresolved identifier 'a'; did you mean 'N'? return a ^ N solution.swift:7:24: note: 'N' declared here public func solution(_ N : Int) -> Int { ^
solution.swift:10:15: error: use of unresolved identifier 'STring'; did you mean 'String'? let str = STring(N,radix:2) ^~~~~~ String Swift.String:1:15: note: 'String' declared here public struct String { ^ solution.swift:16:12: error: use of unresolved identifier 'a'; did you mean 'N'? return a ^ N solution.swift:7:24: note: 'N' declared here public func solution(_ N : Int) -> Int { ^
solution.swift:10:15: error: use of unresolved identifier 'STring'; did you mean 'String'? let str = STring(N,radix:2) ^~~~~~ String Swift.String:1:15: note: 'String' declared here public struct String { ^
solution.swift:16:12: error: cannot convert return expression of type 'String' to return type 'Int' return str ^~~
10000010001
1111
100000
10000010001
1111
100000
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var fullNameArr = split(fullName) {$0 == " "}
print(str)
return N
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var fullNameArr = split(fullName) {$0 == " "}
print(str)
return N
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var fullNameArr = split(fullName) {$0 == "1"}
fullNameArr
print(str)
return N
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var fullNameArr = split(fullName) {$0 == "1"}
var firstName: String = fullNameArr[0]
var lastName: String? = fullNameArr.count > 1 ? fullNameArr[1] : nil
print(str)
return N
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var fullNameArr = split(fullName) {$0 == "1"}
var firstName: String = fullNameArr[0]
var lastName: String? = fullNameArr.count > 1 ? fullNameArr[1] : nil
print(firstName)
print(lastName)
return N
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var fullNameArr = split(fullName) {$0 == "1"}
var firstName: String = fullNameArr[0]
var lastName: String? = fullNameArr.count > 1 ? fullNameArr[1] : nil
print(firstName)
print(lastName)
return N
}
solution.swift:12:23: error: use of unresolved identifier 'split' var fullNameArr = split(fullName) {$0 == "1"} ^~~~~ SwiftGlibc.isprint:1:13: note: did you mean 'isprint'? public func isprint(_: Int32) -> Int32 ^ SwiftGlibc.sprintf:2:13: note: did you mean 'sprintf'? public func sprintf(_ __s: UnsafeMutablePointer<Int8>!, _ __format: UnsafePointer<Int8>!, _ varargs: Any...) -> Int32 ^ solution.swift:12:29: error: use of unresolved identifier 'fullName' var fullNameArr = split(fullName) {$0 == "1"} ^~~~~~~~ solution.swift:17:11: warning: expression implicitly coerced from 'String?' to 'Any' print(lastName) ^~~~~~~~ solution.swift:17:11: note: provide a default value to avoid this warning print(lastName) ^~~~~~~~ ?? <#default value#> solution.swift:17:11: note: force-unwrap the value to avoid this warning print(lastName) ^~~~~~~~ ! solution.swift:17:11: note: explicitly cast to 'Any' with 'as Any' to silence this warning print(lastName) ^~~~~~~~ as Any
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
let fullName = "First Last"
let fullNameArr = fullName.components(separatedBy: " ")
let name = fullNameArr[0]
let surname = fullNameArr[1]
print(firstName)
print(lastName)
return N
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
let fullNameArr = str.components(separatedBy: " ")
let name = fullNameArr[0]
let surname = fullNameArr[1]
print(firstName)
print(lastName)
return N
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
let fullNameArr = str.components(separatedBy: "1")
let name = fullNameArr[0]
let surname = fullNameArr[1]
print(firstName)
print(lastName)
return N
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
let fullNameArr = str.components(separatedBy: "1")
let name = fullNameArr[0]
let surname = fullNameArr[1]
print(name)
print(surname)
return N
}
00000
00000
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
let fullNameArr = str.components(separatedBy: "1")
let name = fullNameArr[0]
let surname = fullNameArr[1]
print(name)
print(surname)
return N
}
00000
00000
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var temp = ""
var isNumberOne = false
var zeroNumber : [Int] // 선언
zeroNumber = [Int]() // 초기화
for char in str {
if char == "1"{
isNumberOne = true;
}else{
isNumberOne = false;
temp.append(char)
}
if isNumberOne == true{
zeroNumber.append(temp.count)
temp = ""
}
}
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var temp = ""
var isNumberOne = false
var zeroNumber : [Int] // 선언
zeroNumber = [Int]() // 초기화
for char in str {
if char == "1"{
isNumberOne = true;
}else{
isNumberOne = false;
temp.append(char)
}
if isNumberOne == true{
zeroNumber.append(temp.count)
temp = ""
}
}
let bounds = minMax(array: zeroNumber)
print(bounds.max)
func minMax(array: [Int]) -> (min: Int, max: Int) {
var currentMin = array[0]
var currentMax = array[0]
for value in array[1..<array.count] {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var temp = ""
var isNumberOne = false
var zeroNumber : [Int] // 선언
zeroNumber = [Int]() // 초기화
for char in str {
if char == "1"{
isNumberOne = true;
}else{
isNumberOne = false;
temp.append(char)
}
if isNumberOne == true{
zeroNumber.append(temp.count)
temp = ""
}
}
let bounds = minMax(array: zeroNumber)
return bounds.max
func minMax(array: [Int]) -> (min: Int, max: Int) {
var currentMin = array[0]
var currentMax = array[0]
for value in array[1..<array.count] {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
}
solution.swift:37:14: error: use of local variable 'minMax' before its declaration let bounds = minMax(array: zeroNumber) ^ solution.swift:40:6: note: 'minMax' declared here func minMax(array: [Int]) -> (min: Int, max: Int) { ^
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var temp = ""
var isNumberOne = false
var zeroNumber : [Int] // 선언
zeroNumber = [Int]() // 초기화
for char in str {
if char == "1"{
isNumberOne = true;
}else{
isNumberOne = false;
temp.append(char)
}
if isNumberOne == true{
zeroNumber.append(temp.count)
temp = ""
}
}
let bounds = minMax(array: zeroNumber)
return bounds.max
}
func minMax(array: [Int]) -> (min: Int, max: Int) {
var currentMin = array[0]
var currentMax = array[0]
for value in array[1..<array.count] {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var temp = ""
var isNumberOne = false
var zeroNumber : [Int] // 선언
zeroNumber = [Int]() // 초기화
for char in str {
if char == "1"{
isNumberOne = true;
}else{
isNumberOne = false;
temp.append(char)
}
if isNumberOne == true{
zeroNumber.append(temp.count)
temp = ""
}
}
let bounds = minMax(array: zeroNumber)
return bounds.max
}
func minMax(array: [Int]) -> (min: Int, max: Int) {
var currentMin = array[0]
var currentMax = array[0]
for value in array[1..<array.count] {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var temp = ""
var isNumberOne = false
var zeroNumber : [Int] // 선언
zeroNumber = [Int]() // 초기화
for char in str {
if char == "1"{
isNumberOne = true;
}else{
isNumberOne = false;
temp.append(char)
}
if isNumberOne == true{
zeroNumber.append(temp.count)
temp = ""
}
}
let bounds = minMax(array: zeroNumber)
return bounds.max
}
func minMax(array: [Int]) -> (min: Int, max: Int) {
var currentMin = array[0]
var currentMax = array[0]
for value in array[1..<array.count] {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var temp = ""
var isNumberOne = false
var zeroNumber : [Int] // 선언
zeroNumber = [Int]() // 초기화
for char in str {
if char == "1"{
isNumberOne = true;
}else{
isNumberOne = false;
temp.append(char)
}
if isNumberOne == true{
zeroNumber.append(temp.count)
temp = ""
}
}
let bounds = minMax(array: zeroNumber)
return bounds.max
}
func minMax(array: [Int]) -> (min: Int, max: Int) {
var currentMin = array[0]
var currentMax = array[0]
for value in array[1..<array.count] {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var temp = ""
var isNumberOne = false
var zeroNumber : [Int] // 선언
zeroNumber = [Int]() // 초기화
for char in str {
if char == "1"{
isNumberOne = true;
}else{
isNumberOne = false;
temp.append(char)
}
if isNumberOne == true{
zeroNumber.append(temp.count)
temp = ""
}
}
let bounds = minMax(array: zeroNumber)
return bounds.max
}
func minMax(array: [Int]) -> (min: Int, max: Int) {
var currentMin = array[0]
var currentMax = array[0]
for value in array[1..<array.count] {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
import Foundation
import Glibc
// you can write to stdout for debugging purposes, e.g.
// print("this is a debug message")
public func solution(_ N : Int) -> Int {
// write your code in Swift 4.2.1 (Linux)
let str = String(N,radix:2)
var temp = ""
var isNumberOne = false
var zeroNumber : [Int] // 선언
zeroNumber = [Int]() // 초기화
for char in str {
if char == "1"{
isNumberOne = true;
}else{
isNumberOne = false;
temp.append(char)
}
if isNumberOne == true{
zeroNumber.append(temp.count)
temp = ""
}
}
let bounds = minMax(array: zeroNumber)
return bounds.max
}
func minMax(array: [Int]) -> (min: Int, max: Int) {
var currentMin = array[0]
var currentMax = array[0]
for value in array[1..<array.count] {
if value < currentMin {
currentMin = value
} else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
The solution obtained perfect score.