[leetcode-58]Length of Last Word(c)
来源:互联网 发布:淘宝流量互刷 编辑:程序博客网 时间:2024/04/28 19:19
问题描述:
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.
分析:这道题很简单,只要从后向前,将空格过滤掉,然后如果没有了,直接返回0,然后再从后向前查找,当出现空格时,退出。返回计数。
代码如下:0ms
int lengthOfLastWord(char* s) { int length = strlen(s); int index = length-1; while(index>=0&&s[index]==' ') index--; if(index<0) return 0; int count = 0; while(index>=0&&s[index]!=' ') { count++; index--; } return count;}
0 0
- 【leetcode c++】58 Length of Last Word
- [leetcode-58]Length of Last Word(c)
- [C++]LeetCode: 23 Length of Last Word
- [leetcode] Length of Last Word C语言
- [leetcode 58] Length of Last Word
- leetcode-58 Length of Last Word
- Leetcode NO.58 Length of Last Word
- LeetCode #58 length of Last Word
- [LeetCode 58]Length of Last Word
- leetcode || 58、Length of Last Word
- LeetCode 58 Length of Last Word
- [Leetcode 58, easy] Length of Last Word
- Length of Last Word (leetCode 58)
- leetcode[58]:Length of Last Word
- leetcode 58:Length of Last Word
- LeetCode---(58)Length of Last Word
- leetcode-58-Length of Last Word
- Leetcode# 58 Length of Last Word
- 李滔搜狐大数据之Spark广告和新闻推荐
- PAT (Advanced Level) 1089. Insert or Merge (25) 插入和归并
- spring 手工实现AOP编程
- iBatis开发详解(2)----------增删改查
- 二叉树思想小结
- [leetcode-58]Length of Last Word(c)
- git 远程操作常用命令
- JSP学习笔记(一)---JSP基础
- ios之简单的个税计算器
- hdu-1950 Bridging signals
- 状态压缩动态规划入门篇
- iOS文章
- 45 - 圆圈中最后剩下的数字
- eclipse启动时jdk环境变量的问题及解决办法