We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
已知数组
var arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10]
function flat(arr) { return [...new Set(arr.flat(Infinity).sort((a,b) => a - b))] }
function flat(arr) { return [...new Set(arr.toString().split(',').sort((a,b) => a-b))] }
function flat(arr) { const flatArr = arr.reduce((res, next) => Array.isArray(next) ? res.concat(flat(next)) : res.concat(next),[]) return [...new Set(flatArr.sort((a,b) => a - b))] }
// 使用 栈 来实现 // 由于栈后进先出的特点,先把原数组倒序 function flat(arr) { let stack = [] let res = [] for (let i = arr.length - 1; i >= 0; i--) { stack.push(arr[i]) } while (stack.length) { const item = stack.pop() if (Array.isArray(item)) { for (let i = item.length - 1; i >= 0; i--) { stack.push(item[i]) } } else { res.push(item) } } return [...new Set(res)].sort((a,b) => a - b) }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
已知数组
解法一
解法二
解法三(使用递归实现 flat 函数)
解法四(非递归实现 flat 函数)
The text was updated successfully, but these errors were encountered: