美文网首页
58. 最后一个单词的长度 leetcode

58. 最后一个单词的长度 leetcode

作者: 出来遛狗了 | 来源:发表于2018-11-02 11:05 被阅读1次
image.png
class Solution {
    func lengthOfLastWord(_ s: String) -> Int {
       if s.count == 0{
            return 0;
        }
        var lenth = 0;
        var start = false;
        for (_,value) in s.enumerated().reversed(){
            if value == " "{
                if start{
                    return lenth;
                }
            }else {
                lenth += 1
                start = true;
            }
        }
        return lenth;
    }
}

相关文章

网友评论

      本文标题:58. 最后一个单词的长度 leetcode

      本文链接:https://www.haomeiwen.com/subject/xgmqxqtx.html