【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 characters only.

For example,

Given s = "Hello World",

return 5.

思路:从后往前搜索直到第一个空格字符,注意“a ”的结果是1而不是0,因此需要先排除最后的空格字符。

class Solution {
public:
    int lengthOfLastWord(const char *s) {
        if(s == NULL)   return 0;

        int length = strlen(s);

        int result = 0;
        for(int i = length - 1; i >= 0 && s[i] == ' '; i--)
            length--;

        for(int i = length - 1; i >= 0; i--)
        {
            if(s[i] != ' ')
                result++;
            else
                break;
        }

        return result;
    }
};

【Leetcode】Length of Last Word,布布扣,bubuko.com

时间: 2024-08-26 04:37:55

【Leetcode】Length of Last Word的相关文章

【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. 给你一个字符串,设法获取它最后一个单词的长度.如果这个单词不存在,则返回0. [ 分析 : ] A word is defined

【leetcode】length of last word (easy)

题目: 输入字符串 s,返回其最后一个单词的长度 如 s="Hello World"   返回5 s="Hello World    "   返回5 s="  "     返回0 开始从前向后判断,超时了.改成从后向前判断,通过了. class Solution { public: int lengthOfLastWord(const char *s) { int length = 0; int slen = strlen(s); for(int

【Leetcode】【Easy】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

【leetcode】Maximum Product of Word Lengths

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Example

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【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 cha

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【leetcode】Word Break

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa