leetcode-reverse words in a string
问题来源: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
".
- 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(),后来发现不是一个简单的所有字符反转,而是单词顺序反转,但是单词还是那个单词!)
- public class Solution {
- public String reverseWords(String s) {
- int length = s.length();
- StringBuffer buff = new StringBuffer();
- StringBuffer sb = new StringBuffer();
- for (int i = length; i > 0; i--) {
- if (s.charAt(i - 1) == ' ') {
- buff.append(sb.reverse().toString());
- sb = new StringBuffer();
- buff.append(' ');
- while (i-1 > 0) {
- if (s.charAt(i - 2) == ' ') {
- i--;
- } else
- break;
- }
- } else {
- sb.append(s.charAt(i-1));
- }
- }
- buff.append(sb.reverse().toString());
- return buff.toString().trim();
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String s = scan.nextLine();
- System.out.println(new Solution().reverseWords(s));
- }
- }
leetcode-reverse words in a string相关推荐
- LeetCode Reverse Vowels of a String
原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...
- LeetCode Reverse Words in a String III
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string ...
- LeetCode Reverse Vowels of a String(字符串中元音字符反转)
题意:给出一个字符串,仅将元音字符反转 思路:设置两点,一个从左开始,一个从右开始,分别找到元音字符,然后替换 代码如下: public class Solution {private boolean ...
- Reverse Vowels of a String (反转字符串中的母音)
leetcode Reverse Vowels of a String 反转字符串中的母音 一.学习要点: 1.find_first_of:查找与字符串str中某个字符相同的位置,并返回他的第一个出现 ...
- 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 ...
- 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 ...
- 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 ...
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- 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, ...
- LeetCode345. Reverse Vowels of a String
345. Reverse Vowels of a String My Submissions QuestionEditorial Solution Total Accepted: 3821 Total ...
最新文章
- 对话G7 CEO翟学魂:收入30亿仍亏损是为研发,上市不是目前主要考虑的事情
- JLink接口的SWD接法
- 最全 JavaScript Array 方法 详解
- 服务器$java_top路径_ERP实施项目-MD120_FIN000_即时打印_V1.0
- C++之对象的动态建立和释放
- 外媒:英特尔未来10年可能投资950亿美元在欧洲新建8家芯片厂
- linux 进程状态 load,Linux - 系统指标 CPU load
- shell脚本批量生成配置文件
- wow mysql dbc_关于WOW的DBC文件内容的介绍
- 1口百兆光纤收发器工业导轨式发送机接收机1百兆光1百兆电工业以太网光纤收发器
- 中国国家航天局:探月工程四期还有三次任务
- 我搭的神经网络不 work 该怎么办!看看这 11 条新手最容易犯的错误
- 查看新增串口、USB设备
- Excel进度条设置百分比解决
- 没想到华为P20的投屏功能,还可以这么强大!
- 华夏旅游CMS商城系统操作手册
- python关键词共现图谱_如何用知网导出的关键词 几秒 生成共现矩阵及图谱 》完整版...
- PhotoShop 安装PS时提示安装程序检测到计算机重新启动操作可能处于挂起状态,建议您退出安装...
- MySQL数据库-表的插入详解
- 给js添加类名/添加元素标签/
热门文章
- Python类的调用之同一个文件里面
- 从Excel文件中找出在TXT文件中没有出现的 行之_代码片段
- ucos ii 源代码中文注释详解 : OS_TIME.C
- 计算机网络8832,2021年4月份自学考试计算机网络原理04741答案.doc
- java数青蛙_『字节跳动LeetCode联合周赛』--周赛185(JavaScript) | OFEII
- qc成果报告范例_株洲水务集团QC成果再获省一等奖
- 疑似iQOO Neo6通过3C认证:最高支持80W快充
- 泡泡玛特回应与肯德基联名引发炒作:暂停食品行业的定制盲盒业务
- 绿屏后再现“粉屏”门!大量网友投诉iPhone 13粉屏问题 客服:非硬件问题
- 小米平板5或无缘MIUI 13:搭配骁龙870 预装MIUI 12.5系统