From d4c08e55afc941463909c78bd8f68513fa0e0a58 Mon Sep 17 00:00:00 2001 From: Eished Date: Fri, 1 Sep 2023 23:41:45 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0JS=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=92=8C=E7=89=9B=E5=AE=A2=E7=BD=91=E9=A2=98=E5=BA=93=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typescript/nodemon.json | 19 ++-- typescript/package.json | 3 +- typescript/readme.md | 3 + typescript/src/index.js | 210 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 225 insertions(+), 10 deletions(-) create mode 100644 typescript/readme.md create mode 100644 typescript/src/index.js diff --git a/typescript/nodemon.json b/typescript/nodemon.json index 4edb065..a472a8e 100644 --- a/typescript/nodemon.json +++ b/typescript/nodemon.json @@ -1,9 +1,10 @@ -{ - "watch": ["src"], - "ext": "ts", - "ignore": ["*.test.ts"], - "delay": "0", - "execMap": { - "ts": "ts-node" - } -} +{ + "watch": ["src"], + "ext": "ts,js", + "ignore": ["*.test.ts"], + "delay": "0", + "execMap": { + "ts": "ts-node", + "js": "node --harmony" + } +} diff --git a/typescript/package.json b/typescript/package.json index 4c6a952..d64c6e0 100644 --- a/typescript/package.json +++ b/typescript/package.json @@ -3,7 +3,8 @@ "version": "1.0.0", "description": "", "scripts": { - "start": "nodemon src/index.ts", + "dev:ts": "nodemon src/index.ts", + "dev:js": "nodemon src/index.js", "lint": "eslint ./src --fix" }, "author": "", diff --git a/typescript/readme.md b/typescript/readme.md new file mode 100644 index 0000000..e2cbcb5 --- /dev/null +++ b/typescript/readme.md @@ -0,0 +1,3 @@ +## nodemon + +https://github.com/remy/nodemon/blob/master/doc/sample-nodemon.md diff --git a/typescript/src/index.js b/typescript/src/index.js new file mode 100644 index 0000000..5c8a45c --- /dev/null +++ b/typescript/src/index.js @@ -0,0 +1,210 @@ +// https://www.nowcoder.com/exam/oj/ta?page=1&tpId=37&type=37 +console.time('time') +function test1() { + const line = 'hello nowcoder' + const tokens = line.split(' ') + console.log(tokens[tokens.length - 1].length) +} + +let text = '' +function test2(line) { + if (!text) { + text = line + } else { + const reg = new RegExp(line, 'gi') + console.log(text.match(reg)?.length ?? 0) + } +} +// test2('ABCabc') +// test2('A') + +let total = 0 +const set = new Set() +function test3(line) { + if (total === 0) { + total = line + } else { + set.add(line) + } +} +// test3(4) +// test3(123) +// test3(2) +// test3(27) +// test3(2) +// test3(1) +// Array.from(set) +// .sort((a, b) => a - b) +// .forEach((s) => console.log(s)) + +function test4() { + let line = '123456789123456789123456789123456789123456789' + // for (let i = 0; i <= parseInt((line.length - 1) / 8); i++) { + // let str = line.slice(i * 8, i * 8 + 8) + // if (str.length < 8) { + // str = str + '0'.repeat(8 - str.length) + // // str = str.padEnd(8, '0') + // } + // console.log(str) + // } + let str = line + '00000000' + for (let i = 8; i < str.length; i += 8) { + console.log(str.substring(i - 8, i)) + } +} +// test4() + +function test5(number) { + console.log(parseInt(number, 16)) + console.log(Number(number)) + console.log(number.toString(10)) +} +// test5('0xAA') + +function test6() { + let num = 180 + const arr = [] + for (let i = 2; i * i <= num; i++) { + while (num % i === 0) { + console.log(num, i) + arr.push(i) + num = num / i + } + } + if (num > 1) { + arr.push(num) + } + console.log(arr.join(' ')) +} +// test6() + +function test7(num) { + console.log(Number(num).toFixed(0)) +} +// test7('5.5') +// test7('5.4') +// test7('2.499') + +const obj8 = {} +function test8(str) { + const strs = str.split(' ') + if (strs[1]) { + const value = obj8[strs[0]] + if (value) { + obj8[strs[0]] = Number(strs[1]) + Number(value) + } else { + obj8[strs[0]] = strs[1] + } + } +} +// test8('4') +// test8('3 4') +// test8('0 1') +// test8('1 2') +// test8('0 2') +// test8('0 2') +// Object.entries(map) +// .sort(([ak], [bk]) => ak - bk) +// .forEach(([k, v]) => console.log(k, v)) + +function test9(str) { + console.log([...new Set([...str].reverse())].join('')) + // console.log([...new Set(str)].reverse().join('')) +} +// test9('9876673') + +function test10(str) { + // const map10 = new Map() + // for (let i = 0; i < str.length; i++) { + // const s = str[i] + // const value = map10.get(s) + // map10.set(s, value) + // } + // console.log(map10.size) + console.log(new Set([...str]).size) +} +// test10('abc') +// test10('aaa') + +function test11(str) { + console.log([...str].reverse().join('')) +} +// test11('1516000') + +function test12(str) { + console.log([...str].reverse().join('')) +} +// test12('abcd') + +function test13(str) { + const strs = str.split(' ') + console.log(strs.reverse().join(' ')) +} +// test13('I am a boy') + +const arr14 = [] +let count14 +function test14(str) { + if (!count14) { + count14 = str + } else { + arr14.push(str) + } +} +// test14('9') +// test14('cap') +// test14('too') +// test14('boot') +// arr14.sort().forEach((a) => console.log(a)) + +function test15(str) { + console.log(parseInt(str, 10).toString(2).match(/1/g).length) +} +// test15('5') +const mainItems16 = [] +const subItems16 = [] +const items16 = [] +let n +let m +function test16(str) { + const strs = str.split(' ') + const q = strs[2] + if (q === undefined) { + n = strs[0] + m = strs[1] + } else { + const v = strs[0] + const p = strs[1] + if (q === 0) { + mainItems16.push([v, p, q]) + } else { + subItems16.push([v, p, q]) + } + items16.push([v, p, q]) + } +} +test16('50 5') +test16('20 3 5') +test16('20 3 5') +test16('10 3 0') +test16('10 2 0') +test16('10 1 0') +const result = [] +let satisfaction = 0 +for (let index = 0; index < items16.length; index++) { + const [v, p, q] = items16[index] + console.log(v, p, q) + let price = Number(v) + satisfaction += v * p + result.push([v, satisfaction]) + items16.slice(index + 1, items16.length).forEach(([v2, p2, q2]) => { + price += Number(v2) + console.log(price, n) + if (price <= n && result.length < 6) { + satisfaction += v2 * p2 + result.push([v2, satisfaction]) + } + }) +} +console.log(result, 'result') +console.timeEnd('time') From 8df57371d85578134593dc417934fc9aa08b78bb Mon Sep 17 00:00:00 2001 From: Eished Date: Sat, 2 Sep 2023 04:54:12 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E8=83=8C=E5=8C=85=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E7=A9=B7=E4=B8=BE=E6=B3=95=E5=86=85=E5=AD=98=E8=B6=85?= =?UTF-8?q?=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typescript/src/index.js | 127 +++++++++++++++++++++++++++------------- 1 file changed, 87 insertions(+), 40 deletions(-) diff --git a/typescript/src/index.js b/typescript/src/index.js index 5c8a45c..54d18c6 100644 --- a/typescript/src/index.js +++ b/typescript/src/index.js @@ -161,50 +161,97 @@ function test15(str) { console.log(parseInt(str, 10).toString(2).match(/1/g).length) } // test15('5') -const mainItems16 = [] -const subItems16 = [] -const items16 = [] -let n -let m -function test16(str) { - const strs = str.split(' ') - const q = strs[2] - if (q === undefined) { - n = strs[0] - m = strs[1] - } else { - const v = strs[0] - const p = strs[1] - if (q === 0) { - mainItems16.push([v, p, q]) + +function shoppingCart() { + let items16 = [] + let n + let m + let id = 1 + function test16(str) { + const strs = str.split(' ') + const q = strs[2] + if (q === undefined) { + n = strs[0] + m = strs[1] } else { - subItems16.push([v, p, q]) + const v = strs[0] + const p = strs[1] + items16.push([v, p, q, id]) + id++ } - items16.push([v, p, q]) } -} -test16('50 5') -test16('20 3 5') -test16('20 3 5') -test16('10 3 0') -test16('10 2 0') -test16('10 1 0') -const result = [] -let satisfaction = 0 -for (let index = 0; index < items16.length; index++) { - const [v, p, q] = items16[index] - console.log(v, p, q) - let price = Number(v) - satisfaction += v * p - result.push([v, satisfaction]) - items16.slice(index + 1, items16.length).forEach(([v2, p2, q2]) => { - price += Number(v2) - console.log(price, n) - if (price <= n && result.length < 6) { - satisfaction += v2 * p2 - result.push([v2, satisfaction]) + test16('1000 5') + // test16('20 3 5') + // test16('20 3 5') + // test16('10 3 0') + // test16('10 2 0') + // test16('10 1 0') + test16('800 2 0') + test16('400 5 1') + test16('300 5 1') + test16('400 3 0') + test16('500 2 0') + // 分类 + // subItems16.forEach((si) => { + // // console.log(si) + // const index = mainItems16.findIndex((mi) => mi[3] == si[2]) + // mainItems16[index].childs.push(si) + // }) + // 穷举购物车 + function getAllSubsets(arr) { + const subsets = [[]] + for (const item of arr) { + const newSubsets = subsets.map((subset) => { + // console.log(subset, 'subset') + return [...subset, item] + }) + subsets.push(...newSubsets) + } + return subsets + } + items16 = getAllSubsets(items16) + // console.log(subsets) + + // 穷举结果 + let results = [] + items16 = items16.filter((sbs) => { + if (sbs.length == 0) { + return true + } else if (sbs.length == 1) { + if (sbs[0][0] < n) { + return true + } + } + let isParentIn = true + const childs = sbs.filter((sb) => sb[2] != 0) + if (childs.length) { + isParentIn = childs.every((s) => sbs.some((b) => b[3] == s[2])) + } + if (isParentIn) { + return true } }) + + items16 = items16.forEach((sbs) => { + let totalPrice = 0 + let satisfaction = 0 + sbs.forEach((item) => { + totalPrice += Number(item[0]) + satisfaction += Number(item[0]) * Number(item[1]) + // console.log(item) + }) + // console.log(sbs) + + if (totalPrice <= n) { + // console.log(totalPrice) + results.push(satisfaction) + } + }) + items16 = undefined + // console.log(results) + results = results.sort((a, b) => b - a) + + console.log(results[0]) } -console.log(result, 'result') + console.timeEnd('time') From b1bb014e1fcc279b1046f1f841dcb9b7c9da9173 Mon Sep 17 00:00:00 2001 From: Eished Date: Sun, 3 Sep 2023 20:33:48 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E4=BD=BF=E7=94=A8esmodule=EF=BC=8C?= =?UTF-8?q?=E5=85=BC=E5=AE=B9ts=E5=92=8Cjs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typescript/{.eslintrc.js => .eslintrc.cjs} | 0 .../{.prettierrc.js => .prettierrc.cjs} | 0 typescript/package.json | 1 + typescript/src/index.js | 259 +----------------- typescript/src/nowcoder/examHuawei.js | 259 ++++++++++++++++++ typescript/src/nowcoder/examTop101.js | 3 + typescript/tsconfig.json | 110 ++++---- 7 files changed, 320 insertions(+), 312 deletions(-) rename typescript/{.eslintrc.js => .eslintrc.cjs} (100%) rename typescript/{.prettierrc.js => .prettierrc.cjs} (100%) create mode 100644 typescript/src/nowcoder/examHuawei.js create mode 100644 typescript/src/nowcoder/examTop101.js diff --git a/typescript/.eslintrc.js b/typescript/.eslintrc.cjs similarity index 100% rename from typescript/.eslintrc.js rename to typescript/.eslintrc.cjs diff --git a/typescript/.prettierrc.js b/typescript/.prettierrc.cjs similarity index 100% rename from typescript/.prettierrc.js rename to typescript/.prettierrc.cjs diff --git a/typescript/package.json b/typescript/package.json index d64c6e0..7194928 100644 --- a/typescript/package.json +++ b/typescript/package.json @@ -8,6 +8,7 @@ "lint": "eslint ./src --fix" }, "author": "", + "type": "module", "license": "ISC", "devDependencies": { "@types/node": "^18.6.5", diff --git a/typescript/src/index.js b/typescript/src/index.js index 54d18c6..a2a40c4 100644 --- a/typescript/src/index.js +++ b/typescript/src/index.js @@ -1,257 +1,2 @@ -// https://www.nowcoder.com/exam/oj/ta?page=1&tpId=37&type=37 -console.time('time') -function test1() { - const line = 'hello nowcoder' - const tokens = line.split(' ') - console.log(tokens[tokens.length - 1].length) -} - -let text = '' -function test2(line) { - if (!text) { - text = line - } else { - const reg = new RegExp(line, 'gi') - console.log(text.match(reg)?.length ?? 0) - } -} -// test2('ABCabc') -// test2('A') - -let total = 0 -const set = new Set() -function test3(line) { - if (total === 0) { - total = line - } else { - set.add(line) - } -} -// test3(4) -// test3(123) -// test3(2) -// test3(27) -// test3(2) -// test3(1) -// Array.from(set) -// .sort((a, b) => a - b) -// .forEach((s) => console.log(s)) - -function test4() { - let line = '123456789123456789123456789123456789123456789' - // for (let i = 0; i <= parseInt((line.length - 1) / 8); i++) { - // let str = line.slice(i * 8, i * 8 + 8) - // if (str.length < 8) { - // str = str + '0'.repeat(8 - str.length) - // // str = str.padEnd(8, '0') - // } - // console.log(str) - // } - let str = line + '00000000' - for (let i = 8; i < str.length; i += 8) { - console.log(str.substring(i - 8, i)) - } -} -// test4() - -function test5(number) { - console.log(parseInt(number, 16)) - console.log(Number(number)) - console.log(number.toString(10)) -} -// test5('0xAA') - -function test6() { - let num = 180 - const arr = [] - for (let i = 2; i * i <= num; i++) { - while (num % i === 0) { - console.log(num, i) - arr.push(i) - num = num / i - } - } - if (num > 1) { - arr.push(num) - } - console.log(arr.join(' ')) -} -// test6() - -function test7(num) { - console.log(Number(num).toFixed(0)) -} -// test7('5.5') -// test7('5.4') -// test7('2.499') - -const obj8 = {} -function test8(str) { - const strs = str.split(' ') - if (strs[1]) { - const value = obj8[strs[0]] - if (value) { - obj8[strs[0]] = Number(strs[1]) + Number(value) - } else { - obj8[strs[0]] = strs[1] - } - } -} -// test8('4') -// test8('3 4') -// test8('0 1') -// test8('1 2') -// test8('0 2') -// test8('0 2') -// Object.entries(map) -// .sort(([ak], [bk]) => ak - bk) -// .forEach(([k, v]) => console.log(k, v)) - -function test9(str) { - console.log([...new Set([...str].reverse())].join('')) - // console.log([...new Set(str)].reverse().join('')) -} -// test9('9876673') - -function test10(str) { - // const map10 = new Map() - // for (let i = 0; i < str.length; i++) { - // const s = str[i] - // const value = map10.get(s) - // map10.set(s, value) - // } - // console.log(map10.size) - console.log(new Set([...str]).size) -} -// test10('abc') -// test10('aaa') - -function test11(str) { - console.log([...str].reverse().join('')) -} -// test11('1516000') - -function test12(str) { - console.log([...str].reverse().join('')) -} -// test12('abcd') - -function test13(str) { - const strs = str.split(' ') - console.log(strs.reverse().join(' ')) -} -// test13('I am a boy') - -const arr14 = [] -let count14 -function test14(str) { - if (!count14) { - count14 = str - } else { - arr14.push(str) - } -} -// test14('9') -// test14('cap') -// test14('too') -// test14('boot') -// arr14.sort().forEach((a) => console.log(a)) - -function test15(str) { - console.log(parseInt(str, 10).toString(2).match(/1/g).length) -} -// test15('5') - -function shoppingCart() { - let items16 = [] - let n - let m - let id = 1 - function test16(str) { - const strs = str.split(' ') - const q = strs[2] - if (q === undefined) { - n = strs[0] - m = strs[1] - } else { - const v = strs[0] - const p = strs[1] - items16.push([v, p, q, id]) - id++ - } - } - test16('1000 5') - // test16('20 3 5') - // test16('20 3 5') - // test16('10 3 0') - // test16('10 2 0') - // test16('10 1 0') - test16('800 2 0') - test16('400 5 1') - test16('300 5 1') - test16('400 3 0') - test16('500 2 0') - // 分类 - // subItems16.forEach((si) => { - // // console.log(si) - // const index = mainItems16.findIndex((mi) => mi[3] == si[2]) - // mainItems16[index].childs.push(si) - // }) - // 穷举购物车 - function getAllSubsets(arr) { - const subsets = [[]] - for (const item of arr) { - const newSubsets = subsets.map((subset) => { - // console.log(subset, 'subset') - return [...subset, item] - }) - subsets.push(...newSubsets) - } - return subsets - } - items16 = getAllSubsets(items16) - // console.log(subsets) - - // 穷举结果 - let results = [] - items16 = items16.filter((sbs) => { - if (sbs.length == 0) { - return true - } else if (sbs.length == 1) { - if (sbs[0][0] < n) { - return true - } - } - let isParentIn = true - const childs = sbs.filter((sb) => sb[2] != 0) - if (childs.length) { - isParentIn = childs.every((s) => sbs.some((b) => b[3] == s[2])) - } - if (isParentIn) { - return true - } - }) - - items16 = items16.forEach((sbs) => { - let totalPrice = 0 - let satisfaction = 0 - sbs.forEach((item) => { - totalPrice += Number(item[0]) - satisfaction += Number(item[0]) * Number(item[1]) - // console.log(item) - }) - // console.log(sbs) - - if (totalPrice <= n) { - // console.log(totalPrice) - results.push(satisfaction) - } - }) - items16 = undefined - // console.log(results) - results = results.sort((a, b) => b - a) - - console.log(results[0]) -} - -console.timeEnd('time') +import './nowcoder/examHuawei.js' +import './nowcoder/examTop101.js' diff --git a/typescript/src/nowcoder/examHuawei.js b/typescript/src/nowcoder/examHuawei.js new file mode 100644 index 0000000..ccaa98e --- /dev/null +++ b/typescript/src/nowcoder/examHuawei.js @@ -0,0 +1,259 @@ +// 华为机试 +// https://www.nowcoder.com/exam/oj/ta?page=1&tpId=37&type=37 + +console.time('time') +function test1() { + const line = 'hello nowcoder' + const tokens = line.split(' ') + console.log(tokens[tokens.length - 1].length) +} + +let text = '' +function test2(line) { + if (!text) { + text = line + } else { + const reg = new RegExp(line, 'gi') + console.log(text.match(reg)?.length ?? 0) + } +} +// test2('ABCabc') +// test2('A') + +let total = 0 +const set = new Set() +function test3(line) { + if (total === 0) { + total = line + } else { + set.add(line) + } +} +// test3(4) +// test3(123) +// test3(2) +// test3(27) +// test3(2) +// test3(1) +// Array.from(set) +// .sort((a, b) => a - b) +// .forEach((s) => console.log(s)) + +function test4() { + let line = '123456789123456789123456789123456789123456789' + // for (let i = 0; i <= parseInt((line.length - 1) / 8); i++) { + // let str = line.slice(i * 8, i * 8 + 8) + // if (str.length < 8) { + // str = str + '0'.repeat(8 - str.length) + // // str = str.padEnd(8, '0') + // } + // console.log(str) + // } + let str = line + '00000000' + for (let i = 8; i < str.length; i += 8) { + console.log(str.substring(i - 8, i)) + } +} +// test4() + +function test5(number) { + console.log(parseInt(number, 16)) + console.log(Number(number)) + console.log(number.toString(10)) +} +// test5('0xAA') + +function test6() { + let num = 180 + const arr = [] + for (let i = 2; i * i <= num; i++) { + while (num % i === 0) { + console.log(num, i) + arr.push(i) + num = num / i + } + } + if (num > 1) { + arr.push(num) + } + console.log(arr.join(' ')) +} +// test6() + +function test7(num) { + console.log(Number(num).toFixed(0)) +} +// test7('5.5') +// test7('5.4') +// test7('2.499') + +const obj8 = {} +function test8(str) { + const strs = str.split(' ') + if (strs[1]) { + const value = obj8[strs[0]] + if (value) { + obj8[strs[0]] = Number(strs[1]) + Number(value) + } else { + obj8[strs[0]] = strs[1] + } + } +} +// test8('4') +// test8('3 4') +// test8('0 1') +// test8('1 2') +// test8('0 2') +// test8('0 2') +// Object.entries(map) +// .sort(([ak], [bk]) => ak - bk) +// .forEach(([k, v]) => console.log(k, v)) + +function test9(str) { + console.log([...new Set([...str].reverse())].join('')) + // console.log([...new Set(str)].reverse().join('')) +} +// test9('9876673') + +function test10(str) { + // const map10 = new Map() + // for (let i = 0; i < str.length; i++) { + // const s = str[i] + // const value = map10.get(s) + // map10.set(s, value) + // } + // console.log(map10.size) + console.log(new Set([...str]).size) +} +// test10('abc') +// test10('aaa') + +function test11(str) { + console.log([...str].reverse().join('')) +} +// test11('1516000') + +function test12(str) { + console.log([...str].reverse().join('')) +} +// test12('abcd') + +function test13(str) { + const strs = str.split(' ') + console.log(strs.reverse().join(' ')) +} +// test13('I am a boy') + +const arr14 = [] +let count14 +function test14(str) { + if (!count14) { + count14 = str + } else { + arr14.push(str) + } +} +// test14('9') +// test14('cap') +// test14('too') +// test14('boot') +// arr14.sort().forEach((a) => console.log(a)) + +function test15(str) { + console.log(parseInt(str, 10).toString(2).match(/1/g).length) +} +// test15('5') + +function shoppingCart() { + let items16 = [] + let n + let m + let id = 1 + function test16(str) { + const strs = str.split(' ') + const q = strs[2] + if (q === undefined) { + n = strs[0] + m = strs[1] + } else { + const v = strs[0] + const p = strs[1] + items16.push([v, p, q, id]) + id++ + } + } + test16('1000 5') + // test16('20 3 5') + // test16('20 3 5') + // test16('10 3 0') + // test16('10 2 0') + // test16('10 1 0') + test16('800 2 0') + test16('400 5 1') + test16('300 5 1') + test16('400 3 0') + test16('500 2 0') + // 分类 + // subItems16.forEach((si) => { + // // console.log(si) + // const index = mainItems16.findIndex((mi) => mi[3] == si[2]) + // mainItems16[index].childs.push(si) + // }) + // 穷举购物车 + function getAllSubsets(arr) { + const subsets = [[]] + for (const item of arr) { + const newSubsets = subsets.map((subset) => { + // console.log(subset, 'subset') + return [...subset, item] + }) + subsets.push(...newSubsets) + } + return subsets + } + items16 = getAllSubsets(items16) + // console.log(subsets) + + // 穷举结果 + let results = [] + items16 = items16.filter((sbs) => { + if (sbs.length == 0) { + return true + } else if (sbs.length == 1) { + if (sbs[0][0] < n) { + return true + } + } + let isParentIn = true + const childs = sbs.filter((sb) => sb[2] != 0) + if (childs.length) { + isParentIn = childs.every((s) => sbs.some((b) => b[3] == s[2])) + } + if (isParentIn) { + return true + } + }) + + items16 = items16.forEach((sbs) => { + let totalPrice = 0 + let satisfaction = 0 + sbs.forEach((item) => { + totalPrice += Number(item[0]) + satisfaction += Number(item[0]) * Number(item[1]) + // console.log(item) + }) + // console.log(sbs) + + if (totalPrice <= n) { + // console.log(totalPrice) + results.push(satisfaction) + } + }) + items16 = undefined + // console.log(results) + results = results.sort((a, b) => b - a) + + console.log(results[0]) +} + +console.timeEnd('time') diff --git a/typescript/src/nowcoder/examTop101.js b/typescript/src/nowcoder/examTop101.js new file mode 100644 index 0000000..7c9b941 --- /dev/null +++ b/typescript/src/nowcoder/examTop101.js @@ -0,0 +1,3 @@ +// 面试必刷TOP101 +// https://www.nowcoder.com/exam/oj?page=1 +console.log(1); \ No newline at end of file diff --git a/typescript/tsconfig.json b/typescript/tsconfig.json index 8c2935e..dc9a5de 100644 --- a/typescript/tsconfig.json +++ b/typescript/tsconfig.json @@ -1,55 +1,55 @@ -{ - // "extends": "@tsconfig/node16/tsconfig.json", - "compilerOptions": { - /* 基本选项 */ - "target": "ES6", // 指定 ECMAScript 目标版本: 'ES3' (default), 'ES5', 'ES6'/'ES2015', 'ES2016', 'ES2017', or 'ESNEXT' - "module": "CommonJS", // 指定使用模块: 'commonjs', 'amd', 'system', 'umd' or 'es2015' - "lib": [], // 指定要包含在编译中的库文件 - "allowJs": true, // 允许编译 javascript 文件 - "checkJs": true, // 报告 javascript 文件中的错误 - "jsx": "preserve", // 指定 jsx 代码的生成: 'preserve', 'react-native', or 'react' - "declaration": true, // 生成相应的 '.d.ts' 文件 - // "sourceMap": true, // 生成相应的 '.map' 文件 - // "outFile": "./", // 将输出文件合并为一个文件 - "outDir": "./build", // 指定输出目录 - "rootDir": "./", // 用来控制输出目录结构 --outDir. - "removeComments": true, // 删除编译后的所有的注释 - "noEmit": false, // 不生成输出文件 - "importHelpers": false, // 从 tslib 导入辅助工具函数 - "isolatedModules": false, // 将每个文件作为单独的模块 (与 'ts.transpileModule' 类似). - - /* 严格的类型检查选项 */ - "strict": true, // 启用所有严格类型检查选项 - "noImplicitAny": true, // 在表达式和声明上有隐含的 any类型时报错 - "strictNullChecks": true, // 启用严格的 null 检查 - "noImplicitThis": true, // 当 this 表达式值为 any 类型的时候,生成一个错误 - "alwaysStrict": true, // 以严格模式检查每个模块,并在每个文件里加入 'use strict' - - /* 额外的检查 */ - "noUnusedLocals": false, // 有未使用的变量时,抛出错误 - "noUnusedParameters": false, // 有未使用的参数时,抛出错误 - "noImplicitReturns": true, // 并不是所有函数里的代码都有返回值时,抛出错误 - "noFallthroughCasesInSwitch": true, // 报告 switch 语句的 fallthrough 错误。(即,不允许 switch 的 case 语句贯穿) - - /* 模块解析选项 */ - "moduleResolution": "node", // 选择模块解析策略: 'node' (Node.js) or 'classic' (TypeScript pre-1.6) - "baseUrl": "./", // 用于解析非相对模块名称的基目录 - "paths": {}, // 模块名到基于 baseUrl 的路径映射的列表 - "rootDirs": [], // 根文件夹列表,其组合内容表示项目运行时的结构内容 - // "typeRoots": [], // 包含类型声明的文件列表 - // "types": [], // 需要包含的类型声明文件名列表 - "allowSyntheticDefaultImports": true, // 允许从没有设置默认导出的模块中默认导入。 - - /* Source Map Options */ - // "sourceRoot": "./", // 指定调试器应该找到 TypeScript 文件而不是源文件的位置 - // "mapRoot": "./", // 指定调试器应该找到映射文件而不是生成文件的位置 - // "inlineSourceMap": true, // 生成单个 soucemaps 文件,而不是将 sourcemaps 生成不同的文件 - // "inlineSources": true, // 将代码与 sourcemaps 生成到一个文件中,要求同时设置了 --inlineSourceMap 或 --sourceMap 属性 - - /* 其他选项 */ - "experimentalDecorators": true, // 启用装饰器 - "emitDecoratorMetadata": true // 为装饰器提供元数据的支持 - }, - "include": ["./src"], - "exclude": ["./node_modules"] -} +{ + // "extends": "@tsconfig/node16/tsconfig.json", + "compilerOptions": { + /* 基本选项 */ + "target": "ES6", // 指定 ECMAScript 目标版本: 'ES3' (default), 'ES5', 'ES6'/'ES2015', 'ES2016', 'ES2017', or 'ESNEXT' + "module": "CommonJS", // 指定使用模块: 'commonjs', 'amd', 'system', 'umd' or 'es2015' + "lib": [], // 指定要包含在编译中的库文件 + "allowJs": true, // 允许编译 javascript 文件 + "checkJs": true, // 报告 javascript 文件中的错误 + "jsx": "preserve", // 指定 jsx 代码的生成: 'preserve', 'react-native', or 'react' + "declaration": true, // 生成相应的 '.d.ts' 文件 + // "sourceMap": true, // 生成相应的 '.map' 文件 + // "outFile": "./", // 将输出文件合并为一个文件 + "outDir": "./build", // 指定输出目录 + "rootDir": "./", // 用来控制输出目录结构 --outDir. + "removeComments": true, // 删除编译后的所有的注释 + "noEmit": false, // 不生成输出文件 + "importHelpers": false, // 从 tslib 导入辅助工具函数 + "isolatedModules": false, // 将每个文件作为单独的模块 (与 'ts.transpileModule' 类似). + + /* 严格的类型检查选项 */ + "strict": true, // 启用所有严格类型检查选项 + "noImplicitAny": true, // 在表达式和声明上有隐含的 any类型时报错 + "strictNullChecks": true, // 启用严格的 null 检查 + "noImplicitThis": true, // 当 this 表达式值为 any 类型的时候,生成一个错误 + "alwaysStrict": true, // 以严格模式检查每个模块,并在每个文件里加入 'use strict' + + /* 额外的检查 */ + "noUnusedLocals": false, // 有未使用的变量时,抛出错误 + "noUnusedParameters": false, // 有未使用的参数时,抛出错误 + "noImplicitReturns": true, // 并不是所有函数里的代码都有返回值时,抛出错误 + "noFallthroughCasesInSwitch": true, // 报告 switch 语句的 fallthrough 错误。(即,不允许 switch 的 case 语句贯穿) + + /* 模块解析选项 */ + "moduleResolution": "node", // 选择模块解析策略: 'node' (Node.js) or 'classic' (TypeScript pre-1.6) + "baseUrl": "./", // 用于解析非相对模块名称的基目录 + "paths": {}, // 模块名到基于 baseUrl 的路径映射的列表 + "rootDirs": [], // 根文件夹列表,其组合内容表示项目运行时的结构内容 + // "typeRoots": [], // 包含类型声明的文件列表 + // "types": [], // 需要包含的类型声明文件名列表 + "allowSyntheticDefaultImports": true, // 允许从没有设置默认导出的模块中默认导入。 + + /* Source Map Options */ + // "sourceRoot": "./", // 指定调试器应该找到 TypeScript 文件而不是源文件的位置 + // "mapRoot": "./", // 指定调试器应该找到映射文件而不是生成文件的位置 + // "inlineSourceMap": true, // 生成单个 soucemaps 文件,而不是将 sourcemaps 生成不同的文件 + // "inlineSources": true, // 将代码与 sourcemaps 生成到一个文件中,要求同时设置了 --inlineSourceMap 或 --sourceMap 属性 + + /* 其他选项 */ + "experimentalDecorators": true, // 启用装饰器 + "emitDecoratorMetadata": true // 为装饰器提供元数据的支持 + }, + "include": ["./src/**/*.ts"], + "exclude": ["./node_modules", "**/*.js"] +} From 8db47ec233b34cacdc6291d917f0929c2a7724a7 Mon Sep 17 00:00:00 2001 From: Eished Date: Tue, 5 Sep 2023 18:14:14 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=9D=A2=E8=AF=95?= =?UTF-8?q?=E5=BF=85=E5=88=B7TOP101?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typescript/src/nowcoder/examTop101.js | 30 ++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/typescript/src/nowcoder/examTop101.js b/typescript/src/nowcoder/examTop101.js index 7c9b941..87303ae 100644 --- a/typescript/src/nowcoder/examTop101.js +++ b/typescript/src/nowcoder/examTop101.js @@ -1,3 +1,31 @@ // 面试必刷TOP101 // https://www.nowcoder.com/exam/oj?page=1 -console.log(1); \ No newline at end of file + +function BM1() { + class ListNode { + constructor(x) { + this.val = x + if (x < 3) { + this.next = new ListNode(x + 1) + } + } + } + + function ReverseList(head) { + // write code here + let pre = null + let cur = head + while (cur) { + // 前插法 + const nextTemp = cur.next + cur.next = pre // 反转当前节点 + pre = cur // 前置节点后移 + cur = nextTemp // 当前节点后移 + } + return pre + } + + console.log(new ListNode(1)) + console.log(ReverseList(new ListNode(1))) +} +BM1() From d6caf887f61c69c6a0530c604b501cc5eccf7ede Mon Sep 17 00:00:00 2001 From: Eished Date: Wed, 6 Sep 2023 23:00:15 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BF=AB=E6=8E=92?= =?UTF-8?q?=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typescript/src/index.js | 1 + typescript/src/nowcoder/examTop101.js | 2 +- typescript/src/nowcoder/quickSort.js | 54 +++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 typescript/src/nowcoder/quickSort.js diff --git a/typescript/src/index.js b/typescript/src/index.js index a2a40c4..312c3d7 100644 --- a/typescript/src/index.js +++ b/typescript/src/index.js @@ -1,2 +1,3 @@ import './nowcoder/examHuawei.js' import './nowcoder/examTop101.js' +import './nowcoder/quickSort.js' diff --git a/typescript/src/nowcoder/examTop101.js b/typescript/src/nowcoder/examTop101.js index 87303ae..984af5d 100644 --- a/typescript/src/nowcoder/examTop101.js +++ b/typescript/src/nowcoder/examTop101.js @@ -28,4 +28,4 @@ function BM1() { console.log(new ListNode(1)) console.log(ReverseList(new ListNode(1))) } -BM1() +// BM1() diff --git a/typescript/src/nowcoder/quickSort.js b/typescript/src/nowcoder/quickSort.js new file mode 100644 index 0000000..26f90d8 --- /dev/null +++ b/typescript/src/nowcoder/quickSort.js @@ -0,0 +1,54 @@ +function partition(arr, start, end) { + // 以最后一个元素为基准 + const pivotValue = arr[end] + let pivotIndex = start + for (let i = start; i < end; i++) { + if (arr[i] < pivotValue) { + // 交换元素 + ;[arr[i], arr[pivotIndex]] = [arr[pivotIndex], arr[i]] + // 移动到下一个元素 + pivotIndex++ + } + } + + // 把基准值放在中间 + ;[arr[pivotIndex], arr[end]] = [arr[end], arr[pivotIndex]] + return pivotIndex +} + +function quickSortRecursive(arr, start, end) { + // 终止条件 + if (start >= end) { + return + } + + // 返回 pivotIndex + let index = partition(arr, start, end) + + // 将相同的逻辑递归地用于左右子数组 + quickSortRecursive(arr, start, index - 1) + quickSortRecursive(arr, index + 1, end) +} +const array = [7, -2, 4, 7, 1, 3, 6, 5, 3, 0, -4, 2] +// quickSortRecursive(array, 0, array.length - 1) +// console.log(array) + +const myQuickSort = (arr) => { + if (arr.length <= 1) { + return arr + } + const pivotIndex = Math.floor(arr.length / 2) + const pivot = arr.splice(pivotIndex, 1)[0] + const left = [] + const right = [] + + arr.forEach((num) => { + if (num < pivot) { + left.push(num) + } else { + right.push(num) + } + }) + return myQuickSort(left).concat([pivot], myQuickSort(right)) +} +console.log(myQuickSort(array)) From 652535ce3a50d21f2af0d952c1394976a4116f58 Mon Sep 17 00:00:00 2001 From: Eished Date: Thu, 7 Sep 2023 01:53:49 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=8F=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E5=92=8C=E5=BE=AE=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typescript/src/index.js | 1 + typescript/src/nowcoder/microTask.js | 55 ++++++++++++++++++++++++++++ typescript/src/nowcoder/quickSort.js | 2 +- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 typescript/src/nowcoder/microTask.js diff --git a/typescript/src/index.js b/typescript/src/index.js index 312c3d7..b97bbc9 100644 --- a/typescript/src/index.js +++ b/typescript/src/index.js @@ -1,3 +1,4 @@ import './nowcoder/examHuawei.js' import './nowcoder/examTop101.js' import './nowcoder/quickSort.js' +import './nowcoder/microTask.js' diff --git a/typescript/src/nowcoder/microTask.js b/typescript/src/nowcoder/microTask.js new file mode 100644 index 0000000..18fa7dd --- /dev/null +++ b/typescript/src/nowcoder/microTask.js @@ -0,0 +1,55 @@ +// 面试题目一 + +async function async1() { + console.log('async1 start') // 2 + await async2() + console.log('async1 end') // 7 +} + +async function async2() { + console.log('async2') // 3 +} + +console.log('script start') // 1 + +setTimeout(function () { + console.log('setTimeout0') +}, 0) // 12 + +setTimeout(function () { + console.log('setTimeout2') +}, 300) // 13 + +setImmediate(() => console.log('setImmediate')) // 11 + +process.nextTick(() => console.log('nextTick1')) // 9 + +async1() // 2 + +new Promise(function (resolve) { + console.log('promise1') // 4 + resolve() + console.log('promise2') // 5 +}).then(function () { + console.log('promise3') // 8 +}) + +process.nextTick(() => console.log('nextTick2')) // 10 + +console.log('script end') // 6 + +/** + * script start +async1 start +async2 +promise1 +promise2 +script end +async1 end +promise3 +nextTick1 +nextTick2 +setImmediate +setTimeout0 +setTimeout2 + */ diff --git a/typescript/src/nowcoder/quickSort.js b/typescript/src/nowcoder/quickSort.js index 26f90d8..1d71964 100644 --- a/typescript/src/nowcoder/quickSort.js +++ b/typescript/src/nowcoder/quickSort.js @@ -51,4 +51,4 @@ const myQuickSort = (arr) => { }) return myQuickSort(left).concat([pivot], myQuickSort(right)) } -console.log(myQuickSort(array)) +// console.log(myQuickSort(array)) From b0c29b810fe77d36c0d3e85abc9bb95b4eb434a1 Mon Sep 17 00:00:00 2001 From: Eished Date: Mon, 11 Sep 2023 10:55:55 +0800 Subject: [PATCH 7/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=8C=E5=88=86?= =?UTF-8?q?=E6=9F=A5=E6=89=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typescript/src/nowcoder/examTop101.js | 34 ++++++++++++++++ typescript/src/nowcoder/microTask.js | 57 ++++++++++++++------------- 2 files changed, 64 insertions(+), 27 deletions(-) diff --git a/typescript/src/nowcoder/examTop101.js b/typescript/src/nowcoder/examTop101.js index 984af5d..b78c53c 100644 --- a/typescript/src/nowcoder/examTop101.js +++ b/typescript/src/nowcoder/examTop101.js @@ -29,3 +29,37 @@ function BM1() { console.log(ReverseList(new ListNode(1))) } // BM1() + +function BM17() { + // 二分查找 + const target = 13 + const arr = [-1, 0, 3, 4, 6, 10, 13, 14] + + const search = function (nums, target) { + // 在区间[left,right]中查找元素,左闭右闭 + let left = 0 + let right = nums.length - 1 + while (left <= right) { + // 计算中间点 + let mid = parseInt(left + (right - left) / 2) + if (target == nums[mid]) { + return mid + // 如果target < nums[mid],表示目标值可能在左半边 + } else if (target < nums[mid]) { + right = mid - 1 + // 如果target > nums[mid],表示目标值可能在右半边 + } else if (target > nums[mid]) { + left = mid + 1 + } + } + // 未找到返回-1 + return -1 + } + + console.log(search(arr, target)) +} +BM17() + +function BM23() { + console.log(1) +} diff --git a/typescript/src/nowcoder/microTask.js b/typescript/src/nowcoder/microTask.js index 18fa7dd..311be5b 100644 --- a/typescript/src/nowcoder/microTask.js +++ b/typescript/src/nowcoder/microTask.js @@ -1,42 +1,45 @@ // 面试题目一 -async function async1() { - console.log('async1 start') // 2 - await async2() - console.log('async1 end') // 7 -} +function events() { + async function async1() { + console.log('async1 start') // 2 + await async2() + console.log('async1 end') // 7 + } -async function async2() { - console.log('async2') // 3 -} + async function async2() { + console.log('async2') // 3 + } -console.log('script start') // 1 + console.log('script start') // 1 -setTimeout(function () { - console.log('setTimeout0') -}, 0) // 12 + setTimeout(function () { + console.log('setTimeout0') + }, 0) // 12 -setTimeout(function () { - console.log('setTimeout2') -}, 300) // 13 + setTimeout(function () { + console.log('setTimeout2') + }, 300) // 13 -setImmediate(() => console.log('setImmediate')) // 11 + setImmediate(() => console.log('setImmediate')) // 11 -process.nextTick(() => console.log('nextTick1')) // 9 + process.nextTick(() => console.log('nextTick1')) // 9 -async1() // 2 + async1() // 2 -new Promise(function (resolve) { - console.log('promise1') // 4 - resolve() - console.log('promise2') // 5 -}).then(function () { - console.log('promise3') // 8 -}) + new Promise(function (resolve) { + console.log('promise1') // 4 + resolve() + console.log('promise2') // 5 + }).then(function () { + console.log('promise3') // 8 + }) -process.nextTick(() => console.log('nextTick2')) // 10 + process.nextTick(() => console.log('nextTick2')) // 10 -console.log('script end') // 6 + console.log('script end') // 6 +} +// events() /** * script start From 0dedd487c7e60a1e96a4f289bf82b59a0e2e7ad9 Mon Sep 17 00:00:00 2001 From: Eished Date: Tue, 12 Sep 2023 22:47:31 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E5=89=8D=E5=BA=8F=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typescript/src/nowcoder/examTop101.js | 43 +++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/typescript/src/nowcoder/examTop101.js b/typescript/src/nowcoder/examTop101.js index b78c53c..1e26080 100644 --- a/typescript/src/nowcoder/examTop101.js +++ b/typescript/src/nowcoder/examTop101.js @@ -58,8 +58,47 @@ function BM17() { console.log(search(arr, target)) } -BM17() +// BM17() function BM23() { - console.log(1) + // 二叉树的前序遍历 + class TreeNode { + constructor(val) { + this.val = val + this.left = null + this.right = null + } + } + + function preorderTraversal(root) { + const result = [] + + function traverse(node) { + if (node === null) { + return + } + // 访问当前节点的值 + result.push(node.val) + // 递归遍历左子树 + traverse(node.left) + // 递归遍历右子树 + traverse(node.right) + } + + traverse(root) // 从根节点开始遍历 + + return result + } + + // 创建一个二叉树 + const root = new TreeNode(1) + root.left = new TreeNode(2) + root.right = new TreeNode(3) + root.left.left = new TreeNode(4) + root.left.right = new TreeNode(5) + + // 执行前序遍历 + const result = preorderTraversal(root) + console.log(result) // 输出 [1, 2, 4, 5, 3] } +BM23() From f6e1098d0e93348645d1e0f32f875f2118333115 Mon Sep 17 00:00:00 2001 From: Eished Date: Thu, 21 Sep 2023 23:57:18 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B1=82=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typescript/src/nowcoder/examTop101.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/typescript/src/nowcoder/examTop101.js b/typescript/src/nowcoder/examTop101.js index 1e26080..cf78d1e 100644 --- a/typescript/src/nowcoder/examTop101.js +++ b/typescript/src/nowcoder/examTop101.js @@ -101,4 +101,29 @@ function BM23() { const result = preorderTraversal(root) console.log(result) // 输出 [1, 2, 4, 5, 3] } -BM23() +// BM23() + +function match() { + const str = 'aeiou wolppzxc alwmdf String prototype' + const result = str.match(/a|e|i|o|u/g) + console.log(result, result.length) +} +// match() + +function getMaxDepth() { + const arr = [1, 1, [2, 2], [2, [3, 3, [4, 4], [4, [5, 5]]]]] + const depth = [] + const getDepth = (arr, deep) => { + arr.forEach((a) => { + if (Array.isArray(a)) { + getDepth(a, deep + 1) + } else { + depth.push(deep) + } + }) + } + + getDepth(arr, 1) + console.log(Math.max(...depth)) +} +getMaxDepth()