You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
同时判断 if (i == index || arr[i] !== arr[index]),如果交换的数的值不同,或者,i == idx,继续递归
回朔法
functionswap(arr,x,y){vartemp=arr[x];arr[x]=arr[y];arr[y]=temp;}//create the arr of resultvarresult=[];//this is recursive fnfunctiongetResult(arr,index){//base caseif(arr.length-1==index){result.push(arr.join(''));return;}// recursive partfor(leti=index;i<arr.length;++i){if(i==index||arr[i]!==arr[index]){swap(arr,index,i);getResult(arr,index+1);swap(arr,index,i);}}}// ------- test -------functionPermutation(str){// if emptyif(!str)return[];// if not emptyvararr=str.split("").sort();// convert into arrgetResult(arr,0);// return arr of resultreturnresult.sort();}console.log(Permutation('abc'));
The text was updated successfully, but these errors were encountered:
字符串全排列
很好的图
步骤:
递归
part)for循环
part)if (i == index || arr[i] !== arr[index])
,如果交换的数的值不同,或者,i == idx,继续递归
回朔法
The text was updated successfully, but these errors were encountered: