241021

Week 1

  • Two Sum

  • Valid parentheses

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
    for(let i = 0; i < nums.length; i++) {
       for(let j = i+1; j < nums.length; j++) {
        if (nums[i] + nums[j] === target) {
            return [i, j]
        }
       }
    }
    return []
};

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
 let stack = []
 for (let char of s) {
    if(char === "(" ||char === "[" || char ==='{') {
        stack.push(char)
    } else {
        if(!stack.length || 
        (char === ")" && stack[stack.length -1] !== "(") || 
        (char === "]" && stack[stack.length -1] !== "[") || 
        (char === "}" && stack[stack.length -1] !== "{")) {
            return false
        }
        stack.pop()
    }
 }
 return !stack.length
};

Last updated