leetcode || 58、Length of Last Word

problem:

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

题意:给一个字符串,包括大写和小写的字母和空格,求最后一次出现的不包括空格的字符串的长度

thinking:

(1)充分理解题意是解这道题的关键。

对于一些特殊情况: char *s=‘abc "  (最后一个是空格),   char  *s = "a   b     "(连续空格)要注意到

(2)刚開始考虑使用 指针之差 来计算字符串的长度。发现掉入了一个无底深渊。各种特殊情况都要考虑在内,能解出来,可是比較绕

(3)採用 计数法  来解这道题最简单高效,遇到连续的非空格字符串開始计数,遇到空格保存计数结果,也有一些细节要注意。在代码中有凝视

(4)这道题考擦的是 分析问题的全面性 和 细节处理能力

code:

class Solution {
public:
    int lengthOfLastWord(const char *s) {
        int i=0;
        int count=0;
        int length=0;
        while(*(s+i)!=‘\0‘)
        {
            if(*(s+i)!=‘ ‘)
                count++;
            else
            {
                if(count>0) //出现连续空格时,防止清空上次有效计数结果
                    length=count;
                count=0;
            }
            i++;
        }
        if(count!=0) //善后处理:以非空格结束的字符串
            return count;
        else
            return length; //以空格结束

    }
};
时间: 2024-08-07 21:09:13

leetcode || 58、Length of Last Word的相关文章

Leetcode(58)题解:Length of Last Word

https://leetcode.com/problems/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 de

LeetCode OJ: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 细节实现 Length of Last Word

Length of Last Word Total Accepted: 17518 Total Submissions: 59526My Submissions 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, re

【算法】LeetCode算法题-Length Of Last Word

这是悦乐书的第155次更新,第157篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第14题(顺位题号是58).给定一个字符串,包含戴尔字母.小写字母和空格,返回最后一个单词的长度,如果最后一个单词不存在则返回0.另外,单词不包含空格.例如: 输入: "Hello World" 输出: 5 说明:最后一个单词为world,其长度为5 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 0

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

【一天一道LeetCode】#58. Length of Last Word

一天一道LeetCode系列 (一)题目 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 cons

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