[LC] 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' '
, return the length of last word (last word means the last appearing word if we loop from left to right) in the string.
If the last word does not exist, return 0.
Note: A word is defined as a maximal substring consisting of non-space characters only.
Example:
Input: "Hello World"
Output: 5
class Solution {
public int lengthOfLastWord(String s) {
if (s == null || s.length() == 0) {
return 0;
}
String[] strArr = s.split("\\s+");
// for case " "
return strArr.length == 0 ? 0: strArr[strArr.length - 1].length();
}
}
[LC] 58. Length of Last Word的更多相关文章
- LeetCode练题——58. Length of Last Word
1.题目 58. Length of Last Word——Easy Given a string s consists of upper/lower-case alphabets and empty ...
- 58. Length of Last Word最后一个单词的长度
[抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误 ...
- [LeetCode] 58. Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- LeetCode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【LeetCode】58 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 58. Length of Last Word
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- Java [Leetcode 58]Length of Last Word
题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- 58. Length of Last Word【leetcode】
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- <LeetCode OJ> 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
随机推荐
- spring boot打成可执行jar
第一步 添加 <packaging>jar</packaging> 第二步添加 <build> <plugins> <plugin> < ...
- UML-活动图及其建模
1.目标:UML活动图标示法. 2.定义:一个UML活动图标示一个过程中的多个顺序活动和并行活动.这些活动有助于对业务过程.工作流.数据流和复杂算法进行建模. 3.作用:既能表示控制流又能标示数据流. ...
- A - Color the ball HDU - 1556 (差分数组+前缀和)
思路等引自博客 https://blog.csdn.net/johnwayne0317/article/details/84928568 对数组a[7]: a[0]=1; = d[0] a[1]=1; ...
- 为什么常用 Map<> map = new HashMap()
在初学Java的时候,经常能看到教材上的写法,使用了接口Map来引用一个map,而不是它的具体实现,那么这样做的好处是什么呢? <Effective Java>第52条:通过接口引用对象 ...
- 常用DIV+CSS命名大全集合
一.命名规则说明: - TOP 1).所有的命名最好都小写 2).属性的值一定要用双引号("")括起来,且一定要有值如class="divcss5",i ...
- PDO是一个“数据库访问抽象层”
PDO是一个"数据库访问抽象层",作用是统一各种数据库的访问接口,与mysql和mysqli的函数库相比,PDO让跨数据库的使用更具有亲和力:与ADODB和MDB2相比,PDO更高 ...
- bugs开发异常解决方法
1. 前提 第一层:遇到异常首先必须告诉自己,冷静,不要慌.(一看到Bug就心慌,那么就不能释放必杀技) 2. 入门级 第二层:遇到Bug,第一潜意识看输出异常的信息的(控制台输出 ...
- 备战秋招——C++知识点
1.字符串的末尾'\0'也算一个字符,一个字节. 2.使用库函数strcpy(a,b)进行拷贝b->a操作,strcpy会从源地址一直往后拷贝,直到遇到'\0'为止.所以拷贝的长度是不定的.如果 ...
- 修改自己的centos输入法
当自己的centos连上网时,就可以修改自己的输入法了 http://jingyan.baidu.com/album/da1091fb3e7f8a027849d681.html?picindex=2
- 第一行代码新闻例子报错 Unable to start activity ComponentInfo 原因
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.timemanager.jason.fragmentbestpractice, PID: 56 ...