逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::
package com.tur.demo;

public class Hello {
    public static void main(String[] args) {
        System.out.println(reverseWords("How are you!"));
        System.out.println(reverseWords("do justice to a dinner"));
        System.out.println(reverseWords(""));
        System.out.println(reverseWords("A"));
        System.out.println(reverseWords("A Biao"));
        System.out.println(reverseWords("!A Biao C."));
    }

    public static String reverseWords(String str) {
        int start = 1;
        int end = 0;
        char[] chs = str.toCharArray();

        // 1. 先逆转整个字符串
        reverseCharactersInRange(chs, 0, chs.length - 1);

        // 2. 再逆转逆转后的字符数组中组成单词的字符
        for (int i = 0; i < chs.length; ++i) {
            if (Character.isLetter(chs[i])) {
                // 找到组成单词的字符的起始和结束位置
                if (start > end) {
                    start = end = i;
                } else {
                    ++end;
                }
            } else {
                if (start < end) {
                    reverseCharactersInRange(chs, start, end);
                }
                start = chs.length;
            }
        }

        if (start < end) {
            reverseCharactersInRange(chs, start, end);
        }

        return new String(chs);
    }

    public static void reverseCharactersInRange(char[] chs, int start, int end) {
        int times = (end - start + 1) / 2;
        for (int i = 0; i < times; ++i) {
            char temp = chs[start + i];
            chs[start + i] = chs[end - i];
            chs[end - i] = temp;
        }
    }
}
posted on 2012-11-22 20:20 逛奔的蜗牛 阅读(517) 评论(0)  编辑 收藏 引用 所属分类: Java

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理