Partition Array According to Given Pivot
Partition一个array, 要求stable..
这个c++有stable_partition的方法, java没有..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Solution { public int[] pivotArray(int[] nums, int pivot) { List<Integer> s = new ArrayList<>(); List<Integer> e = new ArrayList<>(); List<Integer> g = new ArrayList<>(); for(int n : nums){ if(n < pivot) s.add(n); else if(n == pivot) e.add(n); else g.add(n); } s.addAll(e); s.addAll(g); int n = nums.length; int[] res = new int[n]; for(int i = 0; i < n; i++){ res[i] = s.get(i); } return res; } } |