一天一道LeetCode系列

(一)题目

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() == 0) return 0;

        int count = 0;

        int i = s.length()-1;

        while(s[i]==' ') i--;//忽略前面的空格

        while(i>=0&&s[i]!=' ') //当为字母的时候计算长度,为空格就退出循环

        {

            count++;

            i--;

        }

        return count;

    }

};

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

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

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

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

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

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

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

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

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

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

  8. Leetcode 58 Length of Last Word 字符串

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

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

随机推荐

  1. Numpy函数学习--genfromtxt函数

    genfromtxt函数 今天学习时遇到了genfromtxt函数 world_alcohol = numpy.genfromtxt("world_alcohol.txt",del ...

  2. Docker常见仓库Ubuntu

    Ubuntu 基本信息 Ubuntu 是流行的 Linux 发行版,其自带软件版本往往较新一些. 该仓库提供了 Ubuntu从12.04 ~ 14.10 各个版本的镜像. 使用方法 默认会启动一个最小 ...

  3. 2.docker常用命令

    一.安装相关 #查看docker是否安装 rpm -q docker #CentOS下安装docker   sudo yum install docker #启动 Docker systemctl s ...

  4. 2017-暑假作业-Java语言程序设计

    任务列表 1.学会使用Markdown做笔记 本篇随笔就是使用的Markdown语法.养成做笔记的习惯! 参考资料: 极简MarkDown排版介绍(How to) stackedit:在线Markdo ...

  5. ubuntu初始化python3+postgresql+uwsgi+nginx+django

    一. postgresql 数据库 安装 apt-get update apt-get install postgresql 进入psql客户端 sudo -u postgres psql 创建数据库 ...

  6. Hive基本原理及环境搭建

    今天我主要是在折腾这个Hive,早上看了一下书,最开始有点凌乱,后面慢慢地发现,hive其实挺简单的,以我的理解就是和数据库有关的东西,那这样的话对我来说就容易多啦,因为我对sql语法应该是比较熟悉了 ...

  7. IntelliJ Idea 设置 Dialyzer

    IntelliJ Idea 设置 Dialyzer(金庆的专栏)Erlang开发使用IDEA IDE可以设置外部工具Dialyzer, 然后就可以直接Tools->External Tools ...

  8. 设置TextView显示的文字可以复制

    设置TextView显示的文字可以复制 效果图 在xml中设置 <TextView android:layout_width="wrap_content" android:l ...

  9. Calling LoadLibraryEx on ISAPI filter failed

    今天在访问IIS下的站点时莫名奇妙的遇到这个问题Calling LoadLibraryEx on ISAPI filter"C://..."  failed,前面引号中的" ...

  10. RPCZ中的智能指针单例

    RPCZ中的智能指针单例 (金庆的专栏) 智能指针单例应用于 RPCZ 库以实现库的自动初始化与自动清理. RPCZ: RPC implementation for Protocol Buffers ...