Skip to content

Commit f3c05ab

Browse files
add 2161
1 parent 01e68fb commit f3c05ab

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Diff for: contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt

+21
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,25 @@ private fun IntArray.swap(from: Int, to: Int) {
9595
this[to] = temp
9696
}
9797

98+
/**
99+
* 2161. Partition Array According to Given Pivot
100+
*/
101+
fun pivotArray(nums: IntArray, pivot: Int): IntArray {
102+
if (nums.hasSingle()) return nums
103+
val res = mutableListOf<Int>()
104+
for (i in 0 until nums.size) {
105+
if (nums[i] < pivot) res.add(nums[i])
106+
}
107+
for (i in 0 until nums.size) {
108+
if (nums[i] == pivot) res.add(nums[i])
109+
}
110+
for (i in 0 until nums.size) {
111+
if (nums[i] > pivot) res.add(nums[i])
112+
}
113+
114+
return res.toIntArray()
115+
}
116+
117+
118+
98119

0 commit comments

Comments
 (0)