美文网首页Leetcode
Leetcode 58. Length of Last Word

Leetcode 58. Length of Last Word

作者: SnailTyan | 来源:发表于2018-09-09 21:16 被阅读4次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Length of Last Word

2. Solution

class Solution {
public:
    int lengthOfLastWord(string s) {
        int length = 0;
        int index = s.find_last_not_of(" ");
        if(index == -1) {
            return length;
        }
        while(index >= 0 && s[index--] != ' ') {
            length++;
        }
        return length;
    }
};

Reference

  1. https://leetcode.com/problems/length-of-last-word/description/

相关文章

网友评论

    本文标题:Leetcode 58. Length of Last Word

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