Given an array A and a number b.
Return a reordered array such that all numbers before b are smaller than or equal to b, and all numbers after b are greater than or equal to b.
Example:
A = ["1", "2", "5", "3", "4", "8"], b = 3
Good: ["1", "2", "3", "4", "5", "8"] or ["2", "1", "3", "8", "5", "4"] or …
Bad: ["1", "3", "2", "4", "5", "8"]
这是一道典型的数组分区题,要求围绕给定的基准值 b 重新排列数组:b 左侧都要小于等于 b,右侧都要大于等于 b。题目通常不要求保持原有相对顺序,因此可以用双指针或一次遍历配合分区思想快速完成,核心在于把数组按 pivot 切成两部分,而不是做完整排序。
正文完