4、判断回文字符串
function isHuiwen(str) {
let arr = str.split('')
for(let i =0 ; i< arr.length/2 + 1; i++) {
if(arr[i] !== arr[arr.length - 1-i]) {
return false
} else {
continue
}
}
return true
}
let a = 'abba'
let b = 'acdca'
let c = 'favdkoll'
console.info(isHuiwen(a))
console.info(isHuiwen(b))
console.info(isHuiwen(c))
5、函数 防抖 ,函数节流
function debounce(fn, wait) {
let timer = null
return function() {
clearTimeout(timer)
timer = setTimeout(() => {
fn.apply(this, arguments)
}, time)
}
}
// 节流函数
function throttle(fn, wait) {
let flag = true
return function() {
if(!flag) return
flag = flag
setTimeout(() => {
fn.apply(this, arguments)
flag = true
}, time)
}
}
6、全排列
// abc , abc,acb,bac,bca,cab,cba
function getGroup(str) {
const arr = str.split('')
const result = []
function dfs(temp) {
if (temp.length === arr.length) {
result.push(temp)
return
}
for (let i = 0; i < arr.length; i++) {
if (!temp.includes(arr[i])) {
dfs([...temp, arr[i]])
}
}
}
dfs([])
return result
}
7、数组拍平
// 数组拍平 arr = [1,2, ['3', '4' , '5', [6, [7, 8], 9, 10]]]
function flat(arr) {
const result = []
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
result.push(...flat(arr[i]))
} else {
result.push(arr[i])
}
}
return result
}
function flatten(arr) {
return arr.reduce((a, b) => {
// return Array.isArray(b) ? a.concat(flatten(b)) : a.concat(b);
return a.concat(Array.isArray(b) ? flatten(b) : b);
}, []);
};
let arr = [1,2, ['3', '4' , '5', [6, [7, 8],9, 10]]]
let res1 = flat2(arr)
console.info('res1',res1)
8、逗号分割
// 1234567.90 1,234,567.90
function slove(num) {
const arr = num.toString().split('.')
const part1 = arr[0].split('')
const part2 = arr[1]
let res = ''
part1.reverse().forEach((value, index) => {
if (index !== 0 && index % 3 === 0) {
res = value + ',' + res
} else {
res = value + '' + res
}
})
return res+ '.' + part2
}
let num1 = 1234567.90
// let res1 = slove(num1)
// console.info('res1', res1)
// 先行断言
function format_with_regex(number) {
return !(number + '').includes('.')
? // 就是说1-3位后面一定要匹配3位
(number + '').replace(/\d{1,3}(?=(\d{3})+$)/g, (match) => {
return match + ',';
})
: (number + '').replace(/\d{1,3}(?=(\d{3})+(\.))/g, (match) => {
return match + ',';
});
}
console.log(format_with_regex(1243250.990));
9、最长无重复子串
// s: 'ababdfghbc'
function longestSubStr(str) {
let map = new Map()
let max = 0
let i = 0
let arr = str.split('')
for (let j = 0; j < arr.length; j++) {
if (map.has(arr[j])) {
i = map.get(arr[j]) + 1
}
max = Math.max(j - i + 1, max)
map.set(arr[j], j)
}
return max
}
console.info(longestSubStr('ababdfghbc'))
console.info(longestSubStr('bbbbb'))
// pwwkew
console.info(longestSubStr('pwwkew'))
10、小于n的最大数
// 23121 { 2, 4, 9 } 22999
// 23121 { 7 8 9 } 9999
var res = ''
function largestNum(sum, arr) {
var target = sum + ''
DFS(0, false, 0, arr, target)
return res
}
function DFS(index, flag, sum, arr, target) {
if (index === target.length) {
res = sum
return true;
}
if(flag){
return DFS(index+1, true, sum*10 + arr[arr.length-1], arr, target);
} else {
let temp = target.charAt(index) - '0';
for(let i = arr.length-1; i >= 0; i--){
if(arr[i] === temp){
if(DFS(index+1, false, sum*10 + arr[i], arr, target)){
return true;
}
} else if (arr[i] < temp) {
if(DFS(index+1, true, sum*10 + arr[i], arr, target)){
return true;
}
}
}
}
// 数组中最小的元素都大于数字该位的值
if(index !== 0) return false; // 回溯一步(退回一步)
else return DFS(index+1, true, sum, arr, target);
}
let res1 = largestNum(23121, [2, 4, 9])
let res2 = largestNum(23121, [7, 8, 9])
console.info('res1', res1)
console.info('res2', res2)
11、岛屿数量
let grid1 = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
let grid2 = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
/**
* @param {character[][]} grid
* @return {number}
*/
var numIslands = function(grid) {
let count = 0
const m = grid.length
const n = grid[0].length
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === "1") {
solve(i, j)
count++
} else {
continue
}
}
}
function solve(i, j) {
if (i - 1 >= 0 && i - 1 <= m - 1 && grid[i-1][j] === '1') {
grid[i - 1][j] = 0
solve(i-1, j)
}
if (j - 1 >= 0 && j - 1 <= n - 1 && grid[i][j-1] === '1') {
grid[i][j-1] = 0
solve(i, j-1)
}
if (i + 1 >= 0 && i + 1 <= m - 1 && grid[i+1][j] === '1') {
grid[i+1][j] = 0
solve(i+1, j)
}
if (j + 1 >= 0 && j + 1 <= n - 1 && grid[i][j+1] === '1') {
grid[i][j + 1] = 0
solve(i, j + 1)
}
}
return count
};
let res1 = numIslands(grid2)
console.info('res1', res1)
12、
// 最长回文子串
// 输入:s = "babad"
// 输出:"bab"
// 解释:"aba" 同样是符合题意的答案。
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function(s) {
let dp = [];
let res = '';
for (let i = 0; i < s.length; i++) {
dp[i] = [];
for (let j = 0; j < s.length; j++) {
dp[i][j] = 0;
res = s[j]
}
dp[i][i] = true;
}
for(let i = s.length -1;i >= 0;i--){
for(let j = i;j < s.length;j++){
dp[i][j] = s[i] == s[j] && (j - i < 2 || dp[i+1][j-1]);
if(dp[i][j] && j - i +1 > res.length){
res = s.substring(i,j+1);
}
}
}
return res;
};