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. Redux 中的CombineReducer的函数详解

    combineReducers(reducers) 随着应用变得复杂,需要对 reducer 函数 进行拆分,拆分后的每一块独立负责管理 state 的一部分. combineReducers 辅助函 ...

  2. 连接服务器的mysql

    在服务器配置好Mysql 数据库,在客户端连接,报错: 解决方法: 1.在MySQL 数据库中修改user表,将host 中的localhoust 改为 %: 2.配置访问数据库的全选 根据需要配置权 ...

  3. UI/UE/ID/UED/UCD的区别(转)

    对于刚刚接触用户体验交互设计的同学来说,很多云里雾里的英文缩写,分不清各个概念代表着什么含义,今天给大家做一个简单地介绍. 简述: UI (User Interface):用户界面 UE或UX (Us ...

  4. Django之auth登录认证

    前言:我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Django作为一个完美主义者的 ...

  5. 【 jquery 】常用

    $("#input1").show('slide');    渐进显示$("#input1").hide('slide');     渐进隐藏 siblings ...

  6. springAOP源码解析-190313

    Spring相关笔记 SpringAOP讲解 子路老师讲解 spring与aspectj的区别答:它们的区别是 spring是动态加载 aspectj是静态加载,再编译过程就已经实现切面,此时会往代码 ...

  7. Xmind的使用

    Xmind是用来学习整理思维的工具

  8. 利用stylist插件,简单两步屏蔽新浪微博上的广告

    以前新浪微博只是在侧栏有几块小小的广告,还算可以接受,想着忍忍就算了,可最近真是越来越不厚道了,自从和淘宝合作之后,侧栏就开始有一大块广告根据你在淘宝的搜索记录推荐商品,更可恶的是信息流里的祛痘微博现 ...

  9. java 十五周总结

  10. python3连接mysql 稍微进阶 + 日期处理

    1.踩了个操作中文的坑,结果发现之前的文章中有强调了,在连接处加:charset="utf8" conn = pymysql.connect(host = '127.0.0.1', ...