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.

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

For example, 
Given s = "Hello World",
return 5.

public class LengthOfLastWord
{
public int lengthOfLastWord(String s)
{
if(s!=null && !s.trim().equals(""))//equals比较的是内容,==比较的是地址
{
//去掉开始和结尾的空格
String[] arr = s.trim().split(" ");
int len = arr[arr.length - 1].length();
return len;
}
return 0;
}
}

LengthOfLastWord,字符串最后一个子串的长度的更多相关文章

  1. 华为机试001:字符串最后一个单词的长度(华为OJ001)

    华为机试 字符串最后一个单词的长度 计算字符串最后一个单词的长度,单词以空格隔开. 提交网址: http://www.nowcoder.com/practice/8c949ea5f36f422594b ...

  2. HW—字符串最后一个单词的长度,单词以空格隔开。

    描述 计算字符串最后一个单词的长度,单词以空格隔开. 知识点 字符串,循环 运行时间限制 0M 内存限制 0 输入 一行字符串,长度小于128. 输出 整数N,最后一个单词的长度. 样例输入 hell ...

  3. OJ题:字符串最后一个单词的长度

    题目描述 计算字符串最后一个单词的长度,单词以空格隔开. 输入描述: 一行字符串,非空,长度小于5000. 输出描述: 整数N,最后一个单词的长度. 输入例子: hello world 输出例子: 5 ...

  4. 华为oj之字符串最后一个单词的长度

    题目: 字符串最后一个单词的长度 热度指数:9697 时间限制:1秒 空间限制:32768K 本题知识点: 字符串 题目描述 计算字符串最后一个单词的长度,单词以空格隔开. 输入描述: 一行字符串,非 ...

  5. C语言之计算字符串最后一个单词的长度,单词以空格隔开

    //计算字符串最后一个单词的长度,单词以空格隔开. #include<stdio.h> #include<string.h> #include<windows.h> ...

  6. Reverse Word in a String(翻转字符串)&字符串最后一个单词的长度

    1.题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is ...

  7. test_1 计算字符串最后一个单词的长度,单词以空格隔开

    题目描述:计算字符串最后一个单词的长度,单词以空格隔开.  输入描述: 一行字符串,非空,长度小于5000. 输出描述: 整数N,最后一个单词的长度. #coding=utf-8 str = raw_ ...

  8. HWOJ-求字符串最后一个单词的长度

    题目:给定一个字符串,求最后一个单词的长度,每个单词中间有空格. 例如:输入:hello world   输出:5 C代码:通过. #include <stdio.h> #define m ...

  9. 华为oj 字符串最后一个单词的长度

    <img alt="http://img.bbs.csdn.net/upload/201508/06/1438867109_670158.jpg" src="htt ...

随机推荐

  1. 关于TNS_ADMIN环境变量

    转自:https://blog.csdn.net/pan_tian/article/details/7699599 很多oracle产品都有自己的TNS文件,如果你的系统里装了多个Oracle的产品的 ...

  2. Leetcode-Construct Binary Tree from inorder and postorder travesal

    Given inorder and postorder traversal of a tree, construct the binary tree. Solution: /** * Definiti ...

  3. cmake window下 sh.exe was found in your PATH, here

    在window下 mingw环境下 用 camke 编译Cpp程序 CMake Error at D:/Program Files/CMake/share/cmake-3.8/Modules/CMak ...

  4. mvel

    https://en.wikipedia.org/wiki/MVEL import java.util.*; // the main quicksort algorithm def quicksort ...

  5. Pipeline inbound

    精进篇:netty源码死磕7  巧夺天工--Pipeline入站流程详解 1. Pipeline的入站流程 在讲解入站处理流程前,先脑补和铺垫一下两个知识点: (1)如何向Pipeline添加一个Ha ...

  6. always on 之路实践(未完)

    概念及参考:http://www.mssqlmct.cn/dba/?post=97 准备:利用vmvare workstation12 克隆了4台windows server 2008 datacen ...

  7. EasySQLMAIL使用实践系列

    原文:http://blog.sina.com.cn/s/articlelist_5713986487_0_1.html 通过sql语句发送微信消息(转) 使用EasySQLMAIL的外部接口功能实现 ...

  8. Tornado模块分类

    Tornado模块分类 1. Core web framework tornado.web — 包含web框架的大部分主要功能,包含RequestHandler和Application两个重要的类 t ...

  9. 使用Free命令查看Linux服务器内存使用状况(-/+ buffers/cache详解)

    free命令可选参数 -b,-k,-m,-g show output in bytes, KB, MB, or GB -h human readable output (automatic unit ...

  10. js 添加css属性

    $(".active").css('border','1px solid #ddd')curLi.css('border','2px solid red')curLi.css('b ...