【leetcode】【58】Length of Last Word
来源:互联网 发布:广联达算量软件下载 编辑:程序博客网 时间:2024/06/06 03:26
一、问题描述
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
.
二、问题分析
非常简单的一道题,只需要将原字符串trim一下,然后从后往前遍历即可,非空字符count++,碰到空字符return即可;也可以采用split方法,直接返回最后一个string的长度即可。
三、Java AC代码
public int lengthOfLastWord(String s) { if(null == s||s.trim().length()==0){ return 0; } String tmp = s.trim(); int count = 0; for(int i=tmp.length()-1;i>=0;i--){ if(tmp.charAt(i)!=' '){ count++; }else{ return count; } } return count; }
public int lengthOfLastWord(String s) { if(null == s||s.trim().equals("")){ return 0; } String[] splitString = s.trim().split(" "); int len = splitString[splitString.length-1].length();return len; }
0 0
- [leetcode 58] Length of Last Word
- leetcode-58 Length of Last Word
- Leetcode NO.58 Length of Last Word
- LeetCode #58 length of Last Word
- [LeetCode 58]Length of Last Word
- leetcode || 58、Length of Last Word
- LeetCode 58 Length of Last Word
- [Leetcode 58, easy] Length of Last Word
- Length of Last Word (leetCode 58)
- leetcode[58]:Length of Last Word
- 【leetcode c++】58 Length of Last Word
- leetcode 58:Length of Last Word
- LeetCode---(58)Length of Last Word
- leetcode-58-Length of Last Word
- Leetcode# 58 Length of Last Word
- [leetcode-58]Length of Last Word(c)
- leetcode 58: Length of Last Word
- leetCode #58 Length of Last Word
- 关于android.mk
- Java Resources是什么
- 多线程3-NSOperation
- 归并排序
- ios开发--Mac 10.10安装破解版Navicat Premium 11.0.16.dmg
- 【leetcode】【58】Length of Last Word
- 图像验证码识别(七)——字符分割
- session令牌防止表单重复提交
- x86—EFLAGS寄存器详解
- contos安装laravel
- 如何取消tortoiseSVN的管理
- 第五届_李白打酒
- 51NOD算法马拉松11 B君的竞技场
- 图像验证码识别(八)——字符归一化