效果:

输入: "java and python"

输出: "avaj dna nohtyp"

代码:

版本1: 不考虑字符串开头有空格,单词间有多个空格空格的情况

public class StringReverse {
// 翻转一段字符串
public static void swapStr(char[] arr, int begin, int end) {
while (begin < end) {
char tmp = arr[begin];
arr[begin] = arr[end];
arr[end] = tmp;
begin++;
end--;
}
} public static String swapWords(String s) {
if (s == null) {
return null;
}
String ret = "";
if (!s.endsWith(" ")) {
s += " ";
}
char[] charArr = s.toCharArray();
int begin = 0;
for (int i = 0; i < charArr.length; ++i) {
if (charArr[i] == ' ') {
swapStr(charArr, begin, i - 1);
begin = i + 1;
}
}
ret = new String(charArr);
return ret;
}
}

版本2:考虑开头的空格,单词间有多个空格

    public static String swapWords(String s) {
if (s == null) {
return null;
}
String ret = "";
if (!s.endsWith(" ")) {
s += " ";
}
char[] charArr = s.toCharArray();
int begin = 0; int i = 0;
while (i < charArr.length) {
while (charArr[i] == ' ' && i < charArr.length) {
i++;
}
begin = i; // 获取单词的第一个字母对应的位置
while (charArr[i] != ' ') { // 找到单词后第一个空格对应的位置
i++;
}
swapStr(charArr, begin, i - 1);
++i;
}
ret = new String(charArr);
return ret;
}

java翻转字符串中的单词的更多相关文章

  1. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  2. [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  3. [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  4. [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III

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

  5. [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

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

  7. LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)

    题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...

  8. [Swift]LeetCode186. 翻转字符串中的单词 II $ Reverse Words in a String II

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  9. [LintCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

随机推荐

  1. 20155219实验三 敏捷开发与XP实践

    实验内容 XP基础 XP核心实践 相关工具 敏捷开发与XP 1.敏捷开发(Agile Development)是一种以人为核心.迭代.循序渐进的开发方法."敏捷流程"是一系列价值观 ...

  2. HDU 1160:FatMouse's Speed(LIS+记录路径)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. Thread sleep() wait()

    学艺不精,先总结一下两者的区别: 对比项 wait sleep 类所属 Object Thread,static方法 使用 在synchronised block中(包括notify,notifyAl ...

  4. Autofac解耦事件总线

    事件总线之Autofac解耦 事件总线是通过一个中间服务,剥离了常规事件的发布与订阅(消费)强依赖关系的一种技术实现.事件总线的基础知识可参考圣杰的博客[事件总线知多少] 本片博客不再详细概述事件总线 ...

  5. java-为什么非静态内部类中不能有static修饰的属性,但却可以有final常量?

    - 例: public class Demo{ int x; class A{ static int a = 0; //这样写是不合法的. static final int b=0; //这样写是合法 ...

  6. 03 事务,连接池DBCP,C3P0,DBUtils

    事务 Transaction  其实指的一组操作,里面包含许多个单一的逻辑.只要有一个逻辑没有执行成功,那么都算失败. 所有的数据都回归到最初的状态(回滚) 事务的作用:为了确保逻辑的成功. 例子: ...

  7. Python知识点整理,基础1 - 基本语法

  8. jenkins 使用smtp2http 邮件服务,扩展灵活的构建通知功能

    smtp2http 是一个很方便的可以将smtp 转换为http 服务的工具,同时也支持扩展的开发,我们可以使用此工具 扩展灵活的ci.cd 生命周期管理,而不是简单的邮件处理 备注: 使用docke ...

  9. Centos 7 Saltstack 集群

    一. Saltstack  双master master1 -------------------master2 | minion master1 1.yum -y install  salt-mas ...

  10. Zookeeper 基本概念学习

    1.Zookeeper简介: 1) 中间件,为分布式系统提供协调服务 2) 作用于分布式系统,发挥其优势,可以为大数据服务 3) 支持Java,提供java和c语言的客户端api 2. 分布式系统 1 ...