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. 使用navigator.userAgent来进行浏览器嗅探

    /*--------------------------------------------------------------------------------* * 功能描述:使用navigat ...

  2. CSS动画总结效果

       CSS3添加了几个动画效果的属性,通过设置这些属性,可以做出一些简单的动画效果而不需要再去借助JavaScript.CSS3动画的属性主要分为三类:transform.transition以及a ...

  3. HDU1005 Number Sequence(找规律,周期是变化的)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/O ...

  4. 使用DBNEWID Utility 修改oracle数据库的 db name 和 dbid

    使用DBNEWID Utility 工具可以同时修改数据库名.DBID,也可以只修改其中一项 官方参考: https://docs.oracle.com/cd/E11882_01/server.112 ...

  5. Oracle 执行计划的查看方式

    访问数据的方法:一.访问表的方法:1.全表扫描,2.ROWID扫描                                二.访问索引的方法:1.索引唯一性扫描,2.索引范围扫描,3.索引全扫 ...

  6. IO流C++

    1.iostream处理控制台IO #include<iostream> #include<string> using namespace std; istream& ...

  7. MySQL5.7.24安装笔记

    一.下载mysql-5.7.24 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-el7-x86_64.tar.gz 二 ...

  8. MySQL慢日志查询实践

    慢日志查询作用 慢日志查询的主要功能就是,记录sql语句中超过设定的时间阈值的查询语句.例如,一条查询sql语句,我们设置的阈值为1s,当这条查询语句的执行时间超过了1s,则将被写入到慢查询配置的日志 ...

  9. ElasticSearch优化系列七:优化建议

    尽量运行在Sun/Oracle JDK1.7以上环境中,低版本的jdk容易出现莫名的bug,ES性能体现在在分布式计算中,一个节点是不足以测试出其性能,一个生产系统至少在三个节点以上. ES集群节点规 ...

  10. Redis学习笔记(二)

    解读Retwis官网例子 Redis需要考虑需要哪些keys以及对应的value使用合适的数据类型进行存储.在retwis例子中,我们需要users,user的粉丝列表, user的关注用户列表等等. ...