【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 ...
随机推荐
- windows phone主题切换(换肤)
之前项目做了个主题切换的功能,最后客户没来得及出第二套界面给放弃了,默哀中... 为了不让它就这样流产了,就放博客共享吧. 首先说明下原理:这个切换是通过重写资源字典里指定的样式,在运行的过程中加载指 ...
- vim 文字插入
我们知道VIM中,普通的复制和粘贴都是YY和PP.那么怎么将vim以外的文件插入到vim编辑器中呢!这是个问题: 首先我们要选中想要插入的文字,如: 然后进入vim插入模式:SHIFT + Inser ...
- [leetcode]_Symmetric Tree
第二道树的题目,依旧不会做,谷歌经验. 题目解释: give you a tree , judge if it is a symmetric tree. 思路:我以为要写个中序遍历(进阶学习非递归算法 ...
- Knockout.Js官网学习(简介)
前言 最近一段时间在网上经常看到关于Knockout.js文章,于是自己就到官网看了下,不过是英文的,自己果断搞不来,借用google翻译了一下.然后刚刚发现在建立asp.net mvc4.0的应用程 ...
- Hadoop1.x与2.x安装笔记
Hadoop1.x与2.x安装笔记 Email: chujiaqiang229@163.com 2015-05-09 Hadoop 1.x 安装 Hadoop1.x 集群规划 No 名称 内容 备注 ...
- ok6410内存初始化
•DRAM:它的基本原件是小电容,电容可以在两个极板上保留电荷,但是需要定期的充电(刷新),否则数据会丢失.缺点:由于要定期刷新存储介质,存取速度较慢. •SRAM:它是一种具有静止存取功能的内存,不 ...
- Python核心编程--学习笔记--1--Python简介
本章介绍了Python的背景知识,包括什么是Python.Python的起源以及Python的一些关键特性. 1 什么是Python Python是一门优雅而健壮的编程语言,它继承了传统编译语言的强大 ...
- Python学习教程(learning Python)--1.2.3 Python格式化输出百分比
在有些情况下,需要百分比输出数据,我们可以继续使用Python内建函数format来实现百分比的数据输出. >>> print(format(0.5236, '.2%')) 其结果如 ...
- Linux 配置jdk环境变量
1.首先去官网下载所需版本的jdk,必须是.linux下的安装版本. 2.解压到以文件下 3.vim /etc/profile or ~/.bashrc 添加如下环境配置 JAVA_HOME=/usr ...
- 基于socket的客户端和服务端聊天机器人
服务端代码如下: using System;using System.Net;using System.Net.Sockets;using System.Text;using System.Threa ...