【Length of Last Word】cpp
题目:
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的更多相关文章
- 【Merge K Sorted Lists】cpp
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- 【String to Integer (atoi) 】cpp
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- Hdu 4738【求无向图的桥】.cpp
题目: 曹操在长江上建立了一些点,点之间有一些边连着.如果这些点构成的无向图变成了连通图,那么曹操就无敌了.刘备为了防止曹操变得无敌,就打算去摧毁连接曹操的点的桥.但是诸葛亮把所有炸弹都带走了,只留下 ...
- 【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- ...
- 【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 ...
- 【Search for a Range】cpp
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- 【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 ...
- 【Largest Rectangle in Histogram】cpp
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- 【Validate Binary Search Tree】cpp
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
随机推荐
- 【转载】UML用例图
用例图主要用来描述“用户.需求.系统功能单元”之间的关系.它展示了一个外部用户能够观察到的系统功能模型图. [用途]:帮助开发团队以一种可视化的方式理解系统的功能需求. 用例图所包含的元素如下: 1. ...
- linux环境配置
一.JDK安装 1.通过xftp工具把jdk-8u60-linux-x64.gz上传到linux 2.解压JDK命令tar -xzf jdk-8u60-linux-x64.gz 3.linux配置环境 ...
- MongoDB工具介绍
在Windows下面,mongodb就只有一个bin目录以及bin目录以外的三个文件,相对bin目录中包括了如下文件: bsondump.exe 用于将导出的BSON文件格式转换为JSON格式 mon ...
- JSON.stringify 语法实例讲解
语法: JSON.stringify(value [, replacer] [, space]) value:是必选字段.就是你输入的对象,比如数组,类等. replacer:这个是可选的.它又分为 ...
- Incorrect integer value: '' for column 'id' at row 1
最近在写个查询 插入语句的时候 我是这么写的 insert into test values('',$row[contentid],'".$tn."'); 结果搞死没插入进去 然 ...
- How to executing direct SQL statements [Axapta, AX4.0, AX2009, AX2012]
Today I want to talk about executing SQL statements in X++ on both the current AX database and exter ...
- pure css做的pc登陆界面
源码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- python 函数的参数对应
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经接触过函数(function)的参数(arguments)传递.当时我们根 ...
- eclipse新建android项目,编译出错解决方法
1.新建android项目 2.在libs中,将android-support-v4.jar添加到生成目录 3.如果项目引用了ActionBar等,需要引用V7的话,添加外部Jar包,路径为eclip ...
- LaTex中让页码从正文开始编号
在正文和目录之前这样设置即可 \setcounter{page}{}