首页 > 试题广场 >

翻转单词序列

[编程题]翻转单词序列
  • 热度指数:687780 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“nowcoder. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a nowcoder.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?

数据范围:1 \leq n \leq 100 \
进阶:空间复杂度 ,时间复杂度 ,保证没有只包含空格的字符串
示例1

输入

"nowcoder. a am I"

输出

"I am a nowcoder."
示例2

输入

""

输出

""
推荐
public class Solution {
    public String ReverseSentence(String str) {
        if(str.trim().equals("")){
            return str;
        }
        String[] a = str.split(" ");
        StringBuffer o = new StringBuffer();
        int i;
        for (i = a.length; i >0;i--){
            o.append(a[i-1]);
            if(i > 1){
                o.append(" ");
            }
        }
        return o.toString();
    }
}

编辑于 2015-06-19 17:23:02 回复(45)
import java.util.*;
public class Solution {
    public String ReverseSentence(String str) {
        str = reverse(str);
        String[] strs = str.split(" ");
        for (int i = 0; i < strs.length; i++) {
            strs[i] = reverse(strs[i]);
        }
        return String.join(" ", strs);

    }

    private String reverse(String s) {
        int n = s.length();
        char[] chs = s.toCharArray();
        int i = 0;
        int j = n - 1;
        while (i < j) {
            char t = chs[i];
            chs[i] = chs[j];
            chs[j] = t;
            i++;
            j--;
        }
        return new String(chs);
    }
}
发表于 2024-08-02 17:13:54 回复(0)
import java.util.*;
public class Solution {
    public String ReverseSentence(String str) {
        String[] s = str.split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = s.length-1; i >= 0; i--) {
            sb.append(s[i]+" ");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }
}

发表于 2023-09-07 11:02:45 回复(0)
public class Solution {
    public String ReverseSentence(String str) {
        if (str.isEmpty()) return "";
        String[] s = str.split(" ");
        StringBuffer sb = new StringBuffer();
        for (int i = s.length - 1; i >= 0; i--) {
            sb.append(s[i]);
            sb.append(" ");
        }
        return sb.toString().substring(0, sb.length() - 1);
    }
}

发表于 2023-03-09 16:59:23 回复(0)
public class Solution {
    public String ReverseSentence(String str) {
        String res = "", tmp = "";
        for (int i = 0; i < str.length(); ++i) {
            if (str.charAt(i) == ' ') {
                res = " " + tmp + res;
                tmp = "";
            } else tmp += String.valueOf(str.charAt(i));

        }
        if (tmp.length() > 0) res = tmp + res;
        return res;
    }
}

发表于 2023-03-08 22:25:08 回复(0)
public class Solution {
    public String ReverseSentence(String str) {
        if(str.length()==0)return str;
        char[] c = str.toCharArray();
        Reverse(c,0,str.length()-1);
        int start=0;
        for(int i =0;i<=c.length;i++){
            if( i==c.length|| c[i]==' '){
                Reverse(c,start,i-1);
                start=i+1;
            }
        }
        String s = "";

        for(int j = 0;j<c.length;j++){
            s+=c[j];
        }
        return s;
    }
    public void Reverse(char[] c,int start ,int end){
        while(start<=end){
            char temp = c[end];
            c[end] = c[start];
            c[start] = temp;
            start++;
            end--;
        }
    }
}
发表于 2022-11-24 19:45:24 回复(0)
import java.util.Stack;

public class Solution {
    public String ReverseSentence(String str) {
        Stack<String> stack = new Stack<>();
        StringBuilder ans = new StringBuilder();
        int i = 0;
        int j = 0;
        while(i<str.length()){
            //找出整个单词
            if(str.substring(i,i+1).equals(" ")){
                while(j<str.length() && str.substring(j,j+1).equals(" ")){
                    j++;
                }
                stack.push(str.substring(i,j));
                i = j;
            }else{
                //找出整个空格
                while(j<str.length() && !str.substring(j,j+1).equals(" ")){
                    j++;
                }
                stack.push(str.substring(i,j));
                i = j;
            }
        }
        while(!stack.empty()){
            ans.append(stack.pop());
        }
        return ans.toString();
    }
}

发表于 2022-11-11 21:09:29 回复(0)
用栈一下子就搞定啦!
public class Solution {
    public String ReverseSentence(String str) {
        if (str == null || str.length() == 0) {
            return "";
        }
        String[] split = str.split(" ");
        Stack<String> stack = new Stack<>();
        for (int i = 0; i < split.length; i++) {
            stack.push(split[i]);
        }
        StringBuilder builder = new StringBuilder();
        while (!stack.isEmpty()) {
            builder.append(stack.pop()).append(" ");
        }
        return builder.toString().trim();
    }
}


