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.

package leedcode;

class Solution {

    public static void main(String[] args) {
// String s = "Let's take LeetCode contest";
String s = "12345";
System.out.println(s);
System.out.println(new Solution().reverseWords(s));
} public static void reverseWord(char[] chars, int begin, int end) { for (int i = begin; i <= end; i++) {
char temp = chars[i];
chars[i] = chars[end];
chars[end] = temp;
end--;
} } public String reverseWords(String s) { char[] chars = s.toCharArray();
int start = 0;
int end = 0; for (int i = 0; i < chars.length; i++) { if (chars[i] == ' ') {
end = i;
reverseWord(chars, start, end-1);
start = end+1;
}
end++;
} reverseWord(chars,start,end-1); return new String(chars);
}
}

重新整理逻辑

class Solution {

      public static void reverseWord(char[] chars, int begin, int end) {

        for (int i = begin; i <= end; i++) {
char temp = chars[i];
chars[i] = chars[end];
chars[end] = temp;
end--;
} } public String reverseWords(String s) { char[] chars = s.toCharArray();
int start = 0;
int end = 0; for (int i = 0; i < chars.length; i++) {
if (chars[i] == ' ') {
end = i;
reverseWord(chars, start, end-1);
start = end+1;
}
end++;
} reverseWord(chars,start,end-1); return new String(chars);
}
}

ledecode Reverse Words in a String III的更多相关文章

  1. 53. leetcode557. Reverse Words in a String III

    557. Reverse Words in a String III Given a string, you need to reverse the order of characters in ea ...

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

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

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

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

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

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

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

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

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

  8. [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三

    Given a string, you need to reverse the order of characters in each word within a sentence while sti ...

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

随机推荐

  1. dp之01背包hdu3466(带限制的,当你所拥有的钱数大于某个限定值时才可以购买该物品)

    题意:买东西,每个东西有三个特征值,p代表价格,q代表你手中钱必须不低于q才能买这个物品,v代表得到的价值. mark:又是变种01背包,每做一个变种的,就是一种提高.. 按照q - p以由大到小的顺 ...

  2. js数组最大值和最小值计算

    基本概念 reduce() 方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值. reduce 为数组中的每一个元素依次执行回调函数,不包括数组中被 ...

  3. oracle 命令 登录

    1.打开终端,输入命令:sqlplus /nolog 2.连接数据库:conn user_name/passwd@db_name

  4. 关于js中__proto__和prototype的一些理解<转>

    var Person = function(name) {     this.name = name; } var p = new Person(); new操作符的操作是 var p = {} p. ...

  5. [转][Python基础]Python中的Lambda表达式

    引用自:http://www.cnblogs.com/evening/archive/2012/03/29/2423554.html 在学习python的过程中,lambda的语法时常会使人感到困惑, ...

  6. msyql的内存计算

    本文将讨论MySQL内存相关的一些选项,包括: 单位都是b,不是kb,即1B=1/(1024*1024*1024)G 1)全局的buffer,如innodb_buffer_pool_size: 2)线 ...

  7. NPOI导入Excel日期格式的处理 - 附类型格式匹配表

    传统操作Excel方法在部署的时候遇到很多问题,如目标主机需要安装Excel.64位电脑不支持.需要安装相关驱动程序等.所以我们一般会使用开源的NPOI来替代传统的Excel操作方法,NPOI的优点是 ...

  8. 学会读JQuery等JS插件源码

    看了 http://my249645546.iteye.com/blog/1716629 上的这篇文章感觉挺好的,所以决定转过来,谢谢这位博主. 很多人觉得jquery.ext等一些开源js源代码 十 ...

  9. jQuery替换内容

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  10. jQuery制作简洁的多级联动Select下拉框

    今天我们要来分享一款很实用的jQuery插件,它是一个基于jQuery多级联动的省市地区Select下拉框,并且值得一提的是,这款联动下拉框是经过自定义美化过的,外观比浏览器自带的要漂亮许多.另外,这 ...