Java [Leetcode 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.
解题思路:
先定位到字符串从后往前数第一个不为空字符的位置,然后进行判断。
从第一个位置向后读取,每遇到空字符则将计数器清零,然后下次继续计数。
代码如下:
public class Solution {
public int lengthOfLastWord(String s) {
int curLen = 0, len = 0, end = -1;
if(s == null || (len = s.length()) == 0)
return 0;
for(int i = len - 1; i >= 0; i--){
if(s.charAt(i) != ' ') {
end = i;
break;
}
}
for(int i = 0; i <= end; i++){
if(s.charAt(i) != ' ')
curLen++;
else {
curLen = 0;
}
}
return curLen;
}
}
Java [Leetcode 58]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]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 (最后单词的长度) 解题思路和方法
Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space charact ...
- LeetCode: 58. Length of Last Word(Easy)
1. 原题链接 https://leetcode.com/problems/length-of-last-word/description/ 2. 题目要求 给定一个String类型的字符串,字符串中 ...
- Leetcode 58 Length of Last Word 难度:0
https://leetcode.com/problems/length-of-last-word/ int lengthOfLastWord(char* s) { int ans = 0; int ...
- Leetcode 58 Length of Last Word 字符串
找出最后一个词的长度 class Solution { public: int lengthOfLastWord(string s) { , l = , b = ; while((b = s.find ...
- 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 ...
- Java for LeetCode 058 Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
随机推荐
- 【BZOJ】【3757】苹果树
树分块 orz HZWER http://hzwer.com/5259.html 不知为何我原本写的倍增求LCA给WA了……学习了HZWER的倍增新姿势- 树上分块的转移看vfk博客的讲解吧……(其实 ...
- 【BZOJ】【1013】【JSOI2008】球形空间产生器sphere
高斯消元 高斯消元模板题 /************************************************************** Problem: 1013 User: Tun ...
- oracle 删除表、数据
truncate ddl语句,删除表中数据,速度要比delete快:且所有自增字段重新开始计数:删除数据保留表结构:删除的数据不进入rollback segment,无法恢复.例: trunc ...
- [设计模式] 10 外观模式 facade
外观模式应该是用的很多的一种模式,特别是当一个系统很复杂时,系统提供给客户的是一个简单的对外接口,而把里面复杂的结构都封装了起来.客户只需使用这些简单接口就能使用这个系统,而不需要关注内部复杂的结构. ...
- .net中的认证(authentication)与授权(authorization)
“认证”与“授权”是几乎所有系统中都会涉及的概念,通俗点讲: 1.认证(authentication) 就是 "判断用户有没有登录?",好比windows系统,没登录就无法使用(不 ...
- 关于C#中timer类
·关于C#中timer类 在C#里关于定时器类就有3个 1.定义在System.Windows.Forms里 2.定义在System.Threading.Timer类里 3.定义在System.Tim ...
- 使用post()方法以POST方式从服务器发送数据
使用post()方法以POST方式从服务器发送数据 与get()方法相比,post()方法多用于以POST方式向服务器发送数据,服务器接收到数据之后,进行处理,并将处理结果返回页面,调用格式如下: $ ...
- 关于JS中变量的作用域-实例
先看问题,如下,自己运行一下吧! if (!('_qyzA' in window)) { var _qyzA = 1; } alert(_qyzA);//undefined 分析:首先,所有的全局变量 ...
- 一个java的DES加解密类转换成C#
原文:一个java的DES加解密类转换成C# 一个java的des加密解密代码如下: //package com.visionsky.util; import java.security.*; //i ...
- RESTful WebService入门
RESTful WebService入门 RESTful WebService是比基于SOAP消息的WebService简单的多的一种轻量级Web服务,RESTful WebService是没有状 ...