LeetCode OJ: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.
求最后一个非空单词的长度而已,没什么注意点,直接看代码,理解题目意思就可以了:
class Solution {
public:
int lengthOfLastWord(string s) {
int len = s.length();
if(len == ) return ;
while(s[len - ] == ' ')
if(len - >= )
len--;
else
return ;
len--;
int lenCount = ;
while(s[len] != ' ' && len >= ){
lenCount++, len--;
}
return lenCount;
}
};
java版本的代码如下所示,倒过来读很容易了:
public class Solution {
public int lengthOfLastWord(String s) {
int i = s.length() - 1;
while(i >= 0 && s.charAt(i) == ' ')
i--;
int end = i+1;
while(i >= 0 && s.charAt(i) != ' ')
i--;
int start = i+1;
return end - start;
}
}
LeetCode OJ:Length of Last Word(最后一个词的长度)的更多相关文章
- [leetcode]58. Length of Last Word最后一个词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [LeetCode] 58. Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [Leetcode] Length of last word 最后一个单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...
- lintcode:Length of Last Word 最后一个单词的长度
题目: 最后一个单词的长度 给定一个字符串, 包含大小写字母.空格' ',请返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 样例 给定 s = "Hello World& ...
- 058 Length of Last Word 最后一个单词的长度
给定一个字符串, 包含大小写字母.空格 ' ',请返回其最后一个单词的长度.如果不存在最后一个单词,请返回 0 .注意事项:一个单词的界定是,由字母组成,但不包含任何的空格.案例:输入: " ...
- 58. Length of Last Word最后一个单词的长度
[抄题]: [暴力解法]: 时间分析: 空间分析: [优化后]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: "b a " 最后一位是空格,可能误 ...
- [LeetCode] Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Leetcode 58 Length of Last Word 字符串
找出最后一个词的长度 class Solution { public: int lengthOfLastWord(string s) { , l = , b = ; while((b = s.find ...
- 【LeetCode】- Length of Last Word(最后一个单词的长度)
[ 问题: ] Given a string s consists of upper/lower-case alphabets and empty space characters ' ', retu ...
随机推荐
- Kafka的架构
1.Kafka整体架构 一个典型的Kafka集群中包含若干producer(可以是web前端产生的page view,或者是服务器日志,系统CPU.memory等),若干broker(Kafka ...
- node+npm安裝配置
控制臺輸入node 根據提示安裝 sudo apt-get install -g npm配置淘寶源 npm config set registry https://registry.npm.tao ...
- 反射,hashlib模块,正则匹配,冒泡,选择,插入排序
一.反射(自省) 首先通过一个例子来看一下本文中可能用到的对象和相关概念. import sys # 模块,sys指向这个模块对象import inspectdef foo(): pass # 函数, ...
- comet4j开发指南
http://blog.csdn.net/majian_1987/article/details/8489738 好多朋友反映,因排版问题,文章部分边缘内容无法查看.尝试过排版,但仍无法显示正常,所以 ...
- 交叉编译Mesa,X11lib,Qt opengl
记录Mesa配置文件如下: Mesa版本:Mesa-10.2.3 CC=/usr/local/arm-4.8.1/bin/arm-none-linux-gnueabi-gcc CXX=/usr/loc ...
- Go 内置库 IO interface
基本的 IO 接口 io 包为 I/O 原语提供了基本的接口.它主要包装了这些原语的已有实现. 由于这些接口和原语以不同的实现包装了低级操作,因此除非另行通知,否则客户端不应假定它们对于并行执行是安全 ...
- POI实现数据的导入
1.POI技术的概述? POI技术:apache POI是可以对微软office文档进行读和写的工具. l HSSF:操作97格式的excel,扩展名:.xls 纯二进制,最大行数65535. l X ...
- 使用Shell脚本查找程序对应的进程ID,并杀死进程
#!/bin/sh NAME='shell.php' echo $NAME ID=`ps -ef | grep "$NAME" | grep -v "$0" | ...
- java基础9(IO流)-File类
File类 File:文件和目录路径名的抽象表示形式.即java中把文件或者目录都封装成File对象 代码练习1 import java.io.File; public class FileDemo1 ...
- 字符在内存中最终的表示形式是什么?是某种字符编码还是码位(Code Point)?
字符在内存中最终的表示形式是什么?是某种字符编码还是码位(Code Point)? 根据我的了解,编码中有三个核心概念:1. 字符集(Character Set),可以说是一个抽象概念,字符的合集2. ...