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. Django接受ajax传过来的数组

    $.ajax({ cache: false, type: "POST", url: "/userdelete/", traditional:true, //加上 ...

  2. java服务端json结果集传值给前端的数据输出格式

    在服务端输出json数据时按照一定的格式输出时间字段,fastjson支持两种方式:1.使用JSON.toJSONStringWithDateFormat方法2.JSON.toJSONString方法 ...

  3. 基于HTML5坦克大战游戏简化版

    之前我们有分享过不少经典的HTML5游戏,有些还是很有意思的,比如HTML5版切水果游戏和HTML5中国象棋游戏.今天要分享的是一款简化版的HTML5坦克大战游戏,方向键控制坦克的行进方向,空格键发射 ...

  4. jQuery+html5实现的3D动态切换焦点轮播幻灯片

    今天爱编程给网友们分享一款基于jQuery+html5实现的3D动态切换焦点轮播幻灯片,支持左右箭头和圆点按钮播放控制,支持多种不同的3D动态切换特效,自适应全屏显示,兼容360.FireFox.Ch ...

  5. C语言 · 利息计算

    算法提高 利息计算   时间限制:1.0s   内存限制:512.0MB      编制程序完成下述任务:接受两个数,一个为用户一年期定期存款金额,一个为按照百分比格式表示的利率:程序计算一年期满 后 ...

  6. [fork]Linux中的fork函数详解

    ---------------------------------------------------------------------------------------------------- ...

  7. 4种方法让SpringMVC接收多个对象 <转>

    问题背景: 我要在一个表单里同时一次性提交多名乘客的个人信息到SpringMVC,前端HTML和SpringMVC Controller里该如何处理? 第1种方法:表单提交,以字段数组接收: 第2种方 ...

  8. hdu6121 Build a tree 模拟

    /** 题目:hdu6121 Build a tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:n个点标号为0~n-1:节点i的父节点 ...

  9. Lua中的table函数库

    table.concat(table, sep,  start, end) concat是concatenate(连锁, 连接)的缩写. table.concat()函数列出参数中指定table的数组 ...

  10. 使用GitHub建立个人网站

    使用GitHub建立个人网站 1 Git简介 2 为什么使用Github Pages 3 创建Github Pages 3.1 安装git工具. 3.2 两种pages模式 3.3 创建步骤 3.4 ...