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