Leetcode: Length of Last Word
来源:互联网 发布:获取基址的文本源码 编辑:程序博客网 时间:2024/06/18 12:17
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;
public static int lengthOfLastWord(String s) { if(s=="") return 0; int result=0; s=s.trim(); for(int i=s.length()-1;i>=0;i--){ if(s.charAt(i)!=' ') result++ ; else break; } return result; }
注意:
考虑“a ” 实际应该返回1.
所以先去掉字符串两边的空格,然后再计算。
0 0
- LeetCode: Length of Last Word
- LeetCode Length of Last Word
- LeetCode : Length of Last Word
- [Leetcode] Length of Last Word
- [LeetCode] Length of Last Word
- [LeetCode]Length of Last Word
- [leetcode]Length of Last Word
- Leetcode:Length of Last Word
- LeetCode-Length of Last Word
- [LeetCode] Length of Last Word
- [leetcode] Length of Last Word
- leetcode length of last word
- LeetCode - Length of Last Word
- leetcode:Length of Last Word
- LeetCode:Length of Last Word
- LeetCode | Length of Last Word
- LeetCode - Length of Last Word
- Leetcode: Length of Last Word
- MVVM架构~knockoutjs系列之从Knockout.Validation.js源码中学习它的用法
- Leetcode:Merge Two Sorted Lists
- Python编程基础之十四正则表达式
- Flex中ArrayCollection的复制(克隆)
- IOS设置UIView的边框为圆角
- Leetcode: Length of Last Word
- android如何从一个应用跳转到另一个页面
- c++_栈实现进制转换
- Actionscript Number保留两位小数
- JAVA --- 易错点整理
- 把struts2的struts.xml配置文件分解成多个配置文件
- 项目中用flex 问题
- atitit.研发管理--标准化流程总结---java开发环境与项目部署环境的搭建工具包总结
- NYOj 63 小猴子下落