422. Length of Last Word【LintCode java】
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;
}
}
422. Length of Last Word【LintCode java】的更多相关文章
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
- 415. Valid Palindrome【LintCode java】
Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...
- 408. Add Binary【LintCode java】
Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...
- 407. Plus One【LintCode java】
Description Given a non-negative number represented as an array of digits, plus one to the number. T ...
- 373. Partition Array by Odd and Even【LintCode java】
Description Partition an integers array into odd number first and even number second. Example Given ...
- 372. Delete Node in a Linked List【LintCode java】
Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...
随机推荐
- CDN的作用与基本过程
转载请注明出处: leehao.me 或 https://blog.csdn.net/lihao21/article/details/52808747 简介 CDN,Content Distribu ...
- [SCOI2005]互不侵犯(状压DP)
嗝~算是状压DP的经典题了~ #\(\mathcal{\color{red}{Description}}\) 在\(N×N\)的棋盘里面放\(K\)个国王,使他们互不攻击,共有多少种摆放方案.国王能攻 ...
- Selenium自动化测试之基本控件使用
Selenium自动化测试之基本控件使用 1.输入框input: 在Html中样式: <input id="username" type="text"&g ...
- 用PSCP在Windows和Linux之间相互传输文件
在Linux服务器之间相互传文件我们常用 scp命令,但是在Linux和Windows之间相互传输就不那么直接了. 使用 Putty的 PSCP 则会简单的多 1. 下载 http://www.chi ...
- 一次傻叉的安装ubuntu虚拟机记录
因为最近要做一些开发工作,涉及到游戏前后端到的开发,在将代码上传到github之前,所有开发版本的源文件都保存在我的虚拟机svn仓库.所以,就装了两台虚拟机,一台用作svn仓库以及git同步与更新,另 ...
- rman备份报错,全zero错误处理一例(bbed)
问题:某数据库在执行rman全备的时候,发现alert日志中有报错,报错提示, file 10,block 305076全部为zero,内容全零,处理过程如下 分析处理: 1. 这个问题可能是 系统或 ...
- maven多模块部署(转载)
[Maven]使用Maven构建多模块项目 Maven多模块项目 Maven多模块项目,适用于一些比较大的项目,通过合理的模块拆分,实现代码的复用,便于维护和管理.尤其是一些开源框架,也是采用多模块的 ...
- 网站jcms流程分析
本实例大致流程:基于jsp页面,通过servlet传递数据调用方法,利用service更改数据库.本文重点分析的是其中的两个小方法add()和delete(),来反映出反射机制的一个具体作用:减少Se ...
- 各种快速幂(qaq)
今天分享下各种快速幂(有点坑),首先说一下快速幂的原理, 以下以求a的b次方来介绍 [1] 把b转换成二进制数. 该二进制数第i位的权为 例如 11的二进制是1011 11 = 2³×1 + 2 ...
- Linux 三剑客之sed命令总结
sed ### sed ### .关键字取行 sed -n '/jpinsz/p' test.txt sed -n '/^d/p' test.txt .根据行数取行 sed -n '2,5p' tes ...