LeetCode:Length of Last Word
来源:互联网 发布:小众品牌知乎 编辑:程序博客网 时间:2024/05/01 17:45
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
.
基本编程题。
package leetcode;public class LengthofLastWord {public static void main(String[] args) {System.out.println(new LengthofLastWord().lengthOfLastWord(" a "));}public int lengthOfLastWord(String s) { int end = s.length() - 1; while (end >= 0) { if (s.charAt(end) == ' ') { end--; } else { break; } } int index = end; while (index >= 0) { if (s.charAt(index) != ' ') { index--; } else { break; } } return end - index; }}
- 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
- C++中静态类型和动态类型
- 使用JDK自带jvisualvm监控tomcat
- vc++6.0软件出错 (一)
- null和""的区别
- 单片机IO引脚驱动能力与上拉电阻
- LeetCode:Length of Last Word
- 通向架构师的道路(第三天)之apache性能调优
- open和fopen的区别:
- jQuery 自学笔记—5 事件方法
- ADF 三种改变VO查询条件方式
- FC统计图需要属性 总结
- free ebooks: http://stackoverflow.com/questions/194812/list-of-freely-available-programming-books
- 通向架构师的道路(第四天)之Tomcat性能调优-让小猫飞奔
- 指针函数与函数指针的区别