The correct, optimal and working solution for programming question next-permutation on leetcode

class Solution { public: void nextPermutation(vector<int> &num) { bool ans = next_permutation(num.begin(), num.end()); if( ans == false ) { sort(num.begin(), num.end()); } } };