The code you provided has a couple of syntax errors. Here's the corrected version:

class Solution {
    func isLetter(_ character: Character) -> Bool {
        let characterSet = CharacterSet.letters
        let unicodeScalar = UnicodeScalar(String(character))!
        return characterSet.contains(unicodeScalar)
    }
    
    func lengthOfLastWord(_ s: String) -> Int {
        var t: String = String(s.reversed())
        var l: Int = 0
        var ans: Int = 0
        for i in t {
            if l != 0 && i == " " && self.isLetter(t[t.index(t.startIndex, offsetBy: l-1)]) {
                return ans
            }
            l += 1
            if self.isLetter(i) {
                ans += 1
            }
        }
        return ans
    }
}

This version should compile without any errors. Note that I replaced t[l-1] with t[t.index(t.startIndex, offsetBy: l-1)] to access the character at index l-1 in the t string

class Solution func isLetter_ character Character - Bool let characterSet = CharacterSetletters let unicodeScalar = UnicodeScalarStringcharacter! return characterSetcontainsu

原文地址: https://www.cveoy.top/t/topic/ihah 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录