LC Math Problem

128 阅读1分钟

Next Permutation -- Math Problem

Snipaste_2023-06-11_22-37-16.png

use 2 pointers:

  • put first pointer i at the second last position.
  • put second pt j at the last position and judge if the val at j is greater than it is at i do the while judge each time until the position of j == i.
  • in each while iteration, judge if the val at j is greater than it is ati. if so, break the inner while-loop, or j--.
  • if the inner while loop is done, then swap the var at i and j and break the outer while loop.
  • reverse all elems from i+1 to the end;