557. Reverse Words in a String III
来源:互联网 发布:ubuntu mate 15.04 编辑:程序博客网 时间:2024/05/24 02:47
题目:
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest"Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
思路:用空格来判断一个单词的结束和开始,用front(初始化为0) 来记录一个一个单词的开始,用i来找这个单词的结束,然后对这个单词进行反转操作;然后另front = i+1,再寻找下个单词,对这个单词进行反转操作,这样直到字符串的末尾。代码;
class Solution {
public:
string reverseWords(string s) {
int size1 = s.size();
int front = 0;
for(int i = 0; i<=size1;i++)
{
if(i == size1 || s[i] == ' ')
{
for(int j = 0; j< (i-1 - front)/2;j++)
{
char temp = s[front+j];
s[front+j] = s[i-1-j];
s[i-1-j] = temp;
}
front = i+1;
}
}
return s;
}
};
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 557. Reverse Words in a String III
- 【问题解决】Mac上Android环境变量配置后Jenkins打包找不到环境变量问题
- 算法分析与设计第十五周:593. Valid Square
- Solr的日志配置
- websocket客户端的代码
- ffmpeg的时基
- 557. Reverse Words in a String III
- 对象审计+查看审计内容+查看对哪些用户对象进行对象审计+取消对象审计
- lintcode(182)删除数字
- linux下安装Nginx
- JAVA-JDBC: (1) java连接数据库的粗略步骤
- 通讯录管理系统终章
- System.getProperty
- 【C/C++】解决“不是有效的win32应用程序”思路
- Android构造函数有没有返回值?