Length of Last Word

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.

C++代码实现:

#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<cstring>
using namespace std;

class Solution {
public:
    int lengthOfLastWord(const char *s) {
        istringstream istr(s);
        vector<string> vec;
        string ss;
        while(istr>>ss)
            vec.push_back(ss);
        if(vec.size()==0)
            return 0;
        return vec[vec.size()-1].length();
    }
};

int main()
{
    const char *s = "Hello World";
    Solution ss;
    cout<<ss.lengthOfLastWord(s)<<endl;
}

本来是很简单的一个题,但是因为没有判断字符串全部由空格组成。这时经过istringstream处理之后压入到vector中的元素将是0个,因此vec.size()将是0,所以最后一个将返回运行时错误。(细节决定成败啊)

 以前怎么想到那么麻烦的方法呢,明明用双指针就可以搞定的事啊。。

class Solution {
public:
    int lengthOfLastWord(const char *s) {
        if(s==NULL)
            return 0;
        int len=0;
        const char *p=s;
        const char *q=NULL;
        while(*p!='\0') ++p;
        p--;
        while(p>=s&&*p==' ') --p;
        if(p<s)
            return 0;
        q=p;
        while(q>=s&&*q!=' ') q--;
        len=p-q;
        return len;
    }
};

 

时间: 2024-10-13 09:36:09

Length of Last Word的相关文章

Length of Last Word:求最后一个单词的长度

[ 问题: ] 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. 给你一个字符串,设法获取它最后一个单词的长度.如果这个单词不存在,则返回0. [ 分析 : ]A word is defined

[LeetCode]58.Length of Last Word

题目 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

【LeetCode从零单排】No58.Length of Last Word

题目 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

[LeetCode] Length of Last Word - 最后一个单词的长度

题目概述: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-spac

struts项目中导出word excel不成功 麻烦大虾看一下

问题描述 <%@pagelanguage="java"pageEncoding="gbk"%><%@tagliburi="http://struts.apache.org/tags-bean"prefix="bean"%><%@tagliburi="http://struts.apache.org/tags-html"prefix="html"%>&l

我的VSTO之路(三):Word基本知识

原文:我的VSTO之路(三):Word基本知识 在前一篇文章中,我初步介绍了如何如何开发一个VSTO程序,在本文中,我将进一步深入介绍Word的插件开发.Word是一个大家在日常工作中一直接触的文档工具,也是微软最赚钱的产品之一.从最初的Word 1.0到现在的Word 2010历经了13代的演化,已经成为了一个比较复杂的系统.(这里稍微跑题一下,Office 2010的版本代号是version 14,但是我为什么说Word一共演化了13代呢?因为Office并没有Version 13,上一代的

JavaScript打开word文档的实现代码(c#)_javascript技巧

在C#中打开word文档其实不算太难,方法也比较多. 一.C#中打开word文档方法 复制代码 代码如下: //在项目引用里添加上对Microsoft Word 11.0 object library的引用 private void button1_Click(object sender, System.EventArgs e) { //调用打开文件对话框获取要打开的文件WORD文件,RTF文件,文本文件路径名称 OpenFileDialog opd = new OpenFileDialog()

[python] Kmeans文本聚类算法+PAC降维+Matplotlib显示聚类图像

0 前言 本文主要讲述以下几点:        1.通过scikit-learn计算文本内容的tfidf并构造N*M矩阵(N个文档 M个特征词):        2.调用scikit-learn中的K-means进行文本聚类:        3.使用PAC进行降维处理,每行文本表示成两维数据:        4.最后调用Matplotlib显示聚类效果图. 文章更详细的内容参考:http://blog.csdn.net/eastmount/article/details/50473675由于涉及

Java Thread Programming 1.8.4 - Inter-thread Communication

Streaming Data Between Threads Using Pipes The java.io package provides many classes for writing and reading data to and from streams. Most of the time, the data is written to or read from a file or network connection. Instead of streaming data to a