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】的更多相关文章

  1. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  2. 433. Number of Islands【LintCode java】

    Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...

  3. 423. Valid Parentheses【LintCode java】

    Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...

  4. 420. Count and Say【LintCode java】

    Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...

  5. 415. Valid Palindrome【LintCode java】

    Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...

  6. 408. Add Binary【LintCode java】

    Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...

  7. 407. Plus One【LintCode java】

    Description Given a non-negative number represented as an array of digits, plus one to the number. T ...

  8. 373. Partition Array by Odd and Even【LintCode java】

    Description Partition an integers array into odd number first and even number second. Example Given  ...

  9. 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 ...

随机推荐

  1. EF Core中Join可以进行子查询

    我们来看看下面的代码,这个代码是一个INNER JOIN的EF Core查询,其中用SubCategory表INNER JOIN了SubCategoryLanguage表,但是我们需要在SubCate ...

  2. Linux下ELK环境搭建

    一.准备工作 准备3台机器,这样才能完成分布式集群的实验,当然能有更多机器更好: 192.168.3.64(e1) 192.168.3.62  (e2) 192.168.3.63(e3) 角色划分: ...

  3. python 基础 切片 迭代 列表生成式

    对list 进行切片 如列表 L = ['Adam', 'Lisa', 'Bart', 'Paul'] L[0:3] ['Adam', 'Lisa', 'Bart'] L[0:3]表示,从索引0开始取 ...

  4. GBK 文件在 sublime 保存时被强制保存为 utf-8 导致中文乱码, 恢复。

    原来在 CoverteToUTF8 的 README.zh_CN.md 文件里就有解决方法,如下: * 问:我的文件被保存为 UTF-8,而且变成了乱码,要如何恢复? 答:请打开这个文件,并确认它的编 ...

  5. sublime Text3安装 markdownediting 报错 解决记录

    看了一下官方文档,也是醉了,都怪自己的无知. 在安装时候不要打开.md的文件,因为你里面有可能有一些语法错误,所以会导致报错. 解决方法关闭其他文件,在重新安装! 官方解释: 参考文档:Trouble ...

  6. python使用tablib库生成xls表格

    参考文档:http://python-tablib.org Tablib是一个MIT许可的格式不可知的表格数据集库.它允许您导入,导出和操作表格数据集.高级功能包括隔离,动态列,标签和过滤,以及无缝格 ...

  7. js 实现内容的展开和收缩

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. 微信小程序实现转义换行符

    在html中可以直接使用<br />换行,但是小程序wxml中使用<br />无效,可以换成\n Page({ data: { title: '至少5个字\n请多说些感受吧' ...

  9. thinkPHP写txt日志文件

     file_put_contents(DATA_PATH.'文件名.txt', '收到请求:' . date('Y-m-d H:i:s') . PHP_EOL . '通知信息:' . $显示的变量名. ...

  10. Hadoop生态新增列式存储系统Kudu

        Hadoop生态系统发展到现在,存储层主要由HDFS和HBase两个系统把持着,一直没有太大突破.在追求高吞吐的批处理场景下,我们选用HDFS,在追求低延迟,有随机读写需求的场景下,我们选用H ...