题目:

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.

代码:

class Solution {
public:
int lengthOfLastWord(string s) {
for ( int i = s.length()-; i >=; --i )
{
if (s[i]==' ')
{
s.erase(s.end()-);
}
else
{
break;
}
}
const size_t len = s.length();
int ret = ;
for ( size_t i = ; i < len; ++i )
{
if (s[i]!=' ')
{
++ret;
continue;
}
if (s[i]==' ')
{
ret = ;
continue;
}
}
return ret;
}
};

tips:

先把后面的空格都去掉。然后从头遍历,最后留下的ret就是最后一个单词的长度。

还有STL的一个做法,明天再看。

==========================================

第二次过这道题,感觉不知道第一次为啥还用erease这种了,直接一个指针从后往前遍历就AC了。

class Solution {
public:
int lengthOfLastWord(string s) {
int i = s.size()-;
while ( i>= ){
if ( s[i]==' ' )
{
--i;
}
else
{
break;
}
}
int ret = ;
while ( i>= )
{
if (s[i]!=' ')
{
++ret;
--i;
}
else
{
break;
}
}
return ret;
}
};

【Length of Last Word】cpp的更多相关文章

  1. 【Merge K Sorted Lists】cpp

    题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  2. 【String to Integer (atoi) 】cpp

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  3. Hdu 4738【求无向图的桥】.cpp

    题目: 曹操在长江上建立了一些点,点之间有一些边连着.如果这些点构成的无向图变成了连通图,那么曹操就无敌了.刘备为了防止曹操变得无敌,就打算去摧毁连接曹操的点的桥.但是诸葛亮把所有炸弹都带走了,只留下 ...

  4. 【Reverse Linked List II】cpp

    题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...

  5. 【Search a 2D Matrix】cpp

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the f ...

  6. 【Search for a Range】cpp

    题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...

  7. 【Merge Two Sorted Lists】cpp

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  8. 【Largest Rectangle in Histogram】cpp

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  9. 【Validate Binary Search Tree】cpp

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

随机推荐

  1. sublime搭建Java编译平台及编码问题

    Sublime自带Java编译功能,当时只能编译不能运行,我们做一下小小的修改就可以让sublime一步完成编译运行的功能,实现sublime搭建Java编译平台. 使用Ctrl + B 编译时,所编 ...

  2. 学习web前端开发感想

    1.学习一个技术,不是一看见源代码就是copy,而是仔细阅读后,找到自己想要的,并且自己写出来,自己理解了,下次遇到同样的问题,自己才能解决. 2.在电脑上学习的过程中,我总是先建立一个文本文档,这样 ...

  3. dataGridView 如何默认选中第一行

    datagridview默认选中第一行方法: this.dataGridView1.Rows[0].Selected = true; datagridview 去除 默认选中第一行方法:在绑定data ...

  4. c语言学习的第五天

    #include<stdio.h> #include<stdbool.h> int main() { _Bool num=1; if (num==true); { printf ...

  5. while循环中不支持循环使用curl

    <?php $link = mysql_connect('localhost', 'sms', 'sms'); mysql_select_db('sms', $link); mysql_quer ...

  6. Android Material Design:NavigationView抽屉导航菜单

    需要添加的包: 测试代码: package com.zzw.navigationview; import android.app.Activity; import android.os.Bundle; ...

  7. SIMATIC IT HISTORIAN在烟用二醋酸纤维素生产中应用

    原文转载自:http://www.soft6.com/tech/5/54287.html 本文介绍了西门子MES核心产品SIMATIC IT HISTORIAN实时数据库及客户端工具在流程生产中的具体 ...

  8. python爬取糗百第一页的笑话

    自学python网络爬虫,发现request比urllib还是要好用一些,因此利用request和BeautifulSoup来实现糗百的首页笑话的抓取.BeautifulSoup通过find和find ...

  9. 内核堆分配函数brk()源码分析

    Evernote公开链接:http://www.evernote.com/shard/s133/sh/5b8d3b26-0e53-4c61-aa43-66f6e87bbcb7/a44096dd557f ...

  10. eth0: error fetching interface information: Device not found

    转载,原文出处:http://zh888.blog.51cto.com/1684752/775447 亲测有效,感谢作者!!! ----------------------------分割线----- ...