请编写一个函数,其功能为输入一个字符串,打印出该字符串中字符的所有排列

exchange(str, i, j){
      let arr = str.split("")
      if(str == ""){
        return 
      }else {
        let tmp = arr[i]
        arr[i] = arr[j]
        arr[j] = tmp
      }
      this.str = arr.join("")
      // console.log("str", this.str)
    },
    fullPermutationsOfString(str, begin, end){
      str = this.str
      if(str == "" || begin > end){
        return
      }
      if(begin == end - 1){
        console.log(str)
      }else {
        for(let i = begin; i < end; ++i){
          this.exchange(str, begin, i)
          this.fullPermutationsOfString(str, begin+1, end)
          this.exchange(str, begin, i)
        }
      }
    }
    this.str = "ab"
    let length = this.str.length
    this.fullPermutationsOfString(this.str, 0, length)

 

posted @ 2020-08-09 19:06  sinceForever  阅读(456)  评论(0编辑  收藏  举报