let obj = {a:[{b: {c: 1}}]}
// console.log(obj.a[0].b.c)
function get(obj, str) {
str = str.split('.')
let len = str.length
for(let i = 0; i < len; ++i) {
if(str[i] && str[i].indexOf('[') != -1) {
let index = str[i].match(/\[(\d+)\]/)[1]
let name = str[i].split('[')[0]
if(name in obj) {
obj = obj[name][index]
} else {
return undefined
}
} else if(str[i] in obj && obj[str[i]]) {
obj = obj[str[i]]
} else {
return undefined
}
}
return obj
}
console.log(get(obj, 'a[0].b.c'))
console.log(get(obj, 'a[0].b.s'))
console.log(get(obj, 'a.s.q'))
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串 输入字符串
* @return bool布尔型
*/
function isVerified( str ) {
// write code here
let len = str.length
let s = []
for(let i = 0; i < len; ++i) {
if(str[i] == '*') continue
if(str[i] == '{' || str[i] == '[' || str[i] == '(') {
s.push(str[i])
} else {
let top = s[s.length-1]
if((str[i] == '}' && top == '{') || (str[i] == ']' && top == '[')
|| (str[i] == ')' && top == '(')) {
s.pop()
}
}
}
return s.length == 0
}
module.exports = {
isVerified : isVerified
};
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param arr int整型一维数组 数组
* @return int整型
*/
function lengthOfLis( arr ) {
// write code here
let len = arr.length
let dp = new Array(len).fill(1)
dp[0] = 1;
for(let i = 1; i < len; ++i) {
for(let j = 0; j < i; ++j) {
if(arr[i] > arr[j]) dp[i] = Math.max(dp[j]+1, dp[i])
}
}
return dp[len-1]
}
module.exports = {
lengthOfLis : lengthOfLis
};
// 求数组最长递增子序列长度