leetCode题解之寻找string中最后一个word的长度

1、题目描述

返回一个 string中最后一个单词的长度。单词定义为没有空格的连续的字符,比如 ‘a’,‘akkk‘。

2、问题分析

从后向前扫描,如果string是以空格‘  ’结尾的,就不用计数,现将空格最后的空格排除掉,直到找到最后一个出现的字符。从最后一个出现的字符开始计数,一直到下一次出现空格‘ ’。

特殊情况是 输入的字符串是空的,先排除掉。

3、代码

 1
 1 int lengthOfLastWord(string s) {
 2
 3         if(s.size() == 0)    // 排除字符长度为 0 的情况
 4             return 0;
 5
 6         int i = s.size() - 1;
 7         int len = 0;
 8        while( s[i] == ‘ ‘ && i >= 0)   // 排除掉最后的空格
 9        {
10            i--;
11        }
12
13         while(s[i] != ‘ ‘ &&  i >= 0)   // 从最后一个出现的字符开始,
14         {                                         // 向前寻找空格,找到空格或者字
15             i--;                                  // 符结束终止寻找
16             len++;
17         }
18
19        return len;
20
21     }

原文地址:https://www.cnblogs.com/wangxiaoyong/p/8650392.html

时间: 2024-08-30 02:13:22

leetCode题解之寻找string中最后一个word的长度的相关文章

leetcode—58 Length of Last Word Total(字符串中最后一个单词的长度)

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 cha

LeetCode 题解 | 237. 删除链表中的节点

题目描述: 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 --?head =?[4,5,1,9],它可以表示为: 示例 1: 输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为?5?的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9. 示例 2: 输入: head = [4,5,1,9], node = 1 输出: [4,5,9] 解释: 给定你链表

LeetCode题解之Rotate String

1.题目描述 2.问题分析 直接旋转字符串A,然后做比较即可. 3.代码 1 bool rotateString(string A, string B) { 2 if( A.size() != B.size() ) 3 return false; 4 if( A.empty() && B.empty() ) 5 return true; 6 int i = 0 ; 7 while( i < A.size() ){ 8 char c = A[0] ; 9 A += c; 10 A.er

Oracle中查询一个字符串的长度的函数

Oracle取字符串长度的函数length()和lengthb() lengthb(string)计算string所占的字节长度:返回字符串的长度,单位是字节 length(string)计算string所占的字符长度:返回字符串的长度,单位是字符 对于单字节字符,lengthb()和length()是一样的 可以用lengthb('string')=length('string') 判断字符串是否含有中文

leetcode 题解: Length of Last Word

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

计蒜客--最后一个单词的长度

给定由大写,小写字母和空格组成的字符串,返回 最后 一个单词的长度. 如果输入中不存在单词,返回 00. 注意: “单词”是指不包含空格符号的字符串 例如: 对于字符串"hello World"(不带引号), 那么返回的结果是 55: 对于字符串"abc abc "(不带引号),那么返回的结果就是 33. 输入格式 输入仅一行,为字符串 ss(长度不超过 1000010000). 输出格式 输出 ss 中最后一个单词的长度. 样例输入1 Today is a nic

计蒜客 最后一个单词的长度 (字符串)

给定由大写,小写字母和空格组成的字符串,返回最后一个单词的长度. 如果不存在最后一个单词,返回0 注意: “单词”是指不包含空格符号的字符串 例如: s = “hello World”, 那么返回的结果是5 格式: 第一行输入字符串s,然后输出s中最后一个单词的长度. 样例输入 Today is a nice day 样例输出 3分析:可能输入的是空串,也可能末尾有空格 1 #include <iostream> 2 #include <string> 3 4 using name

第12题:最后一个单词的长度

给定由大写,小写字母和空格组成的字符串,返回最后一个单词的长度. 如果不存在最后一个单词,返回0 注意: "单词"是指不包含空格符号的字符串 例如: s = "hello World", 那么返回的结果是5 格式: 第一行输入字符串s,然后输出s中最后一个单词的长度. 样例输入 Today is a nice day 样例输出 3 关于此题: 这道题有个陷阱,题目说是 s = "hello World",那个这字符串也有可能是s = "

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序