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

 

    这题比较简单,只是可能前面有空格,后面有空格。- -
算法逻辑:
  1. 排除最后的空格
  2. index 从后往前查第一个空格。
  3. 返回长度。
 #include <iostream>
#include <cstring>
using namespace std; class Solution {
public:
int lengthOfLastWord(const char *s) {
int n = strlen(s);
if(n <) return ;
int idx=n-;
while(idx>=&&s[idx]==' ') idx--;
n = idx+;
while(idx>=){
if (s[idx]==' ') break;
idx --;
}
// if(idx<0) return 0;
return n - idx -;
}
}; int main()
{
char s[] = " 12 ";
Solution sol;
cout<<sol.lengthOfLastWord(s)<<endl;
return ;
}

最新文章

  1. C#抽象类
  2. javascript 分页组件
  3. JS识记
  4. text .global_start 和_start
  5. Springmvc中 同步/异步请求参数的传递以及数据的返回
  6. 2015 UESTC Winter Training #8【The 2011 Rocky Mountain Regional Contest】
  7. PHP实现队列(Queue)数据结构
  8. iwebshop中的增删改查
  9. 跨过Nginx上基于uWSGI部署Django项目的坑
  10. Linux 链接详解----静态链接实例分析
  11. mysql中float类型使用总结
  12. 大话RabbitMQ 基础入门
  13. VUE中的v-show和v-if
  14. Codeforces.741D.Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree 思路)
  15. NFS 网络文件系统制作
  16. 记unit of work与事务提交
  17. Mybatis分页查询与动态SQL
  18. 代码收藏系列--jquery--筛选器、事件绑定技巧
  19. DER编码简介
  20. Linux下改动Matlab配置文件支持C++ 11标准以生成mex

热门文章

  1. 同时启动多个tomcat的配置信息
  2. Ecshop之ajax修改表里的状态(函数化处理)
  3. Mysql存储过程中的事务回滚
  4. Android广告页循环播放
  5. Codeforces 653G Move by Prime 组合数学
  6. jquery 如何实现回顶部 带滑动效果
  7. B树、B-树、B+树、B*树之间的关系
  8. 朴素贝叶斯python小样本实例
  9. cf976f Minimal k-covering
  10. linux/mac下的配置自定义命令alias