# I pass this problem by using iterator but when I use array I get runtime error,could somebody help me?

• here is the solution using array:
class Solution {

``````    public:
void nextPermutation(vector<int>& nums) {
int len=nums.size();
int i=0;
int j=0;
for(i=len-2;i>=0;i--){
if(nums[i]<nums[i+1]) break;
}
if(i==0){
reverse(nums.begin(),nums.end());
}
else{
for(j=len-1;j>=i;j--){
if(nums[j]>nums[i]) break;
}
swap(nums[i],nums[j]);
reverse(nums.begin()+i+1,nums.end());
}

}
};
``````

Here is the solution using iterator which passed:

``````class Solution {
public:
void nextPermutation(vector<int>& nums) {
permute(nums.begin(),nums.end());
}
void permute(vector<int>::iterator first,vector<int>::iterator last){
vector<int>::reverse_iterator rfirst(last);
vector<int>::reverse_iterator rlast(first);
vector<int>::reverse_iterator p=next(rfirst);
vector<int>::reverse_iterator q;
while(p!=rlast&&*p>=*prev(p)){
p++;
}
if(p==rlast){
reverse(rfirst,rlast);
}
else{
for(q=rfirst;q<=p;q++){
if(*q>*p) break;
}
swap(*p,*q);
reverse(rfirst,p);
}
}
};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.