58. Length of Last Word
来源:互联网 发布:星际争霸2网络问题 编辑:程序博客网 时间:2024/05/29 11:52
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
.
Subscribe to see which companies asked this question.
Solution:
Tips:
reverse count the word length.
Java Code:
public class Solution { public int lengthOfLastWord(String s) { if (null == s || s.trim().isEmpty()) { return 0; } s = s.trim(); int wordLength = 0; for (int i = s.length() - 1; i >= 0; i--) { if (s.charAt(i) == ' ') { return wordLength; } else { wordLength++; } } return wordLength; }}
0 0
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58. Length of Last Word
- 58.Length of Last Word
- 资深制作人谈游戏策划如何入行
- leetcode [Factorial Trailing Zeroes]
- 中介者模式
- 文章标题:Splash是什么?
- 写给程序员的有效学习方法
- 58. Length of Last Word
- GridView等组件绑定后台数据源列的绑定方法,onclick方法调用中含有Eval绑定如何调用js方法
- stringstream真的很好用, 但要注意坑(clear函数之坑, 之前说过)
- 基于IMOOC强力django+杀手级xadmin 打造上线标准的在线教育平台课程的学习(3)
- C#中的多态性
- 希尔排序详解
- XShell远程连接linux系统[转载]
- Debian navicat 相关问题及解决办法
- java将包含unicode的字符串转换成中文