Why Runtime error?


  • 0
    J

    My code can run in VS2013 and get the right answer, but I get run time error for input (3, 1).

    class Solution {
    public:
    string getPermutation(int n, int k) {
    string result;
    if (n <= 0 || k <= 0)
    return result;
    int* fac = new int[n];
    fac[0] = fac[1] = 1;
    for (int i = 2; i <= n; i++)
    {
    fac[i] = fac[i - 1] * i;
    }

    	if (k > fac[n])
    		return result;
    
    	vector<char> left;
    	for (int i = 0; i < n; i++)
    	{
    		left.push_back(i + '1');
    	}
    
    	k--;
    	for (int i = 0; i < n; i++)
    	{
    		int index = k / fac[n - i - 1];
    		char cur = left[index];
    		result.push_back(cur);
    		std::remove(left.begin(), left.end(), cur);
    		k -= index * fac[n - i - 1];
    	}
    	return result;
    }
    

    };


Log in to reply
 

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