LeetCode - Reverse Words in a String
2014-03-18 03:13
351 查看
Reverse Words in a String
2014.3.18 03:09
Given an input string, reverse the string word by word.
For example,
Given s = "
return "
click to show clarification.
Clarification:
What constitutes a word?
A sequence of non-space characters constitutes a word.
Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces.
How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
Solution:
Reverse the whole string first, then reverse every single word. Redundant spaces must be skipped.
Total time complexity is O(n). Space complexity is O(1).
Accepted code:
2014.3.18 03:09
Given an input string, reverse the string word by word.
For example,
Given s = "
the sky is blue",
return "
blue is sky the".
click to show clarification.
Clarification:
What constitutes a word?
A sequence of non-space characters constitutes a word.
Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces.
How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
Solution:
Reverse the whole string first, then reverse every single word. Redundant spaces must be skipped.
Total time complexity is O(n). Space complexity is O(1).
Accepted code:
// 1TLE, 1AC, using another char[] is unnecessary. Don't miss '++i' or '++j'. class Solution { public: void reverseWords(string &s) { int i, j; int len; int offset; // remove trailing spaces while (s.length() > 0 && s[s.length() - 1] == ' ') { s.pop_back(); } len = (int)s.length(); if (len == 0) { return; } // remove leading spaces i = 0; while (i < len && s[i] == ' ') { ++i; } s = s.substr(i, len - i); len = (int)s.length(); // reverse the whole string reverse(s, 0, len - 1); // reverse every word i = 0; while (i < len) { j = i; while (j < len && s[j] != ' ') { ++j; } reverse(s, i, j - 1); i = j; while (i < len && s[i] == ' ') { ++i; } } // remove redundant spaces between words offset = 0; i = 0; while (true) { j = i; while (j < len && s[j] != ' ') { s[j - offset] = s[j]; ++j; } i = j; if (i == len) { break; } s[i - offset] = s[i]; ++i; while (i < len && s[i] == ' ') { ++i; ++offset; } } while (offset > 0) { s.pop_back(); --offset; } } private: void reverse(string &s, int ll, int rr) { int i; char ch; for (i = ll; i < ll + rr - i; ++i) { ch = s[i]; s[i] = s[ll + rr - i]; s[ll + rr - i] = ch; } } };
相关文章推荐
- S3C6410 启动u-boot 加载内核和文件系统
- 《Cracking the Coding Interview》——第2章:链表——题目7
- direct填充
- 《Cracking the Coding Interview》——第2章:链表——题目6
- DNS分析之 dnsdict6 使用方法
- msvbvm60中函数介绍
- flex loaderInfo为null在creationComplete事件中
- 腾讯与搜狗的爱情续篇
- 《Cracking the Coding Interview》——第2章:链表——题目5
- HTML的级联Select
- 51单片机学习笔记(二)_流水灯实验
- SQL SERVER中FIRST_VALUE和LAST_VALUE
- 《Cracking the Coding Interview》——第2章:链表——题目4
- 《Cracking the Coding Interview》——第2章:链表——题目3
- Linux命令之sleep - 延迟指定时间
- Hive 0.12 Caused by: MetaException(message:Version information not found in metastore. )解决方法
- 《Cracking the Coding Interview》——第2章:链表——题目2
- c语言数据结构原型与实例分析(数组,链表)
- 第三周作业,循环与判断语句(网络131 )
- 《Cracking the Coding Interview》——第2章:链表——题目1