class Solution {
public:
int lengthOfLastWord(const char *s) {
if(s=="") return ;
string snew=s;
int n=,len=strlen(s);
char *p=&snew[len-];
while(*p==' '&&len!=){
*p--;
len--;
}
while(*p!=' '&&len!=){
n++;
p--;
len--;
}
return n;
}
};

题意:给一个数组,以空格来判断是否字的结束与开始。一个词的前后都是空格,中间无空格。返回最后一个字的长度。

思路:给的是一个不可修改的字符串,为了方便,创建另外的指针。若最后是空格,先将后面的空格过滤,从后开始寻找第一个不是空格的字符,开始计数,继续往前数,直到有空格或者已经扫完字符串为止。

吐槽:感觉snew可以省略的,直接利用s就行。但是语法忘了~

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 @ Python

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

  4. [LeetCode] Length of Last Word

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

  5. Leetcode: Length of Last Word in python

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

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

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

  7. [LeetCode] Length of Last Word 字符串查找

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

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

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

  9. leetcode 题解: Length of Last Word

    leetcode: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', re ...

随机推荐

  1. 20169201 实验三 敏捷开发与XP实践报告

    敏捷开发与XP介绍 敏捷开发(Agile Development)是一种以人为核心.迭代.循序渐进的开发方法."敏捷流程"是一系列价值观和方法论的集合.从2001年开始,一些软件界 ...

  2. vue 报错 Uncaught (in promise) error

    可尝试在then()后加上catch() ps:该图来自网络

  3. 理解linux服务器mcelog如何工作

    What are Machine Check Exceptions (or MCE)? A machine check exception is an error dedected by your s ...

  4. 20170527关于Thingking in Java

    由于工作上的需求,要开始学习Android开发,所以先开始看一些Java方面的知识.学习从Thingking in Java开始,看了一下第一张,感觉真的是一本好书,希望自己努力把他看完,加油! 第一 ...

  5. sql server列转行的几种方法

    方法一,临时变量: declare @temp nvarchar(max)='' select @temp=coalesce(@temp,'')+Location+',' from( select d ...

  6. SQL Server2012如何打开2016的profiler文件

    SQL Server 2012如何打开2016的profiler文件 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/10980 ...

  7. Ocelot(四)- 认证与授权

    Ocelot(四)- 认证与授权 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/10932805.html 源码地址:http ...

  8. 洛谷P2055 [ZJOI2009]假期的宿舍

    P2055 [ZJOI2009]假期的宿舍 题目描述 学校放假了 · · · · · · 有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如 A 和 B 都是学校的学生,A ...

  9. 利用CSS制作脸书

    很多网站都支持图片上的头像框识别,鼠标在头像框处,会提示一些人物信息. 这次就利用CSS实现这样一个功能: div处主要包括两部分,一部分是图片:另一部分是链接以及脸框 <div class=& ...

  10. k8s集群新增节点

    节点为centos7.4 一.node节点基本环境配置 1.配置主机名 2.配置hosts文件(master和node相互解析) 3.时间同步 ntpdate pool.ntp.org date ec ...