[LeetCode] Length of Last Word
2015-06-28 16:11
337 查看
Well, the basic idea is very simple. Start from the tail of
sand move backwards to find the first non-space character. Then from this character, move backwards and count the number of non-space characters until we pass over the head of
sor meet a space character. The count will then be the length of the last word.
class Solution { public: int lengthOfLastWord(string s) { int len = 0, tail = s.length() - 1; while (tail >= 0 && s[tail] == ' ') tail--; while (tail >= 0 && s[tail] != ' ') { len++; tail--; } return len; } };
相关文章推荐
- Android 异常分类及处理
- GIT 学习二 (查看日志和回退)
- postfix之邮件服务基础知识
- Acdream 1113 The Arrow(概率dp)
- spring jdbctemplate
- SQL中将一列的多行内容拼接成一行
- Oracle ->> 生成测试数据
- [Effective Java]第二章 创建和销毁对象
- SpringMVC+MyBatis环境整合开发案例
- android中的数据交换格式之JSON
- [C#学习笔记]获取当前系统的基本信息
- 鸿学金信风控详细介绍
- 杭电ACM1297——Children’s Queue~~大数相加的应用
- linux下定时任务设置
- SQL中Left Join、Right Join和Inner Join的使用
- 134.根据公式计算和
- SpringMVC国际化
- 如何在ORACLE中异步调用存储过程的方法
- Cocos2d-x项目总结中的一些遇到的问题
- android handler机制的简要分析