LeetCode 58. Length of Last Word(最后一个单词的长度)
来源:互联网 发布:淘宝详情页的制作 编辑:程序博客网 时间:2024/05/17 02:10
原题网址:https://leetcode.com/problems/length-of-last-word/
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
.
方法:两次寻找空格。
public class Solution { public int lengthOfLastWord(String s) { int to = s.length()-1; while (to>=0 && s.charAt(to) == ' ') to --; if (to<0) return 0; int from = to - 1; while (from>=0 && s.charAt(from) != ' ') from --; return to-from; }}
另一种实现:
public class Solution { public int lengthOfLastWord(String s) { int stop = s.length(); for(int i = s.length() - 1; i >= 0; i--) { if (s.charAt(i) != ' ') break; stop = i; } int start = stop; for(int i = start; i > 0; i--) { if (s.charAt(i - 1) == ' ') break; start = i - 1; } return stop - start; }}
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 58.Length of Last Word-最后一个单词长度
- LeetCode 58. Length of Last Word(最后一个单词的长度)
- Leetcode刷题记——58. Length of Last Word(最后一个单词的长度)
- leetcode 58. Length of Last Word(C语言,计算最后一个单词的长度)19
- LeetCode------58. Length of Last Word(字符串中最后一个单词的长度)
- Length of Last Word (最后一个单词的长度) 【leetcode】
- 【LeetCode】- Length of Last Word(最后一个单词的长度)
- LeetCode | Length of Last Word(最后一个单词的长度)
- LeetCode OJ 之 Length of Last Word(求最后一个单词的长度)
- Length of Last Word 最后一个单词的长度
- Length of Last Word 最后一个单词的长度
- 58.Length of Last Word (最后一个单词长度)
- 58. Length of Last Word (计算最后一个单词的长度)
- Lambda表达式类型推断
- Lambda表达式操作引用值,而非对象
- HDFS的搭建
- 蓝牙OBEX剖析(一)
- 线程间协作的二种方式:wait,notify,notifyAll和condition
- LeetCode 58. Length of Last Word(最后一个单词的长度)
- 函数接口
- PYTHON 基础
- 基于Echarts的销售企业经营数据分析-产品组合
- 有很多东西还是有用的
- cf9C. Hexadecimal's Numbers
- 2016/5/21日-另外一种力量
- LeetCode 59. Spiral Matrix II(螺旋矩阵)
- 蓝牙OBEX剖析(二)-- 流程解析