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; //以空格结束 }
};

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

  1. Leetcode(58)题解:Length of Last Word

    https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...

  2. LeetCode OJ:Length of Last Word(最后一个词的长度)

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

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

    这是悦乐书的第155次更新,第157篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第14题(顺位题号是58).给定一个字符串,包含戴尔字母.小写字母和空格,返回最后一个单 ...

  4. 【LeetCode算法-58/66】Length of Last Word/Plus One

    LeetCode第58题: Given a string s consists of upper/lower-case alphabets and empty space characters ' ' ...

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

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  6. LeetCode练题——58. Length of Last Word

    1.题目 58. Length of Last Word——Easy Given a string s consists of upper/lower-case alphabets and empty ...

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

    一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...

  8. 【LeetCode】58. Length of Last Word 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 双指针 单指针 日期 题目地址:https: ...

  9. LeetCode 58. Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

随机推荐

  1. andriod 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用

    package com.example.yanlei.myapplication; import android.media.MediaMetadataRetriever; import androi ...

  2. UILabel混合显示动画效果

    UILabel混合显示动画效果 效果 源码 https://github.com/YouXianMing/Animations // // MixedColorProgressViewControll ...

  3. 《Windows核心编程》第3章——handle复制相关实验

    先写一个程序,用来查看进程的内核对象,这样我们就能比较子进程是否继承了父进程的某个句柄: #include <windows.h> #include <stdio.h> #de ...

  4. uestc 360(区间合并)

    题意:有一个长度为n的序列.然后有两种操作,Q a b是输出区间a b内最长上升子序列的长度.A a b c是把区间a b内全部数字加上c. 题解:用线段树维护区间的最长上升子序列长度,那么一个区间的 ...

  5. iOS:UIToolBar控件的使用

    UIToolBar控件:是经常使用的一个工具条控件,虽然在上面可以添加子控件,但是toolbar中只能添加UIBarButtonItem类型的子控件,其他子控件会被包装成这种类型的,例如UIButto ...

  6. 【右滑返回】滑动冲突 Scroller DecorView

    基本思想 我们的滑动逻辑主要是利用View的scrollBy() 方法, scrollTo()方法和Scroller类来实现的 当手指拖动视图的时候,我们监听手指在屏幕上滑动的距离 利用View的sc ...

  7. 30条技巧提高Web程序执行效率

    尽量避免使用DOM.当需要反复使用DOM时,先把对DOM的引用存到JavaScript本地变量里再使用.使用设置innerHTML的方法来替换document.createElement/append ...

  8. POJ 2488 A Knight&#39;s Journey

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29226   Accepted: 10 ...

  9. android 随手记 摄像头录像

    1 xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androi ...

  10. UVa 10820 - Send a Table

    题目:找到整数区间[1.n]中全部的互质数对. 分析:数论,筛法,欧拉函数.在筛素数的的同一时候.直接更新每一个数字的欧拉函数. 每一个数字一定会被他前面的每一个素数筛到.而欧拉函数的计算是n*π(1 ...