题目:

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.

思路:

  • 题意:给定一个字符串,每个单词用“ ”隔开,求最后一个单词的长度,如果没有返回0
  • 利用String.split(” “),分开成String数组,返回最后衣字符串的长度,考虑输入的字符串s为null,s= “ ”,s=“hello ”(以空格结尾)
  • -

代码:

public class Solution {
    public int lengthOfLastWord(String s) {
        if(s == null){
            return 0;
        }
        String[] ss = s.split(" ");
        int n = ss.length;
        if(n < 1){
            return 0;
        }
        String a = ss[n-1];
        if(a == null){
            return 0;
        }
        char[] aa = a.toCharArray();
        return aa.length;
    }
}

LeetCode(48)-Length of Last Word的更多相关文章

  1. leetcode 题解: Length of Last Word

    leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...

  2. [LeetCode] 58. Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  3. 【leetcode】Length of Last Word

    题目简述 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  4. Java for LeetCode 058 Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  5. LeetCode 58. Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  6. Java [Leetcode 58]Length of Last Word

    题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

  7. [leetcode]58. Length of Last Word最后一个词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  8. 【LeetCode】- Length of Last Word(最后一个单词的长度)

    [ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...

  9. [leetcode] 18. Length of Last Word

    这个题目很简单,给一个字符串,然后返回最后一个单词的长度就行.题目如下: Given a string s consists of upper/lower-case alphabets and emp ...

随机推荐

  1. SQL基本函数

    字符型函数 函数名称 描述 LOWER 将特定的字符串转化为小写,只影响字母字符串. UPPER 将整个字符串转换成大写,只影响字母字符串. INITCAP 将字符串中每一个单词的第一个字母转换为大写 ...

  2. ActiveMQ安装配置及实例

    本文可作为吴水成老师,dubbo课程第21节的学习笔记. ActiveMQ的介绍及功能 参考百度 ActiveMQ的下载 https://activemq.apache.org/activemq-51 ...

  3. 多层界面之间显示与隐藏tabBar

    IOS中多层界面之间显示与隐藏tabBar? 在做项目的时候,遇到了一个难题,使用hidesBottomWhenPushed=YES属性设置,可以让本级界面及其以后界面都隐藏,但是根据项目 需求,在第 ...

  4. 认证模式之Basic模式

    HTTP协议规范中有两种认证方式,一种是Basic认证,另外一种是Digest认证,这两种方式都属于无状态认证方式,所谓无状态即服务端都不会在会话中记录相关信息,客户端每次访问都需要将用户名和密码放置 ...

  5. linux 下停止java jar包 shell

    linux 下停止java jar包 shell http://injavawetrust.iteye.com #!/bin/sh APP_HOME=/home/ap/injavawetrust/ba ...

  6. leetcode 169 Majority Element 冰山查询

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. (NO.00001)iOS游戏SpeedBoy Lite成形记(二十五)

    每次压赌要打开弹出菜单还是让人略觉不爽,下面我们再添加一个随机押注的按钮:自动随机选择选手和下赌金额. 打开spriteBuilder,修改GameInterface.ccb的界面,在Run按钮旁边添 ...

  8. Java进阶(七)Java加密技术之非对称加密算法RSA

    Java加密技术(四)--非对称加密算法RSA 非对称加密算法--RSA 基本概念 非对称加密算法是一种密钥的保密方法. 非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(priv ...

  9. Mahout系列之-----相似度

    Mahout推荐系统中有许多相似度实现,这些组件实现了计算不能User之间或Item之间的相似度.对于数据量以及数据类型不同的数据源,需要不同的相似度计算方法来提高推荐性能,在mahout提供了大量用 ...

  10. C#之面向对象

    话说三国时期,曹操带领百万大军攻打东吴,大军在长江赤壁驻扎,军船连成一片,眼看就要灭掉东吴,统一天下,曹操大悦,于是晏众文武,在酒席间,曹操诗兴大发,不觉吟道:"喝酒唱歌,人生真爽" ...