Lintcode 翻转字符串
来源:互联网 发布:北京时时彩计划数据 编辑:程序博客网 时间:2024/05/16 19:05
题目
给定一个字符串,逐个翻转字符串中的每个单词。
说明
单词的构成:无空格字母构成一个单词
输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包括
如何处理两个单词间的多个空格?在反转字符串中间空格减少到只含一个
样例
给出s = “the sky is blue”,返回”blue is sky the”
class Solution {public: /** * @param s : A string * @return : A string */ string reverseWords(string s) { // write your code here //首先如何判断一个完整的单词? //连续的字符中间没有空格 if(s.empty()||s.size()==1||s.find(" ")==-1) return s; string s0; int i = s.size() - 1;//i表示每个单词的开头 int j = s.size() - 1;//j表示每个单词的结尾 //如果最后一个字符是空格,那么删除 while (s[i]== ' ') { i--; j--; } while (i >= 0 && j >= 0) { //没有遇到空格,那么是单词的一部分,i继续向前移 if (s[i] != ' '&&i>0) i--; //i指向空格 else if (i == 0) { s0.insert(s0.size(), s, i, j - i + 1); break; } else { //首先需要将i到j的单词复制给s0, s0.insert(s0.size(), s, i+1, j - i+1); //然后需要判断是否有连续多个空格 i--; while (i >= 0&&s[i] == ' ') i--; if (i >= 0) { s0.append(" "); j = i; } else { j = i; break; } } } }};
0 0
- LintCode- 翻转字符串
- lintcode --翻转字符串
- LintCode(easy)翻转字符串
- LintCode(3):翻转字符串
- lintcode,翻转字符串
- Lintcode 翻转字符串
- LintCode 53 翻转字符串
- lintcode--翻转字符串
- [LintCode]53.翻转字符串
- lintcode翻转字符串
- [Lintcode] #53 翻转字符串
- 翻转字符串(LintCode)
- LintCode-剑指Offer-(53)翻转字符串
- LintCode 第53题 翻转字符串
- 【LintCode】Reverse Words in a String 翻转字符串
- 【LintCode】Reverse Words in a String 翻转字符串
- LintCode 翻转二叉树
- LintCode-翻转链表
- 进程并发与互斥性问题小结
- 数据库初始化
- Shallow heap & Retained heap
- Spark RDD 到 LabelPoint的转换(包含构造临时数据的方法)
- CSDN-markdown编辑器使用教程
- Lintcode 翻转字符串
- 【9-1-1】静态链表和动态链表
- char *a 与char a[] 的区别
- C++ builder提供从MFC到自身支持的过程
- 浅谈决策树c4.5
- Merge Sorted Array
- 6代cpu安装win7系统提示无驱动的解决
- Locked-Free Data Structures笔记
- JavaScript 跨域问题简述