[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",
return5.

思路:要求最后一个单词的长度,首先想到的当然是从后往前开始遍历,若遇到空格或者下标等于0了,就返回单词的个数就行,但是,这样想忽略了一个问题,若是,字符串最后有空格怎么办?如: s ="Hello World    ",所以应该是从后往前遍历的时候,先跳过空格,直到遇到第一个非空格的字符,才开始计数。代码如下:

 1 class Solution {
 2 public:
 3     int lengthOfLastWord(const char *s)
 4     {
 5         int res=0;
 6         int len=strlen(s);
 7         if(s==NULL) return 0;
 8         int i=len-1;
 9         while(s[i]==‘ ‘)
10             i--;
11         while(s[i] !=‘ ‘&&i>=0)
12         {
13             i--;
14             res++;
15         }
16         return res;
17     }
18 };
时间: 2024-12-28 18:18:47

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

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

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

[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

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(最后一个单词的长度)

[ 问题: ] 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 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)

目录 题目描述: 示例: 解法: 题目描述: 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: "Hello World" 输出: 5 解法: class Solution { public: int lengthOfLastWord(string s) { int sz = s.size(); int i = 0, j = 0; string

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-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】

[058-Length of Last Word (最后一个单词的长度)] [LeetCode-面试算法经典-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

LeetCode: Length of Last Word [057]

昨天同事在做主从时,从库报如下错误: Got fatal error 1236 from master when reading data from binary log: 'Misconfigured master - server id was not set' 粗粗看好像是master的server-id没有设置,但同事做如下查询: 备库采集: [email protected] Fri May 23 14:18:59 2014 14:18:59 [(none)]> show variab