【毎日1題】LeetCode 0020.有効なかっこ

1479 ワード

オープンソースアドレス:https://github.com/jiauzhang/algorithms
タイトル
/*
* https://leetcode-cn.com/problems/valid-parentheses
*         '(',')','{','}','[',']'     ,         。
*         :
*                     。
*                  。
* 
*                 。
*/

/*
*    1:
*       : "()"
*       : true
* 
*    2:
*       : "()[]{}"
*       : true
* 
*    3:
*       : "(]"
*       : false
* 
*    4:
*       : "([)]"
*       : false
* 
*    5:
*       : "{[]}"
*       : true
*/

問題を解く構想.
/*
* 1.              ,           
*                           
*               
* 2.       ,             ,       
*       {[()]},               ,     
*            ,         ,         
*    open_close           
* 3.         ,            ,      
*             ,    
*    {[( --> {[() --> {[ --> {[] --> { --> {} --> empty
*/

サンプルコード
class Solution {
public:
    bool isValid(string s) {
        if (s.size() == 0)
            return true;
        
        stack stk;
        unordered_map open_close = {
            {')', '('},
            {'}', '{'},
            {']', '['}
        };
        
        for (int i=0; i