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.

Hide Tags

String

class Solution {
public:
    int lengthOfLastWord(const char *s) {
        if(s==NULL)
            return 0;
        int len=0;
        const char *start = s;
        while(*s!=‘\0‘){
            if(*s==‘ ‘){
                int le=s-start;
                if(le!=0)
                    len=le;
                start=s+1;
                s++;
            }else
                s++;
        }
        int le=s-start;
        if(le!=0)
            len=le;
        return len;
    }
};
时间: 2024-10-15 15:59:38

Length of Last Word输出最后单词的字母个数的相关文章

返回最长单词的字母个数

返回最长单词的字母个数 Javascript 这种写法太清晰了. 使用了 Math的 max. 用了 Array 的 map 迭代. 用了回调. 用了字符串的分隔. return Math.max.apply(Math, str.split(" ").map(function(el) { return el.length;})); 原文地址:https://www.cnblogs.com/F4NNIU/p/10677107.html

[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

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

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

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

[LeetCode]38. 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—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】- 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 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