[leetcode]151. Reverse Words in a String@Java解题报告
来源:互联网 发布:儒道数据分析地址 编辑:程序博客网 时间:2024/05/20 04:32
https://leetcode.com/problems/reverse-words-in-a-string/tabs/description
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.
click to show clarification.
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.
思路
这道题让我们翻转字符串中的单词,题目中给了我们写特别说明,如果单词之间遇到多个空格,只能返回一个,而且首尾不能有单词,并且对C语言程序员要求空间复杂度为O(1),我是用Java写的,就不考虑这个问题了。
先整个字符串整体翻转一次,然后再分别翻转每一个单词(或者先分别翻转每一个单词,然后再整个字符串整体翻转一次),此时就能得到我们需要的结果了。代码如下:
package go.jacob.day729;/** * 151. Reverse Words in a String * * @author Jacob * */public class Demo2 {public String reverseWords(String s) {if (s == null || s.length() < 2)return s;char[] arr = s.toCharArray();int left = 0, right = s.length() - 1;// 完全翻转reverse(arr,left,right);//翻转单个单词reverseWord(arr);//空格处理return cleanSpaces(arr);}private String cleanSpaces(char[] arr) {int n=arr.length;int i=0,j=0;while(j<n){while(j<n&&arr[j]==' ')j++;while(j<n&&arr[j]!=' '){arr[i++]=arr[j++];}while(j<n&&arr[j]==' ')j++;if(j<n)arr[i++]=' ';}return new String(arr).substring(0,i);}private void reverseWord(char[] arr) {int n=arr.length,i=0,j=0;while(j<n){while(i<j||i<n&&arr[i]==' ')i++;while(j<i||j<n&&arr[j]!=' ')j++;reverse(arr,i,j-1);}}//翻转left到right间的字母private void reverse(char[] arr, int left, int right) {while(left<right){char temp=arr[left];arr[left]=arr[right];arr[right]=temp;left++;right--;}}}
阅读全文
0 0
- [leetcode]151. Reverse Words in a String@Java解题报告
- [leetcode] 151. Reverse Words in a String 解题报告
- [Leetcode] 151. Reverse Words in a String 解题报告
- leetcode解题报告 Reverse Words in a String
- LeetCode 解题报告 Reverse Words in a String
- [LeetCode OJ] Reverse Words in a String 解题报告
- 【LeetCode】Reverse Words in a String 解题报告
- [leetcode] 186. Reverse Words in a String II 解题报告
- 【LeetCode】Reverse Words in a String III 解题报告
- [Leetcode] 186. Reverse Words in a String II 解题报告
- [Leetcode] 557. Reverse Words in a String III 解题报告
- Leetcode-Reverse Words in a String -java
- LeetCode:Reverse Words in a String (Java)
- [Leetcode][JAVA] Reverse Words in a String
- 【Leetcode】Reverse Words in a String JAVA
- LeetCode|Reverse Words in a String-java
- leetcode-java-151. Reverse Words in a String
- leetcode 151. Reverse Words in a String --------- java
- LeetCode 168 Excel Sheet Column Title
- 【51Nod1070】Bash游戏 V4
- 剑指offer面试题[19]-二叉树的镜像
- Java基础
- android常用框架
- [leetcode]151. Reverse Words in a String@Java解题报告
- 学习android个人笔记一
- CentOS7上安装Docker以及简单使用-yellowcong
- CodeForces
- HDU-2159-FATE(二维完全背包)
- socket 数据预留和对齐(skb_reserve()、skb_put()、skb_push())
- inline-block IE兼容处理
- java常见的设计模式
- wireshark 抓取的tcp 数据报文