原题题目
代码实现(首刷自解0ms)
class Solution {
public:bool backtracking(vector<bool>& temp,vector<int>& visit,int pos,int size,int n,int totalsize){
if(size == n) return true;if(visit[pos]) return backtracking(temp,visit,pos+1,size,n,totalsize);for(int i=n;i>=1;--i){
if(temp[i] || (i != 1 && (pos+i>totalsize || visit[pos+i]))) continue;visit[pos] = i;if(i!=1) visit[pos+i] = i; temp[i] = true;if(backtracking(temp,visit,pos+1,size+1,n,totalsize)) return true;visit[pos] = 0;if(i!=1) visit[pos+i] = 0; temp[i] = false;} return false;}vector<int> constructDistancedSequence(int n) {
vector<int> visit(2*n-1,0);vector<bool> temp(n+1,false);vector<int> ret;if(backtracking(temp,visit,0,0,n,2*n-2)) return visit;return ret; }
};