发表于 2022-10-09 14:16:21 回复(0)
划水
import java.util.*;
public class Solution {
    public String ReverseSentence(String str) {
    

        String[] s = str.split(" ");

        List<String> list = Arrays.asList(s);

        Collections.reverse(list); 
        
        String collect = String.join(" ", list);
        
        return collect;
    }
}
发表于 2022-08-29 16:49:32 回复(0)
public class Solution {
    public String ReverseSentence(String str) {
        if (str.equals("")) return "";

        String[] strs = str.split(" ");
        StringBuilder sb = new StringBuilder();

        for (int i = strs.length - 1; i >= 0; i--) {
            sb.append(strs[i]).append(" ");
        }
        sb.deleteCharAt(sb.length() - 1);

        return sb.toString();
    }
}
发表于 2022-08-16 20:43:17 回复(0)
import java.util.*;
public class Solution {
    public String ReverseSentence(String str) {
        if(str==null||str.equals("")){
            return str;
        }
        StringBuilder res=new StringBuilder();
        
        String[] vals=str.split(" ");
        for(int i=vals.length-1;i>=0;i--){
            res.append(vals[i]);
            if(i>0){
               res.append(" ");
            }
           
        }
        
        return res.toString();
    }
}

发表于 2022-07-02 20:35:11 回复(0)
//自己的做法 
public String ReverseSentence(String str) {
        String res = "";
        char[] chs = str.toCharArray();
        String s = reverse(chs,0,chs.length - 1);
        String[] strs = s.split(" ");
        for(int i = 0;i < strs.length;i++) {
            char[] c1 = strs[i].toCharArray();
            res += reverse(c1,0,c1.length - 1);
            if(i != strs.length - 1) {
                res += " ";
            }
        }
        
        return res;
    }
    public String reverse(char[] chs,int i,int j) {
        while(i < j) {
            char temp = chs[i];
            chs[i] = chs[j];
            chs[j] = temp;
            i++;
            j--;
        }
        return new String(chs);
    }

发表于 2022-06-22 10:08:23 回复(0)

public class Solution {
    public String ReverseSentence(String str) {
        String[] split = str.split(" ", -1);
        for (int i = 0; i < split.length / 2; i++) {
            String temp = split[i];
            split[i] = split[split.length - i -1];
            split[split.length - i -1] = temp;
        }
        return String.join(" ", split);
    }
}

发表于 2022-06-11 07:26:50 回复(0)
public class Solution {
    public String ReverseSentence(String str) {
        if (str == null || str.equals("")){
            return "";
        }
        String[] strings = str.split(" ");
        int low = 0;
        int high = strings.length-1;
        while (low < high){
            String tmp = strings[low];
            strings[low] = strings[high];
            strings[high] = tmp;
            low++;
            high--;
        }
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < strings.length; i++) {
            ans.append(strings[i]);
            if (i != strings.length-1){
                ans.append(" ");
            }
        }
        return ans.toString();
    }
}

发表于 2022-05-29 11:44:54 回复(0)
// 第一次写
 public static String ReverseSentence(String str) {
        Stack<String> stack = new Stack<>();
        Queue<String> q=new LinkedList<>();
        if (str==""||str==null){
            return str;
        }
        char[] chars = str.toCharArray();
        int l = chars.length;

        String s="";

    for (int i = 0; i < l; i++) {
        if (chars[i]!=' '){
            s+=chars[i];
        }
        if (chars[i]==' '){
            stack.push(s);
            stack.push(" ");
            s="";
            if (i==l-1){
                stack.push(s);
                s="";
            }
        }

    }

        while (!stack.isEmpty()){
            s+=stack.pop();
        }
        return s;

    }

发表于 2022-05-14 21:48:18 回复(0)
public class Solution {
    public String ReverseSentence(String str) {
        String[] s = str.split(" ");
        int n = s.length;
        StringBuilder sb = new StringBuilder();
        while (n > 0) {
            sb.append(s[n - 1]);
            if (n != 1) {
                 sb.append(" ");
            }
            n--;
        }
        return sb.toString();
    }
}
发表于 2022-05-05 06:56:20 回复(0)
public class Solution {
    public String ReverseSentence(String str) {
        StringBuilder sb = new StringBuilder("");
        if(str == null)
            return null;
        if(str == "")
            return sb.toString();
        String[] arr = str.trim().split("\\s+");
        for(int i = arr.length-1;i>=0;i--)
            sb.append(arr[i]).append(" ");
        return sb.toString().trim();
    }
}

发表于 2022-04-06 17:40:41 回复(0)

    public String ReverseSentence(String str) {
        if (str == null || str.length() <= 1) {
            return str;
        }

        //双指针,从末尾开始
        int head = str.length() -1;
        int tail = head;
        StringBuilder builder = new StringBuilder();
        for (; head >= 0 ; head--) {
            //如果当前是空串,直接拼接,tail 指针左走一步
            if (str.charAt(head ) == ' ') {
                builder.append(str.charAt(head));
                tail = head -1;
            }
            //向前移动head指针,如果移动到0位置,或者左边一位是空串,则对 head --> tail 之间的区间字符串拼接
            if (head == 0 || str.charAt(head - 1) == ' ') {
                for (int j = head; j <= tail; j++) {
                    builder.append(str.charAt(j));
                }
                tail =head -1;
            }
        }
        return builder.toString();
    }
}

发表于 2022-03-26 22:28:46 回复(0)

问题信息

难度:
198条回答 130901浏览

热门推荐

通过挑战的用户

查看代码
翻转单词序列