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.

解题思路1:

遍历字符串,设置整形变量n,记录当前遍历到的无空格子串的长度,如果子串后遇到空格,且空格后再次遇到新的子串,则更新n,否则返回n;

注意:

本题容易忽略的是,返回最后一个子串的长度,不能简单想做遇到空格就重新计数,因为可能空格后不再有子串了,也就是最后一个子串结束后,字符串没有完,还有空格存在。因此要注意对这种情况进行判断。

 class Solution {
public:
int lengthOfLastWord(string s) {
int len = s.size();
int len_lw = ; for (int i = ; i < len; ++i) {
if (i != && s[i-] == ' ' && s[i] != ' ')
len_lw = ;
if (s[i] != ' ')
len_lw++;
} return len_lw;
}
};

解题思路2:

直接从后遍历字符串,先过滤尾部的空格,之后遍历的第一个子串长度就是要返回的结果。

 class Solution {
public:
int lengthOfLastWord(const char *s) {
int len = strlen(s);
int sum = ; while (s[len-] == ' ')
len--; for (int i=len-; i>=; i--) {
if(s[i]!=' ')
sum++;
else
break;
} return sum;
}
};

附录:

1、C语言中的char* char const char 和C++ string的关系

2、C++中对字符串操作的函数总结

3、灵活运用字符串指针,多用指针操作,少用数组操作:

 class Solution {
public:
int lengthOfLastWord(const char* s) {
int len = ; while (*s) {
if (*s++ != ' ')
++len;
else if (*s && *s != ' ')
len = ;
} return len;
}
};

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

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  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每天一题】Length of Last Word(字符串中最后一个单词的长度)

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

  6. 【leetcode刷题笔记】Length of Last Word

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

  7. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  8. 【leetcode刷题笔记】Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  9. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  10. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

随机推荐

  1. BZOJ 2935/ Poi 1999 原始生物

    [bzoj2935][Poi1999]原始生物   2935: [Poi1999]原始生物 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 145  So ...

  2. 最大比例(贪心+思维+gcd)

    X星球的某个大奖赛设了M级奖励.每个级别的奖金是一个正整数. 并且,相邻的两个级别间的比例是个固定值. 也就是说:所有级别的奖金数构成了一个等比数列.比如: 16,24,36,54 其等比值为:3/2 ...

  3. 队列 102 Binary Tree Level Order Traversal

    队列的基本应用 - 广度优先遍历 1)树 : 层序遍历: 2)图:无权图的最短路径. 使用队列来实现二叉树的层序遍历,需要多关注一个层数的信息 /** * Definition for a binar ...

  4. POJ:2456 Aggressive cows(z最大化最小值)

    描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000, ...

  5. Spring整合AspectJ的AOP

    学而时习之,不亦说乎!                              --<论语> 看这一篇之前最好先看前面关于AOP的两篇. http://www.cnblogs.com/z ...

  6. nodejs的一些学习

    要使用npm的时候,其实是可以直接下载node.js的.参考文档http://www.runoob.com/nodejs/nodejs-npm.html 安装成功之后.判断是否安装成功.是不能直接用n ...

  7. PIE SDK算法的同步调用

    1.    算法功能简介 同步调用一旦开始,调用者必须等到方法调用返回后,才能继续后续的行为. PIE SDK支持算法功能的执行,下面对算法的同步调用功能进行介绍. 2.    算法功能实现说明 2. ...

  8. kali 安装命令类

    apt-get常用命令:update – 取回更新的软件包列表信息upgrade – 进行一次升级install – 安装新的软件包(注:软件包名称是 libc6 而非 libc6.deb)remov ...

  9. 小程序 开发阶段请求网络报 不在以下 request 合法域名列表中

    1.在工具栏右边,点开详情, 把图片最后一项选上,再重新编译一下项目就可以了. 2.管理员将需要使用的域名添加到小程序后台 1. 地址:http://mp.weixin.qq.com (需要请求的域名 ...

  10. bnu 28890 &zoj 3689——Digging——————【要求物品次序的01背包】

    Digging Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 36 ...