[LC] 557. Reverse Words in a String III
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.
class Solution {
public String reverseWords(String s) {
if (s == null || s.length() == 0) {
return s;
}
int slow = -1;
char[] charArr = s.toCharArray();
for (int i = 0; i < charArr.length; i++) {
if (charArr[i] != ' ' && (i == 0 ||charArr[i - 1] == ' ')) {
slow = i;
}
if (charArr[i] != ' ' && (i == charArr.length - 1 || charArr[i + 1] == ' ')) {
reverse(slow, i, charArr);
}
}
return new String(charArr);
} private void reverse(int start, int end, char[] charArr) {
while (start <= end) {
char tmp = charArr[start];
charArr[start] = charArr[end];
charArr[end] = tmp;
start += 1;
end -= 1;
}
}
}
[LC] 557. Reverse Words in a String III的更多相关文章
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String
557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 557. Reverse Words in a String III【easy】
557. Reverse Words in a String III[easy] Given a string, you need to reverse the order of characters ...
- 【leetcode_easy】557. Reverse Words in a String III
problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...
- Week4 - 500.Keyboard Row & 557.Reverse Words in a String III
500.Keyboard Row & 557.Reverse Words in a String III 500.Keyboard Row Given a List of words, ret ...
- 557. Reverse Words in a String III 翻转句子中的每一个单词
[抄题]: Given a string, you need to reverse the order of characters in each word within a sentence whi ...
- [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- LeetCode 557 Reverse Words in a String III 解题报告
题目要求 Given a string, you need to reverse the order of characters in each word within a sentence whil ...
随机推荐
- request对象和response对象的作用和相关方法
response对象(响应) 响应行 状态码 :setStatus(int a) 设置状态码 302重定向 304控制缓存 响应头 setHeader() 一个key对应一个value addHead ...
- 牛牛的DRB迷宫(DP、二进制编码器)
牛牛的DRB迷宫I 链接:https://ac.nowcoder.com/acm/contest/3004/A来源:牛客网 题目描述 牛牛有一个n*m的迷宫,对于迷宫中的每个格子都为'R','D',' ...
- Mac OS 终端利器 iTerm2配置大全
之前一直使用 Mac OS 自带的终端,用起来虽然有些不太方便,但总体来说还是可以接受的,是有想换个终端的想法,然后今天偶然看到一个终端利器 iTerm2,发现真的很强大,也非常的好用,按照网上配置了 ...
- 直击JDD | 王振辉:技术是驱动物流革新的第一要素
"从物流基础设施的大规模智能迭代到产业供应链的数字化升级,物流行业从大而重变得'举重若轻',技术是驱动物流革新的第一要素."11月19日,京东物流集团CEO王振辉在2019京东全球 ...
- Python使用+和*操作符 连接2个列表和列表的复制
+ 操作符通常连接两个列表可以使用 +进行连接得到一个新列表 *操作符择可以用于一个列表和一个整数,实现列表的复制.
- php速成_day2
一.PHP中的多维数组 1.多维数组及其用途 多维数组用来存储多个元素,元素是一个数组的结构. 之前学习的数组,是一个一维数组. $person = array( 'name' => 'xiao ...
- UVA 558 SPFA 判断负环
这个承认自己没看懂题目,一开始以为题意是形成环路之后走一圈不会产生负值就输出,原来就是判断负环,用SPFA很好用,运用队列,在判断负环的时候,用一个数组专门保存某个点的访问次数,超过了N次即可断定有负 ...
- PAT A1133 Splitting A Linked List (25) [链表]
题目 Given a singly linked list, you are supposed to rearrange its elements so that all the negative v ...
- python数据预处理for knn
机器学习实战 一书中第20页数据预处理,从文本中解析数据的程序. import numpy as np def dataPreProcessing(fileName): with open(fileN ...
- 关于PIL库Image模块的一些测试代码
为了加深理解,写了一些代码测试,在这里记录一下吧: 关于图片的模式问题,之前做过笔记,有“1”,“L”,"P","RGB","RGBA",& ...