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. linux替换yum源及配置本地源

    linux系统安装后自带的bash源由于在国外,安装软件包的时候会非常慢,最好替换一下yum源. ​关于yum源的简单介绍 ​           yum的主要功能是更方便地添加,删除和更新rpmba ...

  2. 正则表达式-基础知识Review

    正则表达式(Regular Expression)是计算机科学的一个概念. 正则表达式使用单个字符窜来描述.匹配一系列符合某个句法规则的字符窜. 在很多文本编辑器里, 正则表达式通常用来被检索替换哪些 ...

  3. hive数据的导入导出方式

    导入方式 1.load方式 load data local inpath 'local_path' into table tb_name; 从本地复制了文件到表的路径下 应用场景:大部分的使用,文件几 ...

  4. ActiveMQ发布-订阅消息模式(同点对点模式的区别)

    点对点与发布订阅最初是由JMS定义的.这两种模式主要区别或解决的问题就是发送到队列的消息能否重复消费(多订阅) 点对点: 消息生产者生产消息发送到queue中,然后消息消费者从queue中取出并且消费 ...

  5. CCPC_1005

    可怕.....的提.....显而易见的规律活活没照出来...不过说起来却是不能严格证明....于是...脑筋急转弯活活猜不出来..... 1*1->1*2->2*2->2*3-> ...

  6. Redis实现之数据库(一)

    服务器中的数据库 Redis服务器将所有数据库都保存在服务器状态redis.h/redisServer结构体的db数组中,db数组的每个项都是一个redis.h/redisDb结构体,每个redisD ...

  7. 同步锁之lock

    一. synchronized的缺陷 当一个代码块被synchronized修饰时,同时该代码块被一个线程执行,其他线程便只能一直等待,等待获取锁的线程释放锁,而这里获取锁的线程释放锁只会有两种情况: ...

  8. Ntdsutil.exe

    Ntdsutil.exe 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! Ntdsutil.exe 是一个为 Active Directory 提供管理设施的命令行工具 ...

  9. Marketing learning-3

    Part five brand mantra: the elevator speed 1.mental map:Portrays brand associations and responses fo ...

  10. 微信小程序-----校园头条详细开发之注册登录

    1.注册登录功能的实现 1.1结构 1.2 代码实现 1.2.1  为了通信的安全着想,在此我是通过小程序端获得code,然后传递给后端,在后端向微信后台发送api请求,解密,从而得到用户的唯一标示o ...