Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现一个字符串反转:输入:www.toutiao.com.cn 输出:cn.com.toutiao.www #41

Open
CodeRookie262 opened this issue Feb 24, 2021 · 1 comment
Labels

Comments

@CodeRookie262
Copy link
Owner

实现一个字符串反转:输入:www.toutiao.com.cn 输出:cn.com.toutiao.www
可以先暴力解决问题,最终看看是否能否根据以下要求优化。

要求:
1.不使用字符串处理函数
2.空间复杂度尽可能小

@CodeRookie262
Copy link
Owner Author

CodeRookie262 commented Feb 25, 2021

解法一

const reverseWord = function(str){
    return str.split(".").reverse().join('.')
}

解法二

const reverseWord = function(str){
    var rs = '',
          word = '';
    for(var s of str){
        if(s === '.'){
             rs += word + s;
             word = '';   
            }else{
             word += s;
           }
    }
    rs += word;
    return rs;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant