Leetcode 58. Length of Last Word

版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/82562458

文章作者: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/

猜你喜欢

转载自blog.csdn.net/Quincuntial/article/details/82562458