Advertisement
darrellp

Leet rearrange

Feb 20th, 2022
1,151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.34 KB | None | 0 0
  1. void Rearrange(List<int> v)
  2. {
  3.     int[] counts = new int[3];
  4.     foreach (var e in v)
  5.     {
  6.         counts[e]++;
  7.     }
  8.    
  9.     int iNext = 0;
  10.     int limit = counts[0];
  11.     for (; iNext < limit; iNext++)
  12.     {
  13.         v[iNext] = 0;
  14.     }
  15.     limit += counts[1];
  16.     for (; iNext < limit; iNext++)
  17.     {
  18.         v[iNext] = 1;
  19.     }
  20.     for (; iNext < v.Count; iNext++)
  21.     {
  22.         v[iNext] = 2;
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement