用栈结构实现回文数的判断(JavaScript)

封装的方法栈方法:

https://www.cnblogs.com/LIXI-/p/16612874.html

判断回文数:

        function isHuiwen(str) {
            let stack = new Stack();

            for (let i = 0; i < str.length; i++) {
                stack.push(str[i])
            }

            let newstr = ""

            while(!stack.isEmpty()){
                newstr += stack.pop()
            }

            return str == newstr
        }
        console.log(isHuiwen("abcba"));

打印:

true

 

posted on 2022-08-23 09:53  香香鲲  阅读(44)  评论(0编辑  收藏  举报