问题来源:https://oj.leetcode.com/problems/reverse-words-in-a-string/

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

Clarification:

  • What constitutes a word?
    A sequence of non-space characters constitutes a word.
  • Could the input string contain leading or trailing spaces?
    Yes. However, your reversed string should not contain leading or trailing spaces.
  • How about multiple spaces between two words?
    Reduce them to a single space in the reversed string.
  • 我的实现代码:(开始看错了题目要求,以为就是一个字符串反转,直接用了new StringBuffer(s).reverse().toString(),后来发现不是一个简单的所有字符反转,而是单词顺序反转,但是单词还是那个单词!)
[java] view plaincopy
  1. public class Solution {
  2. public String reverseWords(String s) {
  3. int length = s.length();
  4. StringBuffer buff = new StringBuffer();
  5. StringBuffer sb = new StringBuffer();
  6. for (int i = length; i > 0; i--) {
  7. if (s.charAt(i - 1) == ' ') {
  8. buff.append(sb.reverse().toString());
  9. sb = new StringBuffer();
  10. buff.append(' ');
  11. while (i-1 > 0) {
  12. if (s.charAt(i - 2) == ' ') {
  13. i--;
  14. } else
  15. break;
  16. }
  17. } else {
  18. sb.append(s.charAt(i-1));
  19. }
  20. }
  21. buff.append(sb.reverse().toString());
  22. return buff.toString().trim();
  23. }
  24. public static void main(String[] args) {
  25. Scanner scan = new Scanner(System.in);
  26. String s = scan.nextLine();
  27. System.out.println(new Solution().reverseWords(s));
  28. }
  29. }

leetcode-reverse words in a string相关推荐

  1. LeetCode Reverse Vowels of a String

    原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...

  2. LeetCode Reverse Words in a String III

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string ...

  3. LeetCode Reverse Vowels of a String(字符串中元音字符反转)

    题意:给出一个字符串,仅将元音字符反转 思路:设置两点,一个从左开始,一个从右开始,分别找到元音字符,然后替换 代码如下: public class Solution {private boolean ...

  4. Reverse Vowels of a String (反转字符串中的母音)

    leetcode Reverse Vowels of a String 反转字符串中的母音 一.学习要点: 1.find_first_of:查找与字符串str中某个字符相同的位置,并返回他的第一个出现 ...

  5. leetcode 151 Reverse Words in a String (python)

    leetcode 151   Reverse Words in a String 题目描述: Given an input string, reverse the string word by wor ...

  6. leetcode day1 -- Reverse Words in a String Evaluate Reverse Polish Notation Max Points on a Li

    以前从来没做过什么oj,发现做oj和在本地写代码或者纸上写差别还是很大的,觉得今天开始刷oj,特此记录一下. 1.Reverse Words in a String Given an input st ...

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

  8. 345. Reverse Vowels of a String - LeetCode

    Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...

  9. LeetCode 151. Reverse Words in a String

    151. Reverse Words in a String Given an input string, reverse the string word by word. For example, ...

  10. LeetCode345. Reverse Vowels of a String

    345. Reverse Vowels of a String My Submissions QuestionEditorial Solution Total Accepted: 3821 Total ...

最新文章

  1. 对话G7 CEO翟学魂:收入30亿仍亏损是为研发,上市不是目前主要考虑的事情
  2. JLink接口的SWD接法
  3. 最全 JavaScript Array 方法 详解
  4. 服务器$java_top路径_ERP实施项目-MD120_FIN000_即时打印_V1.0
  5. C++之对象的动态建立和释放
  6. 外媒:英特尔未来10年可能投资950亿美元在欧洲新建8家芯片厂
  7. linux 进程状态 load,Linux - 系统指标 CPU load
  8. shell脚本批量生成配置文件
  9. wow mysql dbc_关于WOW的DBC文件内容的介绍
  10. 1口百兆光纤收发器工业导轨式发送机接收机1百兆光1百兆电工业以太网光纤收发器
  11. 中国国家航天局:探月工程四期还有三次任务
  12. 我搭的神经网络不 work 该怎么办!看看这 11 条新手最容易犯的错误
  13. 查看新增串口、USB设备
  14. Excel进度条设置百分比解决
  15. 没想到华为P20的投屏功能,还可以这么强大!
  16. 华夏旅游CMS商城系统操作手册
  17. python关键词共现图谱_如何用知网导出的关键词 几秒 生成共现矩阵及图谱 》完整版...
  18. PhotoShop 安装PS时提示安装程序检测到计算机重新启动操作可能处于挂起状态,建议您退出安装...
  19. MySQL数据库-表的插入详解
  20. 给js添加类名/添加元素标签/

热门文章

  1. Python类的调用之同一个文件里面
  2. 从Excel文件中找出在TXT文件中没有出现的 行之_代码片段
  3. ucos ii 源代码中文注释详解 : OS_TIME.C
  4. 计算机网络8832,2021年4月份自学考试计算机网络原理04741答案.doc
  5. java数青蛙_『字节跳动LeetCode联合周赛』--周赛185(JavaScript) | OFEII
  6. qc成果报告范例_株洲水务集团QC成果再获省一等奖
  7. 疑似iQOO Neo6通过3C认证:最高支持80W快充
  8. 泡泡玛特回应与肯德基联名引发炒作:暂停食品行业的定制盲盒业务
  9. 绿屏后再现“粉屏”门!大量网友投诉iPhone 13粉屏问题 客服:非硬件问题
  10. 小米平板5或无缘MIUI 13:搭配骁龙870 预装MIUI 12.5系统