Leetcode easy issue 6 valid parents

Title: valid brackets

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
1 Open brackets must be closed by the same type of brackets.
2 Open brackets must be closed in the correct order.

  • Example1
Input: "()"
Output: true

  • Example2
Input: "()[]{}"
Output: true

  • Example3
Input: "(]"
Output: false

  • Example4
Input: "([)]"
Output: false

  • Example5
Input: "{[]}"
Output: true

Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.

  • Solution method
To satisfy the question, there are only two ways to distribute the brackets:
1 the corresponding brackets appear one by one:
(){}[]
2 open brackets on the left and closed brackets on the right:
( ( { [ ] } ) ) 

The rule of the two: when the first closed bracket appears, the bracket on the left must be the corresponding open bracket

  • solution
var isValid = function(s) {
  let arr = [];
let obj = {
    ']': '[',
    '}': '{',
    ')': '('
}
let strArr = s.split("");

for(let elem of strArr) {
  // Stack all elements in sequence
    arr.push(elem);
    //If the element at this time is closed bracket
    if(elem === ']' || elem === '}' || elem === ')') {
        //Remove the closing bracket that has been placed at the end of the array through arr.push(item)
        arr.pop();
        //arr.pop() has removed the closing bracket at the end of the array
        //From the thought, if the question is satisfied, the last element of the array must be the open bracket corresponding to the deleted closed bracket,
      // If you delete a pair of matching open and close brackets, the program will continue to execute, otherwise it will not conform to the meaning of the question return false
        if(arr.pop() != obj[elem]) return false //Here, the arr.pop() is executed again
    }

    //If the question is satisfied, the pairs of brackets in the array arr will be deleted, and the length is 0
    return arr.length == 0 ? true : false;
}
  • Take case 1: () {} []
var arr = [  ]
arr = ['('] //arr.push()
arr =  [ '(', ')' ] // arr.push
arr = ['('] // arr.pop()
arr = [] //arr.pop()
arr = ['{']
arr = [ '{',  '}' ], 
arr = ['{']
arr = []

Review Map

Es6 A data structure that can be used as a variable key´╝îTraditional objects can only be treated as strings key

var map = new Map([ ['age',13],['name','zs']])
map.get('age') //13


const m = new Map();
const o = {p: 'Hello World'};

m.set(o, 'content')
m.get(o) // "content"

m.has(o) // true
m.delete(o) // true
m.has(o) // false

Difference between for of and for in

  • for of
1,for of Traversal has Iterator The value of the interface,
2 Does not traverse the values on the prototype
3 Can pass break Launch cycle
for(let key of arr){
  console.log(key)
}
//12 4
  • for in
1 Traversal: object array string argument object
2 Ergodic is its corresponding key
3 What's on the prototype will also be traversed( hasOwnProperty Solve)
4,for in Traversing the enumerable properties of an object( key)
5 Traversal order may not follow actual array order

(function() {
  for (let key in arguments) {
    console.log(key+':' + arguments[key]);
  }
})('a', 'b');
//Output 0:a 1:b

var arr = [12,4]
arr.age = 55
for(let key in arr){
  console.log(key)
}
// 12 4 age


  • Data with Iterator interface
Array

Map:  
let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);
for (let entry of iterable) {
  console.log(entry);
}
// ["a", 1]
// ["b", 2]
// ["c", 3]
Set 

String Functional argument object 
(function() {
  for (let argument of arguments) {
    console.log(argument);
  }
})(1, 2, 3);
//1 2 3

NodeList object

//Note that the object does not have an Iterator interface
var obj = {age:13} //Cannot traverse with for of

```

Posted on Wed, 04 Dec 2019 15:35:35 -0800 by ronz