效果:

输入: "java and python"

输出: "avaj dna nohtyp"

代码:

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

  1. public class StringReverse {
  2. // 翻转一段字符串
  3. public static void swapStr(char[] arr, int begin, int end) {
  4. while (begin < end) {
  5. char tmp = arr[begin];
  6. arr[begin] = arr[end];
  7. arr[end] = tmp;
  8. begin++;
  9. end--;
  10. }
  11. }
  12.  
  13. public static String swapWords(String s) {
  14. if (s == null) {
  15. return null;
  16. }
  17. String ret = "";
  18. if (!s.endsWith(" ")) {
  19. s += " ";
  20. }
  21. char[] charArr = s.toCharArray();
  22. int begin = 0;
  23. for (int i = 0; i < charArr.length; ++i) {
  24. if (charArr[i] == ' ') {
  25. swapStr(charArr, begin, i - 1);
  26. begin = i + 1;
  27. }
  28. }
  29. ret = new String(charArr);
  30. return ret;
  31. }
  32. }

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

  1. public static String swapWords(String s) {
  2. if (s == null) {
  3. return null;
  4. }
  5. String ret = "";
  6. if (!s.endsWith(" ")) {
  7. s += " ";
  8. }
  9. char[] charArr = s.toCharArray();
  10. int begin = 0;
  11.  
  12. int i = 0;
  13. while (i < charArr.length) {
  14. while (charArr[i] == ' ' && i < charArr.length) {
  15. i++;
  16. }
  17. begin = i; // 获取单词的第一个字母对应的位置
  18. while (charArr[i] != ' ') { // 找到单词后第一个空格对应的位置
  19. i++;
  20. }
  21. swapStr(charArr, begin, i - 1);
  22. ++i;
  23. }
  24. ret = new String(charArr);
  25. return ret;
  26. }

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. find 以及linux 和windows 文件互传

    1. find  命令  查找文件或目录 同时也会用到的有 which   whereis   locate   经常也会遇到一些快捷键  ctrl  +  l  e  a  w  u  k     ...

  2. Map 按Key排序 和 按Value排序

    https://www.cnblogs.com/binz/p/6671917.html 一.根据value排序 通用方法 public class MapUtil { public static &l ...

  3. 远程登录多用户同时访问Win7系统远程桌面

    https://jingyan.baidu.com/article/ca00d56c384ea0e99febcf45.html 一,远程电脑上新增三个用户名   1 在远程电脑桌面,右击[计算机]图标 ...

  4. 【传输协议】什么是CA证书

    1.什么是CA证书. 看过一些博客,写的比较形象具体. ◇ 普通的介绍信 想必大伙儿都听说过介绍信的例子吧?假设 A 公司的张三先生要到 B 公司去拜访,但是 B 公司的所有人都不认识他,他咋办捏?常 ...

  5. SpringCloud学习

    1.SpringCloud的参考博客1 首先主要遇到的问题就是1.写好项目然后放到tomcat或者其他的容器中,然后稍微一点修改就要整个项目重新发布,非常麻烦,这就是微服务出现的契机了 基础知识 PS ...

  6. day21-22Redis Mahout

    PS: Redis 在博客的 JavaEE PS:大数据实时执行3个特性,Storm,kafka,Redis PS:比如在系统中,1s中有大量的请求涌入的系统中,那么请求就存入数据库就挂了,这就需要到 ...

  7. 从 Godaddy 转移域名到 Namesilo

    域名本来是在 Godaddy 上注册的,首付很便宜,但是续费时发现是个坑,续费一年是 102 元,再加上隐私保护 60元/年,总共一年需要 160 元,续费贵而且一点优惠也没. 对比下其他商家一年只要 ...

  8. Lua 程序设计 (Roberto,Ierusalimschy 著)

    1 开始 2 类型与值 3 表达式 4 语句 5 函数 6 深入函数 7 迭代器与泛型for 8 编译,执行与错误 9 协同程序(coroutine) 10 完整的示例 11 数据结构 12 数据文件 ...

  9. JQury基础(一)样式篇

    1 初识jQury 1.1 环境搭建 jQuery是一个JavaScript脚本库,不需要特别的安装,只需要我们在页面 标签内中,通过 script 标签引入 jQuery 库即可. <head ...

  10. IntelliJ IDEA使用心得

    前言:我原来一直使用的是Eclipse,但是发现有的教程上使用的是Intellij这个IDE,而且我发现Eclipse在Web编程上特别是页面上的自动补全上确实有些不足,而且Intellij这个软件的 ...