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.

思路:题目非常easy,没什么好说的,唯一要注意的就是后面的结尾可能非常多空格。

详细代码例如以下:

public class Solution {
public int lengthOfLastWord(String s) {
int len = 0;
boolean isEmpty = false;//从后往前数,直到不是空格
for(int i = s.length()-1; i > -1; i--){
if(s.charAt(i) != ' '){
len++;
isEmpty = true;
}else if(isEmpty){//倒数第二个空格出现
return len;
}
}
return len;
}
}

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(Easy)

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

  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. Java [Leetcode 58]Length of Last Word

    题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return ...

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

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

  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 41.First Missing Positive (第一个丢失的正数) 解题思路和方法

    First Missing Positive  Given an unsorted integer array, find the first missing positive integer. Fo ...

  9. leetCode 74.Search a 2D Matrix(搜索二维矩阵) 解题思路和方法

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

随机推荐

  1. Java实现简单的socket通信

    今天学习了一下java如何实现socket通信,感觉难点反而是在io上,因为java对socket封装已经很完善了. 今天代码花了整个晚上调试,主要原因是io的flush问题和命令行下如何运行具有pa ...

  2. C++ Essentials 之 lower_bound 和 upper_bound 的比较函数格式不同

    第一次注意到这个问题. cppreference 上的条目: lower_bound upper_bound C++17 草案 N4659 lower_bound template<class ...

  3. set和dict

    dict属于mapping类型 from collections.abc import Mapping,MutableMapping from collections.abc import __all ...

  4. git 使用报错记录

    错误一:git fatal: unable to write new index file主要原因就是服务器磁盘空间不够导致的,增加服务器空间就OK了在百度上面搜索没得到什么有效信息,在gooogle ...

  5. 2721: [Violet 5]樱花

    2721: [Violet 5]樱花 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 547  Solved: 322[Submit][Status][D ...

  6. 页面get post等查看

    原文发布时间为:2010-03-08 -- 来源于本人的百度文章 [由搬家工具导入] http://www.fiddler2.com/Fiddler2/firstrun.asp

  7. 练习题 求a[i]到a[j]累积和为最大的部分

    原文发布时间为:2009-03-09 -- 来源于本人的百度文章 [由搬家工具导入] 1、有一个数组a[n],里面的数只有两种:-1或1。i,j是两个整数,假设0<=i<=j<=n- ...

  8. .net web api返回结果为json

    web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面为大家介绍几种不错的方法 web api写api接口时默认返回的是把你的对象序列化后以 ...

  9. git使用代理clone加速

    不设置代理10kb/s不到....,设置后,500kb/s左右跑- 开shadowsocks,代理127.0.0.1:1080 编写一个脚本 /YOUR PATH/gitproxy.sh #!/bin ...

  10. Linux定时关机

    sudo shutdown -h +120 :两小时后关机sudo shutdown -h 23:00 :表示在23点定时关机 一.shutdown命令关机 各参数功能: -c 取消前一个shutdo ...