题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
方法一:用sort排序,这样做体现不出什么知识点,出题人应该也不想考察这个
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { // Write your code here let tokens = []; while ((line = await readline())) { tokens.push(line); } let arr = tokens.slice(1, tokens.length); arr.sort() arr.forEach((item) => { console.log(item); }); })();方法二:自己写排序算法和判断逻辑
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { // Write your code here let tokens = []; while ((line = await readline())) { tokens.push(line); } let arr = tokens.slice(1, tokens.length); for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr.length - 1 - i; j++) { if (!func(arr[j], arr[j + 1], 0)) { let temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; } } } arr.forEach((item) => { console.log(item); }); function func(a, b, j) { if (!a[j]) { return true; } if (!b[j]) { return false; } if (a[j] < b[j]) { return true; } else if (a[j] == b[j]) { return func(a, b, j + 1); } else { return false; } } })();