[Swift]プログラマ-数値文字列とアルファベット

3780 ワード


質問リンク
これは簡単に解決できる問題ですが、「replacingOccences(of:with:)」そのものが分からないので、解答が複雑すぎますㅠㅠ
replacegOccuence(帯:)?
func置換が表示される(ターゲット:String、帯域置換:String)->String
targetには置換するオブジェクトが含まれ、置換する値が含まれます.
文字列が返す関数の置換
たとえば、
var test = "one4seveneight"
print(test.replacingOccurence(of:"one",with:"1")
に表示されるように作成します.
14 sevenightが出力されます.
したがって,上記の関数を用いて以下のように解くことができる.

プールコード


import Foundation


func solution(_ s:String) -> Int {
    
    let numArray = ["zero","one","two","three","four","five","six","seven","eight","nine"]
    var resultArray = s
    
    for i in 0..<numArray.count {
        resultArray = resultArray.replacingOccurrences(of: numArray[i], with: "\(i)")
        print(resultArray)
    }
    
    return Int(resultArray)!
    
}

+置換精度が使用されていないプール


import Foundation

func solution(_ s:String) -> Int {
    
    var testArray = s
    var resultArray = ""
    var index : String.Index
    
    if !testArray.isEmpty {
        index = testArray.index(testArray.startIndex, offsetBy: 1)
    } else {
        index = testArray.startIndex
    }
   
    func removeElements(times: Int) {
        for _ in 0..<times {
            testArray.removeFirst()
        }
    }
    
    while !testArray.isEmpty {
        for char in testArray {
            
            if char == "z" {
                resultArray.append("0")
                removeElements(times: 4)
                break
            
            } else if char == "o" {
                resultArray.append("1")
                removeElements(times: 3)
                break
                
            } else if char == "t" {
                
                if testArray[index] == "w" {
                    resultArray.append("2")
                    removeElements(times: 3)

                } else {
                    resultArray.append("3")
                    removeElements(times: 5)
                }
                break
                
            } else if char == "f" {
                if testArray[index] == "o" {
                    resultArray.append("4")
                    removeElements(times: 4)
                } else {
                    resultArray.append("5")
                    removeElements(times: 4)
                }
                break
            } else if char == "s" {
                if testArray[index] == "i" {
                    resultArray.append("6")
                    removeElements(times: 3)
                } else {
                    resultArray.append("7")
                    removeElements(times: 5)
                }
                break
                
            } else if char  == "e" {
                resultArray.append("8")
                removeElements(times: 5)
                break
            } else if char == "n" {
                resultArray.append("9")
                removeElements(times: 4)
                break
            } else {
                resultArray.append(testArray.removeFirst())
            }
            
        }
    }
    
    return Int(resultArray)!
}

  • これも通れます…!
  • どうも無実だ