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/

最新文章

  1. Scala基础语法 (一)
  2. JavaScript的个人学习随手记(三)
  3. 【SAP BO】BOE 4.1版本新特性
  4. spring-session整合
  5. Java Hour 30 Weather ( 3 )
  6. 针对应用程序池“xxxxxx”的模板永久性缓存初始化失败,解决方法
  7. 转:三十一、Java图形化界面设计——布局管理器之GridLayout(网格布局)
  8. JavaScriptSerializer类 对象序列化为JSON,JSON反序列化为对象
  9. Handler和HandlerThread
  10. vs调试时底部输出调试信息“无法查找或打开 PDB 文件”解决办法
  11. Page-Object设计模式
  12. 老李分享:android手机测试之适配(2)
  13. NOIP 2017 day -1 杂记
  14. 金蝶K3外购入库单单价取数规则调整
  15. Python进制表示及转换
  16. C++进阶--逻辑常数和比特位常数(Logical constness vs Bitwise constness)
  17. Hello 2018 A,B,C,D
  18. TL 重构
  19. 转:Java中String与byte[]的转换
  20. JavaScript数组(参考资料)

热门文章

  1. Python爬虫+颜值打分,5000+图片找到你的Mrs. Right
  2. servlet——web应用中路径问题
  3. 【译】x86程序员手册28-7.7任务地址空间
  4. daxcie
  5. 创建一个TCP服务器端通信程序的步骤
  6. mac系统,鼠标移动太慢
  7. nginx_rewrite规则介绍
  8. java8 foreach不能使用break、countinue
  9. 搜索--P1101 单词方阵
  10. Re0:DP学习之路 数塔 HDU - 2084(基础递推)