Mein Problem ist verbunden mit einem effizienten Weg, Elemente mit false
im Array von Arrays zu zählen. Ich jetzt, wie jedes Array iterieren und zählen, wenn Element ist true
oder false
ich suche effizienter Weg.Wie man `falsche` Elemente des Arrays von Arrays zählt
Beispiel des Problems:
let n = 4
var board = Array(repeating:Array(repeating:true, count:n), count:n)
let qPos = [4,4]
board[qPos[0] - 1][qPos[1] - 1] = false
for x in 0..<n{
for y in 0..<n {
if (x == qPos[0] - 1) {
board[x][y] = false
}
if (y == qPos[1] - 1) {
board[x][y] = false
}
if (y == x) {
board[x][y] = false
}
}
}
//How to calculate all negative elements of the board ? In this code output should be 10
Mein Beispiel Zahl:
var count = 0
for x in 0..<board.count {
for y in 0..<board.count {
if board[x][y] == false {
count += 1
}
}
}
Aktualisieren Sie Ihre Frage mit Ihrem Versuch, die falschen Werte zu zählen. – rmaddy
@rmaddy ich füge meinen Versuch hinzu –
'board.forEach {$ 0.forEach {count + =! $ 0? 1: 0}} ' –