题目如下:
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
class Solution { public: void reverseWords(string &s) { if(s.empty()) return; int count = 0; int index = 0, indexTemp = 0, begin = 0, end = s.length()-1; while(s[begin] == ' ' && begin<s.length()-1) begin++; while(s[end] == ' ' && end>0) end--; if(end == 0 && s[end] == ' ') { s = ""; return; } else if(end == 0) { s = s.substr(0,1); return; } index = begin; while(index <= end) { if(s[index] == ' ') { count++; while(s[index] == ' ') index++; } count++; index++; } string temp(count,'\0'); index = end; indexTemp = 0; while(index >= begin) { if(s[index] == ' ') { temp[indexTemp] = s[index]; while(s[index] == ' ') index--; indexTemp++; } temp[indexTemp] = s[index]; indexTemp++; index--; } indexTemp = 0; begin = -1; end = -1; while(indexTemp < count) { if(temp[indexTemp] != ' ' && begin == -1) { begin = 0; } else if(indexTemp-1 >= 0 && temp[indexTemp-1] == ' '&&temp[indexTemp] != ' ') { begin = indexTemp; } else if((indexTemp+1 < count && temp[indexTemp+1] == ' ' && temp[indexTemp] != ' ') || ((indexTemp == count-1)&&temp[indexTemp] != ' ')) { end = indexTemp; reverse(temp, begin, end); } indexTemp++; } s = temp; } void reverse(string &s, int begin, int end) { while(begin < end) { char temp = s[begin]; s[begin] = s[end]; s[end] = temp; begin++; end--; } } };
对于ios7扫描二维码功能的实现,布布扣,bubuko.com
原文地址:http://blog.csdn.net/lyhuzhu/article/details/28716407