我正在寻找多维字符数组中的字符。我创建了自己的字符类,其中包含字符和找到的标志。我试图在数组中搜索字符串中包含的所有字符。由于某种原因,如果字符串和数组包含多个相同的字符,我的算法只获取最后一个字符,然后找不到任何其他字符。它应该找到第一个,然后如果有另一个相同的字符,它应该忽略找到的那个,并在数组中查找另一个。有人可以请帮助。
class myChar : NSObject {
var value: Character?
var found: Bool?
var row: Int?
var column: Int?
}
class myCharForArray : NSObject {
var value: Character?
var found: Bool?
}
class ViewController: UIViewController {
var multiDimCharArray = [[myCharForArray]]()
override func viewDidLoad() {
super.viewDidLoad()
multiDimCharArray = createMainArray(nSize: 5)
searchLinearName(userName: "AANDREW", arrayForTesting: multiDimCharArray)
flattenArray(userName: "AANDREW", nestedArray: multiDimCharArray)
}
func createCharacterForArrayMainArray(randomNSize: Int) -> [myCharForArray]{
let alphabet: [Character] = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
var randomLetterArray = [myCharForArray]()
for i in 0..<randomNSize{
let rand = Int(arc4random_uniform(26))
let randomLetter = myCharForArray()
randomLetter.value = alphabet[rand]
randomLetter.found = false
randomLetterArray.append(randomLetter)
}
// print(randomLetterArray)
return randomLetterArray
}
func createMainArray(nSize: Int) -> [[myCharForArray]] {
var mainArray = [[myCharForArray]]()
for i in 0..<nSize{
mainArray.append(createCharacterForArrayMainArray(randomNSize: nSize))
}
if nSize <= 10{
print("RANDOM MATRIX: \n")
for i in mainArray{
var charArray = [Character]()
for j in i{
charArray.append(j.value!)
}
print(charArray)
}
print("\n")
}
return mainArray
}
func stringToMyCharArray(stringToChange: String) -> [myChar]{
let chars = Array(stringToChange)
var myCharArray = [myChar]()
for char in chars{
let thisChar = myChar()
thisChar.value = char
thisChar.found = false
thisChar.row = 0
thisChar.column = 0
myCharArray.append(thisChar)
}
return myCharArray
}
func searchLinearName(userName: String, arrayForTesting: [[myCharForArray]]){
let chars = stringToMyCharArray(stringToChange: userName)
print("Linear Search")
for char in chars{
var i = 0;
for arrayChar in arrayForTesting{
i = i + 1
var j = 0;
for singleCharInArray in arrayChar{
j = j + 1
if char.value == singleCharInArray.value && singleCharInArray.found == false{
char.found = true
char.row = i
char.column = j
singleCharInArray.found = true
// print("\(char.value!): [\(char.row!),\(char.column!)]")
}else if char.value == singleCharInArray.value && singleCharInArray.found == true{
print("This Char \(singleCharInArray.value!) has been found [\(i - 1),\(j - 1)]")
}
}
}
}
for char in chars{
print("\(char.value!): [\(char.row! - 1),\(char.column! - 1)]")
}
print("\n")
}
我的输出看起来像这样
RANDOM MATRIX:
["A", "A", "W", "M", "S"]
["C", "C", "Y", "S", "K"]
["B", "M", "N", "P", "R"]
["D", "D", "B", "N", "V"]
["I", "Q", "K", "X", "C"]
Linear Search
This Char A has been found [0,0]
This Char A has been found [0,1]
A: [0,1]
A: [-1,-1]
N: [3,3]
D: [3,1]
R: [2,4]
E: [-1,-1]
W: [0,2]
应该是这样的
A: [0,0]
A: [0,1]
N: [3,3]
D: [3,1]
R: [2,4]
E: [-1,-1]
W: [0,2]
答案 0 :(得分:0)
您必须在找到下一个角色后立即开始新的迭代,因此您需要两个break
。
func searchLinearName(userName: String, arrayForTesting: [[myCharForArray]]) {
let chars = stringToMyCharArray(stringToChange: userName)
print("Linear Search")
for char in chars {
var i = 0
for arrayChar in arrayForTesting {
i += 1
var j = 0
for singleCharInArray in arrayChar {
j += 1
if char.value == singleCharInArray.value && singleCharInArray.found == false {
char.found = true
char.row = i
char.column = j
singleCharInArray.found = true
break
// print("\(char.value!): [\(char.row!),\(char.column!)]")
} else if char.value == singleCharInArray.value && singleCharInArray.found == true {
print("This Char \(singleCharInArray.value!) has been found [\(i - 1),\(j - 1)]")
}
}
if char.found! {
break
}
}
}
for char in chars{
print("\(char.value!): [\(char.row! - 1),\(char.column! - 1)]")
}
print("\n")
}