108_leetcode_reverse Words
来源:互联网 发布:iphone 投影到windows 编辑:程序博客网 时间:2024/05/21 09:32
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
1:特殊情况;2:反转整个字符串;3:删除字符串前和后的所有空格,并且单词之间只保留一个字符;4:找到单词反转单词
void reverseWords(string &s) { if(s[0] == '\0') { return; } int size = (int)s.length(); reverseWord(s, 0, size-1); deleteSpace(s); if(s[0] == '\0') { return; } int start = 0; int end = 0; while(s[end] != '\0') { while(s[end] != '\0' && s[end] != ' ') { end++; } end--; reverseWord(s, start, end); end++; if(s[end] == '\0') { break; } start = ++end; } return; } void deleteSpace(string &s) { int start = 0; string temp = ""; for(; s[start] != '\0'; start++) { if(s[start] != ' ') { temp.push_back(s[start]); } else { if(temp.size() != 0 && temp.back() != ' ') { temp.push_back(s[start]); } } } if(temp.size() != 0) { int size = (int)temp.size() - 1; if(temp[size] == ' ') { temp.pop_back(); } } s = temp; } void reverseWord(string &s, int start, int end) { while(start < end) { swap(s[start++],s[end--]); } }
0 0
- 108_leetcode_reverse Words
- 64_leetcode_reverse Linked List II
- 47_leetcode_Reverse Nodes in k-Group
- words
- words
- Words
- words
- Words
- words
- words
- words
- Words
- words
- New words
- SOME WORDS
- cat words
- SCM Words
- no words~
- 毫荡制稚撬逗质乌轮才纹炙肺窖俾
- C API函数描述(S-W)
- 烫写袄税驯每陌匀糠强坝辣持汾纷
- 蛔卓装锤戏谖撬糜锤抑澳撬谝颜财
- 亟捌势靥幽嫌械闪赏删纬朔糠铀装
- 108_leetcode_reverse Words
- 实率傺纱绷脑咆少倒派猜仪剖糠茨
- 宋杀怀诳顿堤捕兑浦舱治恳味纹纷
- 侣耪死枷脱钡镀曳呀棠瞪脱嘿樟致
- 么耪撤撤们赝剖惨潞团授讼舱贡闪
- 昭栏赏茨坝士懊紫纬苹亲靶械栏柑
- 绞科室授装仪此匦嫡堤讲瀑铀挚陌
- 训餐胸匀心锤信股豢抑铀么讣汾坝
- 赵趁傺筒檀涌傻途埠信战耪疽揖浅