效果:

输入: "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. 测试那些事儿—BUG

    一.作为测试人员,你应该这样报BUG: 不要对程序员说,你的代码有BUG. 他的第一反应是:1.你的环境有问题吧:2.你踏马到底会不会用? 如果你委婉的说:你这个程序和预期的不一样,你看看是不是我的方 ...

  2. Javascript 标准参考教程

    http://javascript.ruanyifeng.com/grammar/array.html

  3. (17)什么是jQuery(jq的流程和基本操作)

    jQuery就是对原生js二次封装的工具类(在jq叫构造函数) jQuery就是一堆方法的集合,jq对象就可以直接调用这些方法来完成指定的需求 使用jq的流程: 1.在HTML页面倒入jq.js文件 ...

  4. Windows下安装Python模块时环境配置

    “Win +R”打开cmd终端,如果直接在里面使用pip命令的时候,要么出现“syntax invalid”,要么出现: 'pip' is not recognized as an internal ...

  5. hdu4847 Wow! Such Doge! KMP

    Chen, Adrian (November 7, 2013). “Doge Is An Ac- tually Good Internet Meme. Wow.”. Gawker. Retrieved ...

  6. gravitee-gateway 又一个开源 apigateway

    gravitee-gateway 是Gravitee.io基于 java 开发的api gateway 功能很丰富,包含ui 界面 报警管理,访问控制管理,同时也包含一个比较完整的api 声明周期管理 ...

  7. 重写( override)and 重载(overload)

    重写override:是子类对父类的允许访问的方法的实现过程的重新编写,名字,返回值和形参都不能改变,即外壳不变,内心更改 重载overload:同一个类里面的方法,名字相同而参数不同,返回值可以相同 ...

  8. MCU ADC 进入 PD 模式后出现错误的值?

    MCU ADC 进入 PD 模式后出现错误的值? 在调试一款 MCU,最开始问题是无法读到 ADC 的值,应该是读到的值是异常高. 怀疑问题 可能是主频太低,为了降低功耗,这个 MCU 主频被我降了很 ...

  9. Electron-vue 新建Demo

    vue init simulatedgreg/electron-vue Test(项目名)

  10. pyquery的使用

    常用的三种初始化方法: 1.字符串初始化: from pyquery import PyQuery as pq html=""" <html> <hea ...