[Leetcode] Reverse Words in a String

本文介绍了一种使用Java实现的方法,该方法可以将输入字符串中的单词顺序进行反转,同时处理了字符串中可能出现的多余空格问题。

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

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

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.

public class Solution {
    public String reverseWords(String s) {
        if(s == null)
            return null;
        if(s.length() == 0)
            return s;
        char[] array = s.toCharArray();
        reverse(array, 0, array.length - 1);
        int index = -1;
        int start = 0;
        int space = -1;
        while(start < array.length && array[start] == ' ')
            start++;
        while(start < array.length){
            if(index == -1 || array[start] != ' ')
                array[++index] = array[start++];
            else if(array[start] == ' ' && array[index] == ' '){
                start++;
            }
            else {// array[start] == ' ' && array[index] != ' ' 
                reverse(array, space + 1, index);
                array[++index] = array[start++];
                space = index;
            }
        }
        reverse(array, space + 1, index);
        if(index == -1)
            return new String("");
        else{
            if(index < array.length && array[index] == ' ')
                return new String(array, 0, index);
            else
                return new String(array, 0, index + 1);
        }
    }
    public void reverse(char[] array, int left, int right){
        while(left < right){
            char temp = array[left];
            array[left] = array[right];
            array[right] = temp;
            left++;
            right--;
        }
    }
}

Use Java String.split()
public class Solution {
    public String reverseWords(String s) {
        if(s == null)
            return null;
        if(s.length() == 0)
            return s;
        String[] res = s.split(" ");
        StringBuilder sb = new StringBuilder("");
        for(int i = res.length - 1; i >= 0; i--)
            if(res[i].length() != 0){
                sb.append(' ');
                sb.append(res[i]);
            }
        if(sb.length() == 0)
            return sb.toString();
        return sb.substring(1, sb.length());
    }
}


内容概要:本文围绕列车-轨道-桥梁交互仿真研究,基于Matlab平台构建数值模型,系统分析列车运行过程中轨道与桥梁结构间的动态相互作用机制。研究涵盖多体动力学建模、耦合系统运动方程求解、边界条件设定及仿真结果可视化等关键环节,重点揭示高速行车条件下基础设施的振动传递规律与力学响应特征。该仿真方法可有效评估结构安全性、舒适性指标及疲劳寿命,为轨道交通工程的设计优化与运维管理提供理论支撑和技术路径。文中配套提供了完整的Matlab代码实现方案及操作说明,便于用户复现、验证和拓展相关研究。; 适合人群:具备Matlab编程基础和结构动力学、车辆动力学等相关专业知识的研究生、科研人员及从事铁路工程、桥梁工程与交通系统安全评估的工程技术人才,尤其适合开展轨道交通耦合振动课题的研究者。; 使用场景及目标:①用于高校与科研机构进行列车-轨道-桥梁耦合系统动力学特性的教学演示与科学研究;②支撑高速铁路桥梁的设计优化、运营安全性评估与减振降噪方案验证;③为复杂交通基础设施的多物理场耦合仿真提供建模思路与代码参考。; 阅读建议:建议读者结合所提供的Matlab代码逐模块深入研读,重点关注系统建模假设、质量-刚度-阻尼矩阵构建方法及数值积分算法的实现细节,同时可通过调整参数进行敏感性分析,进一步掌握仿真模型的适用范围与优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值