截屏2020-08-21 下午3.02.05.png
var myAtoi = function(str) {
let string = str.replace(/^\s*/,'');
if(/^[-]?\d/.test(string) || /^[+]?\d/.test(string) ){
let num = string.match(/(^[-]?[+]?\d*)/)[1];
if( Number(num) > Math.pow(-2,31) && Number(num) < Math.pow(2,31)-1) {
return Number(num);
}else {
if(num[0] == '-') {
return Math.pow(-2,31)
} else {
return Math.pow(2,31)-1
}
}
}else {
return 0;
}
};
截屏2020-08-21 下午3.04.10.png
var countSubstrings = function(s) {
if(s.length>1000) {
alert('长度不超过 1000')
}else{
let number = 0;
for(let i = 0; i < s.length; i++){
for(let j=i+2; j<s.length+1; j++) {
let str = s.slice(i, j)
let start = 0;
let end = str.length-1;
while(str[start] == str[end]) {
start++;
end--;
if(start >= end) {
number++;
break
}
}
}
}
return number+ s.length;
}
};
截屏2020-08-31 下午5.14.34.png
/**
* @param {number[][]} rooms
* @return {boolean}
*/
var canVisitAllRooms = function(rooms) {
let n = 0 ;
let key = [0];
while (key.length > n) {
key = key.concat([...new Set(rooms[key[n]].filter(item=> !key.includes(item)))]);
n++;
}
if(key.length == rooms.length) {
return true
}else{
return false
}
};
网友评论