leetcode.557.Reverse Words in a String III
来源:互联网 发布:java 接口 编辑:程序博客网 时间:2024/05/21 10:03
Description
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.
sln
c++的标准库中有提供一个reverse函数,能够对字符串做反转。我们的题目要求我们将每个单词进行反转,但不对句子进行反转,所以我们只要把这个问题分解成多个单词反转的问题即可。顺序遍历字符串,每次遇到空格就把该上一个空格开始到这一个空格前的子串进行反转即可。c++实现如下:
class Solution {public: string reverseWords(string s) { int last_space = 0; for (int i = 0; i <= s.length(); ++i) { if (i == s.length() || s[i] == ' ') { reverse(&s[last_space], &s[i]); last_space = i + 1; } } return s; }};
阅读全文
0 0
- leetCode---Reverse Words in a String III
- leetcode-Reverse Words in a String III
- Reverse Words in a String III(leetcode)
- leetcode题解-151. Reverse Words in a String && 557. Reverse Words in a String III
- Leetcode-557. Reverse Words in a String III
- 【Leetcode】557. Reverse Words in a String III
- leetcode 557. Reverse Words in a String III
- LeetCode之路:557. Reverse Words in a String III
- Leetcode 557. Reverse Words in a String III
- LeetCode 557. Reverse Words in a String III
- leetcode 557. Reverse Words in a String III(easy)
- [LeetCode]557. Reverse Words in a String III
- [leetcode] 557. Reverse Words in a String III
- leetcode 557. Reverse Words in a String III
- [leetcode]: 557. Reverse Words in a String III
- LeetCode 557. Reverse Words in a String III
- [leetcode]557. Reverse Words in a String III
- [leetcode: Python]557. Reverse Words in a String III
- vuex怎么学(一)
- 【原创】KM算法 ※HDU 2225 POJ 2195
- 使用openssl进行RSA加密解密
- C#自己编写软键盘
- CentOS7安装Python3.6
- leetcode.557.Reverse Words in a String III
- 设计模式(对象创建)——工厂方法模式
- 高级项目管理师公开课举行
- hdoj 1001
- 二维码识别开源项目zxing的使用和源码分析
- 字符串
- Spring Boot Junit 测试Controller
- 给初学者的RxJava2.0教程(六)
- docker部署mitmproxy代理抓包(五)