58. 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.
链接: http://leetcode.com/problems/length-of-last-word/
题解:
从后向前遍历。
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
public int lengthOfLastWord(String s) {
if(s == null || s.length() == 0)
return 0;
int i = s.length() - 1, count = 0;
s = s.toLowerCase();
while(i >= 0 && (s.charAt(i) > 'z' || s.charAt(i) < 'a') )
i --;
while(i >= 0 && s.charAt(i) != ' '){
count ++;
i --;
}
return count;
}
}
Update:
看一看以前的代码...完全不认识了...
public class Solution {
public int lengthOfLastWord(String s) {
int count = 0;
if(s == null || s.length() == 0)
return count;
for(int i = s.length() - 1; i >= 0; i--) {
if(s.charAt(i) == ' ') {
if(count == 0)
continue;
else
break;
}
count++;
}
return count;
}
}
二刷:
还是跟一刷一样,从后向前遍历。
Java:
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
public int lengthOfLastWord(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int count = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) != ' ') {
count++;
} else if (count > 0) {
break;
}
}
return count;
}
}
三刷:
这道题属于基本很少能给人留下印象的题。从后向前遍历, 假如s.charAt(i)不为空格的时候,我们增加count, 否则,假如count = 0,我们还没找到单词,继续查找。 假如count > 0,说明我们已经找到并且计算了最后一个单词,返回count。 遍历数组以后也返回count,因为有可能字符串里就一个单词,没空格。 这分析写得比较糙...
Java:
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
public int lengthOfLastWord(String s) {
if (s == null) {
return 0;
}
int count = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) != ' ') {
count++;
} else if (count > 0) {
return count;
}
}
return count;
}
}
58. Length of Last Word的更多相关文章
- LeetCode练题——58. Length of Last Word
1.题目 58. Length of Last Word——Easy Given a string s consists of upper/lower-case alphabets and empty ...
- [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】58 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- Java [Leetcode 58]Length of Last Word
题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...
- 58. Length of Last Word【leetcode】
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- <LeetCode OJ> 58. Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 58. Length of Last Word(easy, 字符串问题)
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- 【一天一道LeetCode】#58. Length of Last Word
一天一道LeetCode系列 (一)题目 Given a string s consists of upper/lower-case alphabets and empty space charact ...
随机推荐
- oracle创建用户,修改用户,删除用户等关于用户的
--直接修改底层表 USER$ 更换用户名 1.windows 平台下运行 cmd 2.sqlplus /nolog 3.SQL> conn SYSTEM/123@ORCL as sysdba ...
- sqlchemy - day3
session 直接上代码,创建表结构,初始化部分数据. from sqlalchemy import create_engine engine = create_engine(" ...
- posix thread 浅谈
用Posix thread进行多线程设计,就不怕跨平台了,因为很多OS都兼容Posix thread,如Linux/Windows等,甚至嵌入式系统上(如rt-thread)都支持posix thre ...
- Mysql 创建数据库表(删除,删除,插入)
MySQL 创建数据表 创建MySQL数据表需要以下信息: 表名 表字段名 定义每个表字段 语法 以下为创建MySQL数据表的SQL通用语法: CREATE TABLE table_name (col ...
- 【狼窝乀野狼】Serializer妙手回春
在我们很多程序中,需要将数据保存到本地,以便于下次打开还能看到原始数据.例如我们Xmind思维导图,例如我们的Power Designer等等,都是有保存一个隶属于自己的工程文件,那么今天我要说的就是 ...
- 静态wenb开发,动态web开发
- 使用libuv实现生产者和消费者模式
生产者和消费者模式(Consumer + Producer model) 用于把耗时操作(生产线程),分配给一个或者多个额外线程执行(消费线程),从而提高生产线程的响应速度(并发能力) 定义 type ...
- 管道Pipe
管道Pipe java.nio.channels包中含有一个名为Pipe(管道)的类.广义上讲,管道就是一个用来在两个实体之间单向传输数据的导管.管道的概念对于Unix(和类Unix)操作系统的用户来 ...
- linux 下载并安装Memcache服务器端
1.下载并安装Memcache服务器端 服务器端主要是安装memcache服务器端. 下载:http://www.danga.com/memcached/dist/memcached-1.2.2.ta ...
- 混乱的url编码||URL编码解码问题
转载自:http://www.ruanyifeng.com/blog/2010/02/url_encoding.html 一.问题的由来. url就是网址,只要上网就一定会用到. 一般来说,URL只能 ...