【44】翻转单词顺序列
来源:互联网 发布:剑网3捏脸数据女 编辑:程序博客网 时间:2024/04/30 19:22
【44】翻转单词顺序列
- 时间限制:1秒
- 空间限制:32768K
- 本题知识点: 字符串
题目描述
牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?
牛客网题目链接:点击这里
VS2010代码:
// Source: http://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3?tpId=13&tqId=11197&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking// Author: Yang Qiang// Date : 2016-8-7#include<iostream>#include<stack>using namespace std;//方法一:提示超出内存使用限制class Solution { public: string ReverseSentence(string str) { if(str.empty()) return str; string result; stack<string> S; int curBlankPos=0; int preBlankPos=0; string subString; while(curBlankPos!=str.size()) { curBlankPos=str.find_first_of(' ', preBlankPos); if(curBlankPos<0) curBlankPos=str.size(); subString=str.substr(preBlankPos, curBlankPos-preBlankPos ); S.push(subString); preBlankPos=curBlankPos+1; } subString.erase(); while(!S.empty()) { result+=S.top(); S.pop(); if(!S.empty()) result+=" "; subString.erase(); } return result; }};int main(){ string test1="I am a student."; Solution s1; string test; test=s1.ReverseSentence(test1); for(int i=0; i!=test.size(); i++) { cout<<test[i]; } cout<<"ok"<<endl;}
补充字符串string知识:
http://www.cnblogs.com/dongsheng/articles/2685114.html
- string类的size()/length()方法。两者没有区别。
- string::npos 是什么 c++ 。string 类提供了 6 种查找函数,每种函数以不同形式的 find 命名。这些操作全都返回 string::size_type 类型的值,以下标形式标记查找匹配所发生的位置;或者返回一个名为 string::npos 的特殊值,说明查找没有匹配。string 类将 npos 定义为保证大于任何有效下标的值。int型显示为-1.
6提取子串和字符串连接
提取子串的函数是:substr(),形式如下:
s.substr();//返回s的全部内容
s.substr(11);//从索引11往后的子串
s.substr(5,6);//从索引5开始6个字符
把两个字符串结合起来的函数是+。
string的连接:
string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾
string &append(const char *s); //把c类型字符串s连接到当前字符串结尾。
string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾。
string &append(const string &s); //同operator+=()
string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾。
string &append(int n,char c); //在当前字符串结尾添加n个字符c。8搜索与查找
查找函数很多,功能也很强大,包括了:
find() rfind() find_first_of() find_last_of() find_first_not_of() find_last_not_of()
这些函数返回符合搜索条件的字符区间内的第一个字符的索引,没找到目标就返回npos。
npos的含义,string::npos的类型是string::size_type,所以,一旦需要把一个索引与npos相比,这个索引值必 须是string::size_type类型的,更多的情况下,我们可以直接把函数和npos进行比较(如:if(s.find(“jia”)== string::npos))。
int find(char c, int pos = 0)//从pos开始查找字符c在当前字符串的位置。
int find(const char *s, int pos = 0) //从pos开始查找字符串s在当前串中的位置。
int find(const char *s, int pos, int n) //从pos开始查找字符串s中前n个字符在当前串中的位置。
int find(const string &s, int pos = 0) //从pos开始查找字符串s在当前串中的位置。查找成功时返回所在位置,失败返回string::npos的值。
int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置。
int rfind(const char *s, int pos = npos)
int rfind(const char *s, int pos, int n = npos)
int rfind(const string &s,int pos = npos)
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值。
int find_first_of(char c, int pos = 0) //从pos开始查找字符c第一次出现的位置。
int find_first_of(const char *s, int pos = 0)
int find_first_of(const char *s, int pos, int n)
int find_first_of(const string &s,int pos = 0)
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos 。
int find_first_not_of(char c, int pos = 0)
int find_first_not_of(const char *s, int pos = 0)
int find_first_not_of(const char *s, int pos,int n)
int find_first_not_of(const string &s,int pos = 0)
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos 。
int find_last_of(char c, int pos = npos)
int find_last_of(const char *s, int pos = npos)
int find_last_of(const char *s, int pos, int n = npos)
int find_last_of(const string &s,int pos = npos)
int find_last_not_of(char c, int pos = npos)
int find_last_not_of(const char *s, int pos = npos)
int find_last_not_of(const char *s, int pos, int n)
int find_last_not_of(const string &s,int pos = npos)
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找 。
牛客网通过图片:
- 【44】翻转单词顺序列
- 44、翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- 翻转单词顺序列
- missForest一种非参数的缺失值填补方法
- LeetCode进阶之路( First Missing Positive)
- MIT License探讨
- JS对象及其方法-Date
- 安卓第三方支付之银联支付
- 【44】翻转单词顺序列
- 根据银行卡账号获取所属银行php代码
- java中Map
- 数论 知识概念梳理
- leetcode题目整理
- 资源分享
- JNDI参数设置
- 北京研究院联想公司android面试题
- (暴力+各种算法)hihoCoder - 1353 满减优惠