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. 某欧洲电信运营商OSS功能架构

  2. C++基础之函数和作用域

    (1)函数的定义格式如下所示.<类型><函数名>(<形参表>) {<若干条语句>}其中,<类型>包含存储类和数据类型.存储类省略为外部函数, ...

  3. Java中的变量传递机制以及JS中的参数传递机制

    JAVA: 传递基本类型是 就是基本的值传递 不会影响值本身. package com.wuqi.p1; public class ValuePassTest { public static void ...

  4. C++的STL总结(2)

    紧接着上篇博客,把没总结全的继续补充. (13)set容器 set是用红黑树的平衡二叉索引树的数据结构来实现的,插入时,它会自动调节二叉树排列,把元素放到适合的位置,确保每个子树根节点的键值大于左子树 ...

  5. Flask-SQLAlchemy 配置,处理对象-关系,一对多,多对多

      ORM(Object Relational Mapper) 对象关系映射.指将面对对象得方法映射到数据库中的关系对象中. Flask-SQLAlchemy是一个Flask扩展,能够支持多种数据库后 ...

  6. 搭建一个简单的FTP服务器

    本文介绍通过win7自带的IIS来搭建一个只能实现基本功能的FTP服务器,第一次装好WIN7后我愣是没整出来,后来查了一下网上资料经过试验后搭建成功,其实原理和步骤与windows前期的版本差不多,主 ...

  7. vue中点击空白处隐藏弹框(用指令优雅地实现)

    在写vue的项目的时候,弹框经常性出现,并要求点击弹框外面,关闭弹框,那么如何实现呢?且听我一一...不了,能实现效果就好 <template> <div> <div c ...

  8. python3.6下安装wingIDE破解方法

    1.wingIDE的下载: 在电脑配置好的python环境情况下,去官网下载wingIDE6,按照一般方式安装好.安装好它会自动提示你是否激活,你点击激活.然后到下一步. 2.脚本的制作: impor ...

  9. AJAX下拉框联动

    function getProvince() { var ProName = $("#dvProv").val(); LoadProvince(ProName); } functi ...

  10. ubuntu apache2 https

    1. enable the module ssl by: sudo a2enmod ssl 2.after you have enabled module ssl , you will have to ...