题解 | #牛群消息传递#

牛群消息传递

https://www.nowcoder.com/practice/28df6c40150a40b49c9c4d4ae1dd675d

package main

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param s string字符串 
 * @return string字符串
*/
func reverseWords( s string ) string {
    // write code here
    var res string 
    for i,j :=len(s)-1,len(s);i>=0;i--{
        if s[j-1]==' '{
            j--
        }
        if s[i]==' '&&i!=j{
            temp :=s[i:j]
            res+=temp
            j=i
        }else if i==0&&i!=j{
            temp :=s[i:j]
            temp =" "+temp
            res+=temp
        }
    }
    if res[0]==' '{
        res=res[1:]
    }
    return res
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务