DEV Community

Tim-Wu-330
Tim-Wu-330

Posted on

Algorithm Problem Set

Next Permutation -- Math Problem

Image description

  • 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 at i. 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 at the nums;

Top comments (0)