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.

Hide Tags

String

 

    这题比较简单,只是可能前面有空格,后面有空格。- -
算法逻辑:
  1. 排除最后的空格
  2. index 从后往前查第一个空格。
  3. 返回长度。
 #include <iostream>
#include <cstring>
using namespace std; class Solution {
public:
int lengthOfLastWord(const char *s) {
int n = strlen(s);
if(n <) return ;
int idx=n-;
while(idx>=&&s[idx]==' ') idx--;
n = idx+;
while(idx>=){
if (s[idx]==' ') break;
idx --;
}
// if(idx<0) return 0;
return n - idx -;
}
}; int main()
{
char s[] = " 12 ";
Solution sol;
cout<<sol.lengthOfLastWord(s)<<endl;
return ;
}

[LeetCode] Length of Last Word 字符串查找的更多相关文章

  1. 【LeetCode每天一题】Length of Last Word(字符串中最后一个单词的长度)

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

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

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

  3. LeetCode——Length of Last Word

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

  4. [leetcode]Length of Last Word @ Python

    原题地址:https://oj.leetcode.com/problems/length-of-last-word/ 题意: Given a string s consists of upper/lo ...

  5. [Leetcode] Length of last word 最后一个单词的长度

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

  6. [LeetCode] Length of Last Word

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

  7. Leetcode: Length of Last Word in python

    Length of Last Word Total Accepted: 47690 Total Submissions: 168587     Given a string s consists of ...

  8. Leetcode 58 Length of Last Word 字符串

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

  9. LeetCode Length of Last Word 最后一个字的长度

    class Solution { public: int lengthOfLastWord(const char *s) { ; string snew=s; ,len=strlen(s); ]; ) ...

随机推荐

  1. 第八篇:ORM框架SQLAlchemy 了解知识

    一 介绍 SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据API执行SQL并获取 ...

  2. 第39-43课 thinkphp5完成商品会员价格功能(后置勾子afterInsert)

    目录 功能一:利用后置勾子,处理好商品主键id,会员的价格,再插入member_price表里. 要实现的功能: 思路: html里 控制器里 模型里的后置勾子afterInsert() 功能二:利用 ...

  3. Python9-From-CSS-day48

    1.form表单相关内容前后端有数据交互的时候用form表单form表单提交数据的几个注意事项: 1.所有获取用户输入的标签都必须放在form表单里面 2.action 控制着往哪里提交 3.inpu ...

  4. Codeforces Round #460 (Div. 2)-D. Substring

    D. Substring time limit per test3 seconds memory limit per test256 megabytes Problem Description You ...

  5. 动态规划:HDU1864-最大报销额(处理带小数的dp问题)

    最大报销额 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Descriptio ...

  6. mysql 外连接的时候,条件在on后面和条件在where后面的区别

    最近使用mysql的时候碰到一个问题:当一个表外联另一个表的时候,将一些查询条件放在on后面和放在where后面不太一样: 学生分数表stuscore: 当查询语句如下(查询语句1): SELECT ...

  7. IOS开发---菜鸟学习之路--(十)-实现新闻详细信息浏览页面

    前面已经将了上下拉刷新 实现了上下拉刷新后我们的第一级界面就做好,接下来我们就需要实现 新闻详细信息浏览了 我个人认为一般实现新闻详细页面的方法有两种(主要是数据源的不同导致了方法的不同) 第一种是本 ...

  8. MongoDB快速入门学习笔记6 MongoDB的文档删除操作

    db.集合名称.remove({query}, justOne)query:过滤条件,可选justOne:是否只删除查询到的第一条数据,值为true或者1时,只删除一条数据,默认为false,可选. ...

  9. jquery实现轮播插件

    这几天用jquery写了两个轮播的插件,功能很简单.第一次尝试写插件,有很多不足的地方,代码如下: 注:图片链接请替换掉,配置信息必须加上图片width和height. <!DOCTYPE ht ...

  10. How Ironic Inspector Works

    翻译官网概述. 操作员将节点注册为Ironic,例如 通过openstack baremetal CLI命令. 电源管理认证应该在这一步提供给Ironic. 如节点状态所述,节点被置于正确的自省状态. ...