’12345678‘形式的整数字符串变为’12,345,678‘字符串(用英文逗号分割)
function transfrom(str) {
let result=''
if (str.length % 3 == 0) {
for (let j = 2; j < str.length; j = j + 3) {
result = result + str[j - 2] + str[j - 1] + str[j] + ','
}
} else {
if(str.length % 3 == 1){
result= str[0]+','
}else{
result = str[0] + str[1] + ','
}
let begin=str.length%3
for (let i = begin+2; i < str.length; i = i + 3) {
result = result + str[i - 2] + str[i - 1] + str[i] + ','
}
}
result=result.slice(0,result.length-1)
console.log(result)
}
transfrom('1234567890')
有{}、()、[],三种
测试用例一:
输入:{}[]
输出:true
测试用例二:
输入:{]
输出:false
测试用例三:
输入:{[]}
输出:true
var obj = {
'{': 0,
'}': 0,
'[': 1,
']': 1,
'(': 2,
')': 2
}
function pipei(str) {
if (str.length % 2 !== 0) return false
let j = str.length
for (let i = 0; i < j; i++) {
if (obj[str[i]] == obj[str[i + 1]]) {
i = i + 2
if (i = str.length - 2) {
return true
}
} else if (obj[str[i]] == obj[str[str.length - i]]) {
j = str.length - i
if(i==str.length%2-1){
return true
}
}
}
return false
}
console.log(pipei('([])'))