LeetCode(48)-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 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的更多相关文章
- leetcode 题解: Length of Last Word
leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...
- [LeetCode] 58. Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【leetcode】Length of Last Word
题目简述 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- 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 ...
- LeetCode 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Java [Leetcode 58]Length of Last Word
题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- [leetcode]58. Length of Last Word最后一个词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【LeetCode】- Length of Last Word(最后一个单词的长度)
[ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...
- [leetcode] 18. Length of Last Word
这个题目很简单,给一个字符串,然后返回最后一个单词的长度就行.题目如下: Given a string s consists of upper/lower-case alphabets and emp ...
随机推荐
- COM原理与实现之二: 聚合
COM原理与实现之二: 聚合 C++没有同聚合等价的特性.聚合实际上是继承性的一种动态形式.而C++的继承总是静态的,是实现继承.COM是接口继承,通过聚合接口,可以做成动态配置. 研究COM,主要是 ...
- FFmpeg源代码简单分析:avcodec_close()
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- ExpandableListView简单应用及listview模拟ExpandableListView
首先我们还是来看一些案例,还是拿搜狐新闻客户端,因为我天天上下班没事爱看这个东东,上班又没时间看新闻,上下班路途之余浏览下新闻打发时间嘛. 看这个效果挺棒吧,其实实现起来也不难,我 ...
- 【一天一道LeetCode】#344. Reverse String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- Android项目-高考作文-使用ORMLite抽象公共的Dao层
1, 定义统一的Dao接口 public interface IDao<T> { public abstract T getSingleById(int id); public abstr ...
- 海量数据挖掘MMDS week6: 决策树Decision Trees
http://blog.csdn.net/pipisorry/article/details/49445465 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...
- Uva - 11853 - Paintball
先判断是否有解,从上到下dfs判断连通性,如果有从顶部到底部连通图,则无解.再判断最北的进出位置,从上边界开始遍历,沿途检查与边界相交的圆.这些圆的左边界的交点中最靠南边的一个就是所有的最北进入位置, ...
- [WinForm]dataGridView自定动态设定序号列框
你可以在数据绑定或者行数有很大变化时测量一下DataGridView最大行数的行数的宽度然后在绘制代码如下 SolidBrush solidBrush; StringFormat stringForm ...
- H5的学习之旅-H5的实体(14)
H5有些关键字比如<等等是显示不出来的,这时候,就需要用实体来表示,实体我理解就是最初的编码 代码实例 <!DOCTYPE html> <html lang="en& ...
- 高德地图SDK使用经验
下文说的是高德地图 Android SDK版本,详细版本如下: 2D地图:v2.3.1 定位:v1.3.0 导航:v1.1.1 发现的问题如下,其中一些疑是地图BUG,一些是需要你自己小心的地方: 1 ...