58. Length of Last Word
来源:互联网 发布:usb数据传输线 编辑:程序博客网 时间:2024/06/05 11:00
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
.
public class Solution { public int lengthOfLastWord(String s) { if(s == null || s.length() == 0){ return 0; } int res = 0; for(int i=s.length() - 1; i>=0; i--){ if(s.charAt(i) != ' '){ res++; } if(s.charAt(i) == ' ' && res != 0){ break; } } return res; }}
注意判断最后一个是空格的情况
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
- 2016年回顾及2017年规划
- Leetcode Count of Smaller Numbers After Self
- .gitignore 比较完善的写法
- JAVA编程风格
- exercise 38 列表操作
- 58. Length of Last Word
- ABAP Bom按层展开的几种实现方法
- 139. Word Break
- HDU 1159 Common Subsequence
- 解决android扫描二维码时,用户禁止权限报错问题
- eMMC 原理 :Flash Memory 简介
- canny边缘检测C++实现
- GitLab的安装实战
- 启动顺序导致的 ntldr is missing 错误情况