题目:

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. Spring开发环境搭建教程

    Spring开发环境搭建 JDK7以上版本 eclispe for j2ee 4.0以上版本 Spring frameWorks 3.0以上版本 至于前两个我们就不介绍,直接百度就可以了,对于Spri ...

  2. Android开发学习之路--React-Native之初体验

      近段时间业余在学node.js,租了个阿里云准备搭建后端,想用node.js,偶尔得知react-native可以在不同平台跑,js在iOS和android上都可以运行ok,今天就简单学习下rea ...

  3. Cocos2D游戏项目CCTableView在Xcode7.2下的无法滚动问题

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 一个RPG游戏转换到Xcode7.2下发现一个问题,原来可以上 ...

  4. (一)php的基本知识和一些注意点

    注意:任何程序,包括php,在运行时都在内存中进行,php代码需要被读取到内存中才能执行. [php的运行方式] 1.通过服务器(例如apache)调用. 2.通过命令行调用(不需要服务器参与,因为没 ...

  5. Linux Debugging(四): 使用GDB来理解C++ 对象的内存布局(多重继承,虚继承)

    前一段时间再次拜读<Inside the C++ Object Model> 深入探索C++对象模型,有了进一步的理解,因此我也写了四篇博文算是读书笔记: Program Transfor ...

  6. java Domj4读取xml文件加强训练案例

    需求:给出一段xml文件.要求按照鸳鸯输出. xml文件代码如下: <?xml version="1.0" encoding="utf-8"?> & ...

  7. 客户地点分配多OU

    DECLARE l_num_user_id NUMBER; l_num_appl_id NUMBER; l_num_resp_id NUMBER; cust_account_rec_type hz_c ...

  8. sublimeText3使用记录

    sublime确实是神器,最近学习了一下,做个记录 1.下载 http://www.sublimetext.com/3 选择对应的版本安装即可(我的是win10 64位机) 2.个人配置 2.1.默认 ...

  9. Android轶事之View要去大保健?View大小自己决定?

    -"爹,我要吃糖" -"好哒儿子" -"爹,我要吃包包" - "好哒儿子" - "爹,我要吃串串" ...

  10. JQuery实战总结二 横向纵向菜单下拉效果图

    记得以前在浏览了大多数网站的上面发现很多下拉的导航栏,觉得特别好玩,毕竟咱们是学习编程的嘛,对这下拉的效果还是挺感兴趣的,这种淡入淡出,随着鼠标移动的位置不同.有无等而出现不同的效果,给用户以神美感. ...