Length of Last Word
来源:互联网 发布:冒险岛boss数据 编辑:程序博客网 时间:2024/05/16 18:48
Q:
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
.
Solution:
public class Solution { public int lengthOfLastWord(String s) { int count = 0; boolean last = false; for (int i = s.length()-1; i >= 0; i--) { if (s.charAt(i) != ' ' && !last) { last = true; count++; continue; } if (s.charAt(i) != ' ' && last) count++; if (s.charAt(i) == ' ' && last) break; } return count; }}
0 0
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- [Leetcode] Convert Sorted List to Binary Search Tree Solution
- 三星 Samsung Galaxy Note/I9220/N7000 ZCLPP 4.0.4 国行ROM优化版
- Java程序员的JavaScript学习笔记(12——jQuery-扩展选择器)
- Binary Search Tree
- OS X10.10下HomeBrew的安装提示
- Length of Last Word
- 第一个周计划
- 修复PHP支持的标准JSON数据格式
- 请问去问问企鹅屋企恶趣味
- javascript新特性let,function*,yield,promise
- session_start()导致history.go(-1)返回时无法保存表单数据的解决方法
- 108 盧守愼 蘇齋
- 109 鄭惟吉 林塘
- 110 李俊民 新庵