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的更多相关文章

  1. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

  2. 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 ...

  3. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  4. 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 ...

  5. 【leetcode_easy】557. Reverse Words in a String III

    problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. UML-状态机图和建模

    1.目标:如何画状态机图 2.定义:描述某个对象的状态.感兴趣的事件.以及对象响应该事件的行为. 转换:用箭头表示 状态:用圆角矩形表示 事件:指的是一件值得注意的事情的发生.如:拿起电话. 当事件“ ...

  2. 安装lombok插件IDEA的插件栏加载不出来

    打开 Setting-->Appearance & Behavior -->Syetem Setting -->Updates,将Use secure connection  ...

  3. IO流处理文件读取到字节数组,再从字节数组读取到文件,Java实现

    package cn.zhang.io; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; impo ...

  4. [极客大挑战 2019]Knife

    根据题目Knife 猜想尝试用蚁剑连接 http://40b92ebd-5234-40b7-b2e0-3c42fb5ad000.node3.buuoj.cn/?Knife.php 密码:Syc 找到f ...

  5. 1811 06 pygame 的继续开发

    早上看了  高数和python   好像系统没有保存  桑心啊 关于游戏背景的制作 游戏背景就是    背景在移动  而主人物还在原位置的    常常用于跑酷游戏类  背景开始绘制两张图像  一张完全 ...

  6. TCP/IP与IETF的RFC

    究竟是谁控制着 TCP/IP协议族,又是谁在定义新的标准以及其他类似的事情?事实上, 有四个小组在负责Internet技术. 1) Internet协会(ISOC,Internet Society)是 ...

  7. 约数个数函数(d)的一个性质证明

    洛谷P3327 [SDOI2015]约数个数和 洛谷P4619 [SDOI2018]旧试题 要用到这个性质,而且网上几乎没有能看的证明,所以特别提出来整理一下. \[ d(AB) = \sum_{x| ...

  8. 基本 Python 词汇

    本文档介绍了要理解“使用 Python 进行地理处理”的帮助文档需要掌握的一些词汇. !   术语 说明 Python Python 是由 Guido van  Rossum 在上世纪八十年代末构想并 ...

  9. 饭卡(DP)

    电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额. 如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够).所以大 ...

  10. catalina.out日志膨胀问题解决实例,日志门面commons-logging的实践

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 笔者在公司的时候,遇到一个问题,2个模块A.B Tomcat中的catalina.out及catalina ...