美文网首页
ARTS挑战第五周

ARTS挑战第五周

作者: 陈_振 | 来源:发表于2019-05-10 16:22 被阅读0次

Algorithm

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.

import java.util.Stack;

class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '{' || c == '[' || c == '(') {
                stack.push(c);
            } else {
                if (stack.isEmpty()) {
                    return false;
                }

                char topChar = stack.pop();
                if (c == '}' && topChar != '{') {
                    return false;
                }
                if (c == ']' && topChar != '[') {
                    return false;
                }
                if (c == ')' && topChar != '(') {
                    return false;
                }
            }
        }

        return stack.isEmpty();
    }
}

Review

Tip

关于选择

  1. 面对多个选择,展望一下各个选择的最终结果,在结果上进行斟酌。
  2. 明白自己的目标是什么,并确定优先级,然后进行选择
  3. 目标清晰才能井然有序
  4. 在发动汽车前一定要知道自己要往哪里去,还要知道选择什么途径去
  5. 在实现目标的方式上要灵活变通

Share

一般将先存放MSB所在字节的架构称为大端,将先存放LSB所在字节的架构称为小端。至于先放置MSB所在字节还是先放置LSB所在的字节,是由CPU的类型决定的。(近期设计的CPU有些可以在大端和小端之间切换)。

相关文章

  • ARTS挑战第五周

    Algorithm Review Tip 关于选择 面对多个选择,展望一下各个选择的最终结果,在结果上进行斟酌。 ...

  • 20181104_ARTS_W5

    第五周arts Algorithm-dp算法题 121. Best Time to Buy and Sell St...

  • ARTS第五周

    Algorithm leetCode 202 Happy Number将数字的每一个数字平方求和,如果等于1就是h...

  • 第五周ARTS

    Algorithmic LeetCode整数反转需要考虑多种情况,负数反转、数据溢出等等 https://leet...

  • ARTS第五周

    Algorithm。主要是为了编程训练和学习。每周至少做一个 leetcode 的算法题(先从Easy开始,然后再...

  • ARTS挑战-第二周

    Algorithm Leetcode-75 Review File System Programming Guid...

  • ARTS挑战第九周

    Algorithm 350. 两个数组的交集 II Review Tip HTTPS可以有效的防止信息窃听,身份伪...

  • ARTS挑战第七周

    Algorithm 804. 唯一摩尔斯密码词 Review Tip 关于搜索 不要给信息归档,用的时候去搜索就行...

  • ARTS打卡第五周

    ARTS打卡第五周 Algorithm:每周至少做一个 leetcode 的算法题 717. 单调数列 代码: }...

  • ARTS打卡,第五周

    每周完成一个ARTS:1.A(Algorithm)每周至少做一个 leetcode 的算法题2.R(Review)...

网友评论

      本文标题:ARTS挑战第五周

      本文链接:https://www.haomeiwen.com/subject/ccddoqtx.html