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.

这道题比较简单,注意考虑字符串最后的空格以及全为空格的字符串的这几种特殊情况。

代码如下:

 class Solution {
public:
int lengthOfLastWord(string s) {
if(s.length() == )
{
return ;
}
int n = ;
int l = s.length();
for(int i = l-; i >= ; i--)
{
if(n == && s[i] == ' ')
{
continue;
}
if(s[i] != ' ')
{
n ++;
}
else
{
break;
}
}
return n;
}
};

leetcode 58的更多相关文章

  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 难度:0

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

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

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

  4. Leetcode(58)题解:Length of Last Word

    https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...

  5. Java实现 LeetCode 58 最后一个单词的长度

    58. 最后一个单词的长度 给定一个仅包含大小写字母和空格 ' ' 的字符串 s,返回其最后一个单词的长度. 如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词. 如果不存在最后一个单词, ...

  6. Leetcode 58 Length of Last Word 字符串

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

  7. LeetCode 58. Length of Last Word

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

  8. Java [Leetcode 58]Length of Last Word

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

  9. LeetCode 58 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

随机推荐

  1. GridView--scroolview嵌套listview和gridview

    我们在真实项目中通常会遇到ListView或者GridView嵌套在ScrollView中问题.但是做的时候会发现,一旦两者进行嵌套,即会发生冲突.得不到我们希望的效果.由于ListView和Grid ...

  2. JAVA 处理程序异常,(try、catch、finally),(thorws)

    一.try语句: try{//这里写可能出现异常的程序} catch(Exception e){//这里写如果出现异常怎么处理的程序} 二.throws语句 语法:函数方法()  throws Exc ...

  3. linux命令行下导出导入.sql文件

    一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径):1.导出数据和表结构(以管理员身份运行): ------------------------------------ ...

  4. .NET 验证码/验证图片

    /// <summary> /// 验证码类 /// </summary> public class Rand { #region 生成随机数字 /// <summary ...

  5. java 反射技术

    什么是反射?反射就是将字节码中的各种成分映射到相应的java类中来,java反射技术自JDK1.1以来就出现了,目前大多数流行的框架都采用了这种技术,可见其重要性,这篇文章将详细介绍我对java反射技 ...

  6. 一些代码 I (斐波那契、for...else...、try和return、classmethod、统计个数)

    1. 斐波那契 from itertools import islice def fib(): a, b = 0, 1 while True: yield a a, b = b, a+b print ...

  7. abap case when 例子

    DATA: gv_1 TYPE c. DATA: gv_2 TYPE i. gv_2 = 60. IF gv_2 >= 0 AND gv_2 < 60 . gv_1 = 'A'. ELSE ...

  8. Arch时间校准

    sudo ln -s /usr/share/zoneinfo/UTC /etc/localtime sudo hwclock --systohc --utc sudo timedatectl set- ...

  9. Redis服务器配置

    Redis 服务器提供了一些配置选项(configuration option),通过修改这些选项的值,可以改变选项对应功能的行为. 比如:介绍 SELECT 命令时曾经说过,Redis 服务器默认会 ...

  10. 三种硬件平台运行Laxcus大数据系统的表现

    从2.0版本开始,Laxcus大数据管理系统开始支持POWERPC.X86.ARM三种平台.其中X86和ARM又分为32位和64位两种,POWERPC是纯64位,所以实际上共有五种平台,操作系统统一使 ...