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. JSF request参数传递

    转载自:http://blog.csdn.net/duankaige/article/details/6711044 1:JSF页面之间传参 方法1: <h:outputLink value=& ...

  2. Unix系统编程()malloc和free的实现

    尽管malloc和free所提供的内存分配接口比之brk和sbrk要容易许多,但在使用时仍然容易犯下各种编程错误. 理解malloc和free的实现,将使我们洞悉产生这些错误的原因以及如何才能避免此类 ...

  3. PHP——0128练习相关2——js点击button按钮跳转到另一个新页面

    js点击button按钮跳转到另一个新页面 投稿:whsnow 字体:[增加 减小] 类型:转载 时间:2014-10-10我要评论 点击按钮怎么跳转到另外一个页面呢?点击图片要跳转到新的页面时,怎么 ...

  4. asp.net 省市联级代码

    3种代码 asp.net js 和数据库的 都测试通过了..例子在http://download.csdn.net/download/jine515073/6878157

  5. 关于跨平台的理解以及Unity的由来--Unity学习

    1.在2000到2003年的时候,掀起了一阵Java跨平台的浪潮,Java本来是在自己的Java机器上运行的,这时候出现了一个叫Java虚拟机的东西,Java虚拟机可以在Windows系统中运行,这样 ...

  6. TensorFlow基础笔记(5) VGGnet_test

    参考 http://blog.csdn.net/jsond/article/details/72667829 资源: 1.相关的vgg模型下载网址 http://www.vlfeat.org/matc ...

  7. shell命令技巧——文本去重并保持原有顺序

    简单来说,这个技巧相应的是例如以下一种场景 假设有文本例如以下 cccc aaaa bbbb dddd bbbb cccc aaaa 如今须要对它进行去重处理.这个非常easy,sort -u就能够搞 ...

  8. Linux下Vi/Vim的使用方法

    本文介绍了vi (vim)的基本使用方法,但对于普通用户来说基本上够了!i/vim的区别简单点来说,它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特 ...

  9. MFC 单选按钮Radio使用注意

    使用MFC Radio时遇到问题:数据交换时出现断言崩溃框 定位于: 解决方法: 1.按CTRL+D,保证同一组内的radio的tab序号是连续的: 2.同一组内,设置 radio1的属性:  gro ...

  10. docker实用命名

    删除tag/镜像: #删除tag docker rmi index-dev.qiniu.io/cs-kirk/nginx:latest docker rmi index-dev.qiniu.io/cs ...