题目描述:

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的更多相关文章

  1. [LeetCode] 58. Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  2. LeetCode 58. Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  3. [leetcode]58. Length of Last Word最后一个词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  4. leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法

    Length of Last Word  Given a string s consists of upper/lower-case alphabets and empty space charact ...

  5. LeetCode: 58. Length of Last Word(Easy)

    1. 原题链接 https://leetcode.com/problems/length-of-last-word/description/ 2. 题目要求 给定一个String类型的字符串,字符串中 ...

  6. Leetcode 58 Length of Last Word 难度:0

    https://leetcode.com/problems/length-of-last-word/ int lengthOfLastWord(char* s) { int ans = 0; int ...

  7. Leetcode 58 Length of Last Word 字符串

    找出最后一个词的长度 class Solution { public: int lengthOfLastWord(string s) { , l = , b = ; while((b = s.find ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 剑指offer--面试题15--相关

    感受:清晰的思路是最重要的!!! 题目:求链表中间节点 ListNode* MidNodeInList(ListNode* pHead) { if(pHead == NULL) return NULL ...

  2. 【锋利的JQuery-学习笔记】输入框提示语-隐藏/显示

    html <div class="search"> <input type="text" id="inputSearch" ...

  3. 《head first java 》读书笔记(五)

    Updated 2014/04/09 P581--P615 如何组织.包装与部署Java程序. 部署的选择 本机: Executable Jar 两者之间的结合: Web Start, RMI app ...

  4. PHP 性能分析与实验(二)——PHP 性能的微观分析

    [编者按]此前,阅读过了很多关于 PHP 性能分析的文章,不过写的都是一条一条的规则,而且,这些规则并没有上下文,也没有明确的实验来体现出这些规则的优势,同时讨论的也侧重于一些语法要点.本文就改变 P ...

  5. Java中LinkedList的remove方法真的耗时O(1)吗?

    这个问题其实来源于Leetcode的一道题目,也就是上一篇日志 LRU Cache.在使用LinkedList超时后,换成ArrayList居然AC了,而问题居然是在于List.remove(Obje ...

  6. Robot framework+python安装使用图解版

    一.安装包 1.Python2.7(一切的基础,切记安装目录不能有中文不能有空格) 1)python2.7:(python环境):python-2.7.msi 2)setuptools(python包 ...

  7. Android中使用HTTP和HttpClient进行通信

    /** * 使用HTTP的Get方式进行数据请求 */ protected void httpGet() { /** * 进行异步请求 */ new AsyncTask<String, Void ...

  8. PKUSC 模拟赛 day2 上午总结

    今天上午考得不是很好,主要还是自己太弱QAQ 开场第一题给的图和题意不符,搞了半天才知道原来是走日字形的 然后BFS即可 #include<cstdio> #include<cstr ...

  9. java代码实现自动登录功能

    通常我们登录某网站,会有选择保存几天,或者是几个星期不用登录,之后输入该网站地址无需登录直接进入主页面,那么这就叫做自动登录,怎么实现呢,下面我以一个小例子来演示一下 登录页面:login.jsp & ...

  10. jdbc的通讯录CRUD

    基于JDBC的通讯录练手:项目以MVC模式开发,包名:cn.itcast.txl.domain;cn.itcast.txl.dao;cn.itcast.txl.dao.impl;cn.itcast.t ...