LeetCode(48)-Length of Last Word
2016-04-12 10:37
316 查看
题目:
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; } }
相关文章推荐
- 为什么钱钟书、秦晖成不了大思想家 | 苏小和
- Test 水杯问题
- Qt tab标签页
- WPF 内建面板学习总结(一)
- UART时序分析
- AOP之通知顺序
- socket通信
- RxJava 备注
- centos7更改机器名后oracle无法使用
- iOS新建项目架构规范
- win10下设置虚拟机配置hadoop-yarn单机伪分布环境
- 沉睡
- mysql知识积累
- struts.xml文件属性介绍
- iOS 打包上传 Version 和 Build 的意义
- Python collections deque - double-ended queue ()
- 收藏的css
- Selenium2 Python 自动化测试实战学习笔记(二)
- weightSum属性和layout_weight属性的用法和详解
- c++全局变量的正确声明