Length of Last Word:求最后一个单词的长度

[ 问题: ]

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.

给你一个字符串,设法获取它最后一个单词的长度。如果这个单词不存在,则返回0。

[ 分析 : ]A word is defined as a character sequence consists of non-space characters only.

For example,

Given s = "Hello World",

return 5.

[ 解法: ]

public class Solution {  

    public int lengthOfLastWord(String s) {
        if (s != null && !s.trim().equals("")) {
            String[] arr = s.trim().split(" ");
            int length = arr[arr.length - 1].length();
            return length;
        }
        return 0;
    }  

    public static void main(String[] args) {
        String s = "leet code";
        System.out.println(new Solution().lengthOfLastWord(s));
    }
}

更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索string
, return
, 单词
, public
, length
一个
length of last word、last stick length、length of stay、the length of、length of time,以便于您获取更多的相关知识。

时间: 2024-07-28 21:51:08

Length of Last Word:求最后一个单词的长度的相关文章

[LeetCode] Length of Last Word - 最后一个单词的长度

题目概述: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-spac

[华为机试练习题]19.字符串最后一个单词的长度

题目 代码 /*--------------------------------------- * 日期:2015-06-30 * 作者:SJF0115 * 题目:字符串最后一个单词的长度 * 来源:华为上机 -----------------------------------------*/ #include <iostream> #include <string> #include <algorithm> #include <vector> using

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

题目描述 计算字符串最后一个单词的长度,单词以空格隔开. 输入描述: 一行字符串,非空,长度小于5000. 输出描述: 整数N,最后一个单词的长度. 输入例子: hello world 输出例子: 5 程序如下: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char str[5000] = {0}; int count = 0 ,start; gets(str);

设计-关于一个单词匹配的题目,得不到想要的结果,贴出源程序,求大神帮忙~~~

问题描述 关于一个单词匹配的题目,得不到想要的结果,贴出源程序,求大神帮忙~~~ #include#include#include#include#includeint IsOneCharDifferent(char array[255]);int IsIt(char array[255]);int IsMoreOrLessOneChar(char array[255]);//************************************************************

求大神,用javascript合并几个WORD并形成一个word,然后打印

问题描述 求大神,用javascript合并几个WORD并形成一个word,然后打印 有没有具体的方式实现呢?求大神围观 ,用javascript合并几个WORD并形成一个word,然后打印 要保持原有的文档格式,怎么做呢 解决方案 客户端js安全问题操作word很麻烦,要用ie的acx,其他浏览器没有acx搞不了 用服务器端的语言来合并转换成html形式进行打印还差不多 解决方案二: 单独用js实现不了,需要上传服务器合并后再下载,js去调用. 解决方案三: 不建议用JS合并,如果要合并 可以

正则表达式-【已解决】正则 零宽断言 匹配一个单词后的第一个数字

问题描述 [已解决]正则 零宽断言 匹配一个单词后的第一个数字 比如 [CM][bt][02][03] 我想匹配 02 我的正则是这样写的: i)(?<=bCMb(D*)?)d+(?=D) 但无法匹配成功,请问该怎样改呢? 感谢各位! 正则本身没错...错的是 PCRE 8.34–8.35 UTF-8 does not support variable repetition inside lookbehind or alternatives of different lengths inside

《Arduino家居安全系统构建实战》——2.2 根据一个单词决定

2.2 根据一个单词决定 有了数据,我们就可以开始分析了.我们的最终目标是区分垃圾短信和非垃圾短信,但是和数字识别器的情况不同,我们还没有一组清晰的特征,唯一的材料是原始文本块--SMS本身.与此同时,人们会猜测文本中有许多信息可供利用.我们只需要找到一个途径,将这些字符串转换成可以使用的特征即可. 2.2.1 以单词作为线索 如果你仔细观察刚刚加载的数据集,就可能会注意到垃圾短信看起来和非垃圾短信有些不同.浏览这些短信,你的眼睛很容易找出某些值得警惕的线索,暗示着某一条短信可能是垃圾短信.举个

初学者的忧伤-哪位大神给自己公司开发过好的异常处理机制,求指点一个好的方案

问题描述 哪位大神给自己公司开发过好的异常处理机制,求指点一个好的方案 我公司现在需要开发一套自己的异常处理机制,就是当异常出现时,在客户端能够返回具体是哪的什么异常,如微信API以及一些大公司做的那种,求指点方案. 解决方案 定义一套有规律异常编码,一看返回编码就知道哪里有异常 解决方案二: 主要就是定义错误码,可以参考微软的GetLastError的做法,就是提供了各种各样的错误码信息,这样用户可以查询了解具体的错误原因. 解决方案三: package com.lz.ctsframework

Length of Last Word

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 cha