【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 ...
随机推荐
- Android照相机应用
前言 Android在设计架构的时候,采用了mashup(混搭)的设计理念,也就是说一切都是组建,自己写的是组件,别人提供的也是组件,使用的时候只要符合相关协议就可以把他们当作自己的组件.比如系统提供 ...
- Java 中的抽象类及接口
抽象类使用 abstract 关键字修饰,该类即为抽象类. 抽象类的作用: 1.某些情况下,父类约束子类必须包含哪些方法,但不知道子类如何去实现这些方法. 2.可以从多个具有相同特征的类中抽象出一个抽 ...
- PropertyGrid 控件使用方法
编写一个对象,后面传递给 PropertyGrid 来显示: using System; using System.Collections.Generic; using System.Linq; us ...
- [转]命令行 Subversion 入门
http://omyyal.iteye.com/blog/1762831 命令行 Subversion 入门 如果您参与的项目正在使用 Subversion 进行版本控制,您将需要使用 Subvers ...
- C语言--通用类型栈
#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <string.h&g ...
- Mybatis关于like的字符串模糊处理
其中通过"%"#{key}"%"来拼接语句 <sql id="select_where"> from cellphone c l ...
- 通过DB_LINK按照分区表抽取数据
DB:11.2.0.3.0OS:oracle-linux 5.7 建表语句:create table YOON.YOON_HIS( c_id NUMBER not null ...
- hdu 2579 Dating with girls(2)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) Description If you have sol ...
- android asmack 注册 登陆 聊天 多人聊天室 文件传输
XMPP协议简介 XMPP协议(Extensible Messaging and PresenceProtocol,可扩展消息处理现场协议)是一种基于XML的协议,目的是为了解决及时通信标准而提出来的 ...
- Go返回参数命名
Go语言中可以为返回值定义名称.代码实例: package main import "fmt" func add1(a int, b int) int { return a + b ...