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

2018-11-02  本文已影响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;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读