58. Length of Last Word

Posted optor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了58. Length of Last Word相关的知识,希望对你有一定的参考价值。

原题链接:https://leetcode.com/problems/length-of-last-word/description/
这题目没啥难度,稍微懂点编程均可独立完成:

class Solution {
    public int lengthOfLastWord(String s) {
        int len = 0;
        
        char[] chars = s.toCharArray();
        for (int i = chars.length - 1; i >= 0; i--) {
            if (chars[i] == ‘ ‘) {
                if (len > 0) {
                    return len;
                }
            } else {
                len++;
            }
        }
        
        return len;
    }
}

以上是关于58. Length of Last Word的主要内容,如果未能解决你的问题,请参考以下文章

58. Length of Last Word

LeetCode 58. Length of Last Word

#Leetcode# 58. Length of Last Word

58. Length of Last Word [easy] (Python)

58. Length of Last Word

58. Length of Last Word