Description

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.

A word is defined as a character sequence consists of non-space characters only.

Example

Given s = "Hello World", return 5.

解题:返回最后一个单词,注意是要用一个变量缓冲一下,否则一个字符串的最后几个字符为空时,可能会丢失数据。代码如下:

public class Solution {
/**
* @param s: A string
* @return: the length of last word
*/
public int lengthOfLastWord(String s) {
// write your code here
int count = 0;
int res = 0;
for(int i = 0; i < s.length(); i++){
if( Character.isLetter(s.charAt(i)) ){
count++;
}else {
res = count;
count = 0;
}
}
if(count != 0)
return count;
else
return res;
}
}

最新文章

  1. 一小时包教会 —— webpack 入门指南
  2. 《DSP using MATLAB》示例Example5.11
  3. jquery easyui window中的datagrid,只能显示一次问题
  4. SPOJ ORDERSET - Order statistic set
  5. 【学】CSS3基础实例1 - 用CSS3做网页中的小三角,以及transition的用法
  6. ligerui_ligerTree_003_配置url参数,加载“树”
  7. 模式自由(Schema-free)和数据存储的非格式化趋势
  8. 剑指offer--面试题12
  9. toastr
  10. 老李分享:接电话之uiautomator 2
  11. Problem A
  12. Netty——高级发送和接收数据handler处理器
  13. CentOs7 systemd添加自定义系统服务
  14. 用Python浅析股票数据
  15. Android远程桌面助手(B1309)
  16. dell g3:安装ubuntu16.04 + CUDA8.0
  17. &lt;算法&gt;&lt;Union Find并查集&gt;
  18. MySQL的架构与历史
  19. Linux 定时任务crontab使用
  20. Oracle学习笔记(五)

热门文章

  1. vlc源码分析(三) 调用live555接收RTSP数据
  2. 404 Note Found 队-Alpha 事后诸葛亮
  3. Observer(观察者)模式
  4. html标签种类
  5. iOS:手势与矩形、点运算相关(18-01-24更)
  6. Angular7教程-04-Angular常用操作(下)
  7. 微信JSSDK的使用
  8. 【八】将日志写入log(glog)
  9. 嵌入式C语言自我修养 10:内联函数探究
  10. [FreeRTOS入门] 1.CubeMX中FreeRTOS配置参数及理解