The 13th Week of ARTS:Valid Parentheses_20

Introduction

  • Algorithm - Learning Algorithm
  • Review - Learning English
  • Tip - Learning Techniques
  • Share - Learning Influence

Let’s do it!!!

Algorithm

Sort List

  1. Description

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.

  1. My Solution
package com.silence.arts.leetcode.list;


import java.util.*;

/**
 * <br>
 * <b>Function:</b><br>
 * <b>Author:</b>@author Silence<br>
 * <b>Date:</b>2018-11-25 16:49<br>
 * <b>Desc:</b>无<br>
 */
public class ValidParentheses_20 {

    /**
     *
     * @param s
     * @return
     */
    public static boolean isValid(String s) {
        if (null != s && s.length() > 0) {
            Map<String, String> map = new HashMap<>();
            map.put(")", "(");
            map.put("]", "[");
            map.put("}", "{");
            Stack<String> stack = new Stack<>();
            for (int i = 0; i < s.length(); i++) {
                String val = String.valueOf(s.charAt(i));
                //if (!map.containsKey(val)) {
                if (val.equals("(") || val.equals("[") || val.equals("{")) {
                    stack.push(val);
                } else if (stack.size() <= 0 || !map.get(val).equals(stack.pop())) {
                    return false;
                }
            }
            //stack needs empty
            return stack.size() <= 0;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isValid(""));
    }
}


tow points

Review

No Review

Tips

MySQL实战

Share

Sharing

常用算法笔记——持续更新

One more thing