[LeetCode] 58. Length of Last Word 求末尾单词的长度

Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

Example:

Input: "Hello World"
Output: 5

Java:

public int lengthOfLastWord(String s) {
    String use = s.trim();
    int count = 0;
    for (int i = use.length() - 1; i >= 0; i--) {
        if (use.charAt(i) != ‘ ‘) count++;
        else break;
    }
    return count;
}  

Java:

public int lengthOfLastWord(String s) {
    return s.trim().length()-s.trim().lastIndexOf(" ")-1;
}  

Python:

class Solution(object):
    def lengthOfLastWord(self, s):
        """
        :type s: str
        :rtype: int
        """
        return len(s.strip().split(‘ ‘)[-1])

Python:

class Solution:
    # @param s, a string
    # @return an integer
    def lengthOfLastWord(self, s):
        length = 0
        for i in reversed(s):
            if i == ‘ ‘:
                if length:
                    break
            else:
                length += 1
        return length

C++:

class Solution {
public:
    int lengthOfLastWord(string s) {
        int len = 0, tail = s.length() - 1;
        while (tail >= 0 && s[tail] == ‘ ‘) tail--;
        while (tail >= 0 && s[tail] != ‘ ‘) {
            len++;
            tail--;
        }
        return len;
    }
};

 

 

原文地址:https://www.cnblogs.com/lightwindy/p/9602354.html

时间: 2024-07-28 17:09:15

[LeetCode] 58. Length of Last Word 求末尾单词的长度的相关文章

Length of Last Word 求末尾单词的长度

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha

58. Length of Last Word最后一个单词的长度

[抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误判lastindexof().所以必须用.trim() [思维问题]: [一句话思路]: 用函数 再次强调是最后一位的索引是length() - 1 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [二刷]: [三刷]: [四刷]: [

leetCode 58. Length of Last Word 字符串

58. Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence c

Java [Leetcode 58]Length of Last Word

题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-spa

LeetCode#58 Length of Last Word

Problem Definition: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consi

leetcode 58 Length of Last Word ----- java

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha

leetcode—58 Length of Last Word Total(字符串中最后一个单词的长度)

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha

leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法

Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consi

[Leetcode] Length of last word 最后一个单词的长度

Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space char