Coder Social home page Coder Social logo

实现字符串反转 about fe-learning HOT 6 OPEN

metroluffy avatar metroluffy commented on June 2, 2024
实现字符串反转

from fe-learning.

Comments (6)

Mrcxt avatar Mrcxt commented on June 2, 2024

用for循环是不是要比while好点,逻辑上也更清晰一点

from fe-learning.

Mrcxt avatar Mrcxt commented on June 2, 2024

利用栈

String.prototype.str_reverse = function() {
        let stack = [],
            _str = '',
            str = this;

        for (let i = str.length - 1; i >= 0; i--) {
            let s = str[i]
            if (s === '.') {
                while (stack.length) {
                    _str += stack.pop();
                }
                _str += s
            } else {
                stack.push(s);
            }
        }

        while (stack.length) {
            _str += stack.pop();
        }

        return _str
    }

from fe-learning.

metroluffy avatar metroluffy commented on June 2, 2024

利用栈

String.prototype.str_reverse = function() {
        let stack = [],
            _str = '',
            str = this;

        for (let i = str.length - 1; i >= 0; i--) {
            let s = str[i]
            if (s === '.') {
                while (stack.length) {
                    _str += stack.pop();
                }
                _str += s
            } else {
                stack.push(s);
            }
        }

        while (stack.length) {
            _str += stack.pop();
        }

        return _str
    }

赞一个,不过在遇到 .之前是不是可以不用入栈

    for (let i = 0; i < str.length; i++) {
        let s = str[i]
        if (s === '.') {
            stack.push(_str);
            _str = ''
        } else {
            _str += s
        }
    }    

    while (stack.length) {
        _str += '.' + stack.pop();
    }

from fe-learning.

Mrcxt avatar Mrcxt commented on June 2, 2024

利用栈

String.prototype.str_reverse = function() {
        let stack = [],
            _str = '',
            str = this;

        for (let i = str.length - 1; i >= 0; i--) {
            let s = str[i]
            if (s === '.') {
                while (stack.length) {
                    _str += stack.pop();
                }
                _str += s
            } else {
                stack.push(s);
            }
        }

        while (stack.length) {
            _str += stack.pop();
        }

        return _str
    }

赞一个,不过在遇到 .之前是不是可以不用入栈

    for (let i = 0; i < str.length; i++) {
        let s = str[i]
        if (s === '.') {
            stack.push(_str);
            _str = ''
        } else {
            _str += s
        }
    }    

    while (stack.length) {
        _str += '.' + stack.pop();
    }

good,第一版的split函数 + 栈 😂

from fe-learning.

KennethYo avatar KennethYo commented on June 2, 2024

头条的面试是不能开辟额外空间

from fe-learning.

pigpigever avatar pigpigever commented on June 2, 2024

我记得字符串是不能被更改的?所以怎么样都会有额外空间吧,感觉这样写更简洁一点

function reverse(str) {
    let res = ''
    for (const word of str) {
        res = word + res
    }
    return res
}

from fe-learning.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.