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.


题目标签:String

  题目给了我们一串string,让我们把每一个word 给reverse一下。

  设p1 和 p2,每一次让p2 找到 “ ” space,然后 reverse p1 和 p2 之间的 word,然后更新p1 和 p2 的值。

  具体请看code。

Java Solution:

Runtime beats  96.43%

完成日期:05/31/2018

关键词:Two pointers

关键点:反转p1 和 p2 之间的 word

 class Solution
{
public String reverseWords(String s)
{
char [] arr = s.toCharArray();
int p1 = 0;
int p2 = 0; while(p1 < arr.length)
{
// let p2 find the space
while(p2 < arr.length && arr[p2] != ' ')
p2++; // once p2 find the space, do reverse between p1 and p2
int left = p1;
int right = p2 - 1; while(left < right)
{
char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp; left++;
right--;
} // reset p1 p2
p1 = p2 + 1;
p2++; } return new String(arr);
} }

参考资料:n/a

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 557. Reverse Words in a String III (反转字符串中的单词 III)的更多相关文章

  1. [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  2. [leetcode]151. Reverse Words in a String翻转给定字符串中的单词

    Given an input string, reverse the string word by word. Example: Input: "the sky is blue", ...

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

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

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

  5. C#版(击败97.76%的提交) - Leetcode 557. 反转字符串中的单词 III - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...

  6. Java实现 LeetCode 557 反转字符串中的单词 III(StringBuilder的翻转和分割)

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

  7. Leetcode 557.反转字符串中的单词III

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

  8. LeetCode557 反转字符串中的单词 III

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

  9. LeetCode 557:反转字符串中的单词 III Reverse Words in a String III

    公众号:爱写bug(ID:icodebugs) 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. Given a string, you need to reve ...

随机推荐

  1. windows server 2008 如何查看异常重启日志

    下面蓝队网络为大家介绍下windows server 2008 如何查看异常重启日志 开始->管理工具->时间查看器 windows日志->系统 筛选当前日志 选择Kernel-Po ...

  2. Swift mutating Equatable Hashable 待研究

    Swift mutating Equatable Hashable 待研究

  3. [iOS]查看苹果支持的所有字库

    有时候,在开发的时候,想换个字体更加优雅的显示.但是不知道苹果本身到底支持了那些字体,怎么办? 没关系,打开这个网址:http://iosfonts.com/ 所见即所得,非常方便,而且还表明了从那个 ...

  4. 系统异常 NSException.h

    FOUNDATION_EXPORT NSExceptionName const NSGenericException; FOUNDATION_EXPORT NSExceptionName const ...

  5. 并发编程学习笔记(6)----公平锁和ReentrantReadWriteLock使用及原理

    (一)公平锁 1.什么是公平锁? 公平锁指的是在某个线程释放锁之后,等待的线程获取锁的策略是以请求获取锁的时间为标准的,即使先请求获取锁的线程先拿到锁. 2.在java中的实现? 在java的并发包中 ...

  6. Flask框架 之第一个Flask程序

    from flask import Flask # 创建flask应用对象 # __name__ 代表当前模块名称 # flask以当前目录为总目录,static目录为静态目录,templates为模 ...

  7. mac os 10.10解决pod问题

    转一下 http://leancodingnow.com/how-to-get-cocoapods-work-on-yosemite/

  8. 第四章 模块化React和Redux应用

    第四章 模块化React和Redux应用 4.1 模块化应用要点 构建一个应用的基础: 代码文件的组织结构: 确定模块的边界: Store的状态树设计. 4.2 代码文件的组织方式 4.2.1 按角色 ...

  9. Number 数据类型

    //Number 数据类型//包含 整数 小数 NaN(not a number)var a = 1233;var b = 12.34;//1/'a'//把其他数据类型转化成数字,他在转化时,只要字符 ...

  10. layui 动态表格之合并单元格

    需求: 下面用excel表格大概模拟下需求,左边是原来的,要改成右边这样的: ①第一步:再生成表格后调用此方法,以合并重复的单元格 done : function(res, curr, count) ...