LeetCode 58. Length of Last Word【模拟/字符串】简单

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。

为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库:https://github.com/memcpy0/LeetCode-Conquest。在这一仓库中,你不仅可以看到LeetCode原题链接、题解代码、题解文章链接、同类题目归纳、通用解法总结等,还可以看到原题出现频率和相关企业等重要信息。如果有其他优选题解,还可以一同分享给他人。

由于本系列文章的内容随时可能发生更新变动,欢迎关注和收藏征服LeetCode系列文章目录一文以作备忘。

Given a string s consisting of some words separated by some number of spaces, return the length of the last word in the string.

A word is a maximal substring consisting of non-space characters only.

Example 1:

Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.

Example 2:

Input: s = "   fly me   to   the moon  "
Output: 4
Explanation: The last word is "moon" with length 4.

Example 3:

Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.

Constraints:

  • 1 <= s.length <= 104
  • s consists of only English letters and spaces ' '.
  • There will be at least one word in s.

题意:给你一个字符串 s ,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中最后一个单词的长度。


解法 模拟

很简单的题目。 我们根据题意,「从后往前」对字符串进行遍历。具体来说:

  • 使用指针 j 指向字符串末尾,j 不断前移跳过空格,直到指向目标单词的最后一个字符;
  • 之后 j 不断前移,直至遇到空格或者到达字符串首位,同时使用 len 计数;
  • 返回 len 作为函数结果。

实际代码如下所示。算法的时间复杂度为 O ( n ) O(n) O(n) ,空间复杂度为 O ( 1 ) O(1) O(1) :

//C++ version
class Solution {
public:
    int lengthOfLastWord(string s) {
        int j = s.size() - 1, len = 0;
        while (j >= 0 && s[j] == ' ') --j; //跳过空格
        while (j >= 0 && s[j] != ' ') ++len, --j; //计算最后一个单词长度
        return len;
    }
};
//执行用时:0 ms, 在所有 C++ 提交中击败了100.00% 的用户
//内存消耗:6.3 MB, 在所有 C++ 提交中击败了97.48% 的用户
上一篇:npt时间服务


下一篇:HalconCpp判断HObject数据是否为空