• 两个关于栈的应用例子


    package DataStructures.Stacks;
    
    import java.util.Stack;
    
    public class DecimalToAnyUsingStack {
        public static void main(String[] args) {
            assert convert(0, 2).equals("0");
            assert convert(30, 2).equals("11110");
            assert convert(30, 8).equals("36");
            assert convert(30, 10).equals("30");
            assert convert(30, 16).equals("1E");
        }
    
        /**
         * Convert decimal number to another radix
         *
         * @param number the number to be converted
         * @param radix the radix
         * @return another radix
         * @throws ArithmeticException if <tt>number</tt> or <tt>radius</tt> is invalid
         */
        private static String convert(int number, int radix) {
            if (radix < 2 || radix > 16) {
                throw new ArithmeticException(
                        String.format("Invalid input -> number:%d,radius:%d", number, radix));
            }
            char[] tables = {
                    '0', '1', '2', '3', '4',
                    '5', '6', '7', '8', '9',
                    'A', 'B', 'C', 'D', 'E', 'F'
            };
            Stack<Character> bits = new Stack<>();
            do {
                bits.push(tables[number % radix]);
                number = number / radix;
            } while (number != 0);
    
            StringBuilder result = new StringBuilder();
            while (!bits.isEmpty()) {
                result.append(bits.pop());
            }
            return result.toString();
        }
    }

    这个就是利用栈来进行进制转换

    package DataStructures.Stacks;
    
    import java.util.Stack;
    
    /**
     * The nested brackets problem is a problem that determines if a sequence of
     * brackets are properly nested. A sequence of brackets s is considered properly
     * nested if any of the following conditions are true: - s is empty - s has the
     * form (U) or [U] or {U} where U is a properly nested string - s has the form
     * VW where V and W are properly nested strings For example, the string
     * "()()[()]" is properly nested but "[(()]" is not. The function called
     * is_balanced takes as input a string S which is a sequence of brackets and
     * returns true if S is nested and false otherwise.
     *
     * @author akshay sharma
     * @author <a href="https://github.com/khalil2535">khalil2535<a>
     * @author shellhub
     */
    class BalancedBrackets {
    
        /**
         * Check if {@code leftBracket} and {@code rightBracket} is paired or not
         *
         * @param leftBracket   left bracket
         * @param rightBracket right bracket
         * @return {@code true} if {@code leftBracket} and {@code rightBracket} is paired,
         * otherwise {@code false}
         */
        public static boolean isPaired(char leftBracket, char rightBracket) {
            char[][] pairedBrackets = {
                    {'(', ')'},
                    {'[', ']'},
                    {'{', '}'},
                    {'<', '>'}
            };
            for (char[] pairedBracket : pairedBrackets) {
                if (pairedBracket[0] == leftBracket && pairedBracket[1] == rightBracket) {
                    return true;
                }
            }
            return false;
        }
    
        /**
         * Check if {@code brackets} is balanced
         *
         * @param brackets the brackets
         * @return {@code true} if {@code brackets} is balanced, otherwise {@code false}
         */
        public static boolean isBalanced(String brackets) {
            if (brackets == null) {
                throw new IllegalArgumentException("brackets is null");
            }
            Stack<Character> bracketsStack = new Stack<>();
            for (char bracket : brackets.toCharArray()) {
                switch (bracket) {
                    case '(':
                    case '[':
                    case '{':
                        bracketsStack.push(bracket);
                        break;
                    case ')':
                    case ']':
                    case '}':
                        if (bracketsStack.isEmpty() || !isPaired(bracketsStack.pop(), bracket)) {
                            return false;
                        }
                        break;
                    default: /* other character is invalid */
                        return false;
                }
            }
            return bracketsStack.isEmpty();
        }
    
    
        public static void main(String[] args) {
            assert isBalanced("[()]{}{[()()]()}");
            assert !isBalanced("[(])");
        }
    }

    匹配括号

    比较简单,今天主要在学习函数式编程。

    一个没有高级趣味的人。 email:hushui502@gmail.com
  • 相关阅读:
    warning: LF will be replaced by CRLF in ***. The file will have its original line endings in your working directory.
    GitHub出现Permissiondenied (publickey).
    浏览器编辑web页面的方法
    用scikit-learn进行LDA降维
    支持向量机原理(二) 线性支持向量机的软间隔最大化模型
    奇异值分解(SVD)原理与在降维中的应用
    scikit-learn K近邻法类库使用小结
    矩阵分解在协同过滤推荐算法中的应用
    精确率与召回率,RoC曲线与PR曲线
    异常点检测算法小结
  • 原文地址:https://www.cnblogs.com/CherryTab/p/11964307.html
Copyright © 2020-2023  润新知