zoukankan      html  css  js  c++  java
  • 剑指 Offer 38. 字符串的排列

    剑指 Offer 38. 字符串的排列

    地址:剑指 Offer 38. 字符串的排列

    输入一个字符串,打印出该字符串中字符的所有排列。

    你可以以任意顺序返回这个字符串数组,但里面不能有重复元素。

    示例:

    输入:s = "abc"
    输出:["abc","acb","bac","bca","cab","cba"]

    限制:

    1 <= s 的长度 <= 8

    
    
    //var res []string
    //var path []byte
    var st []bool
    var judgeMap map[string]bool
    
    func permutation(s string) []string {
        length := len(s)
        path := make([]byte, 0)
        res := make([]string, 0)
        judgeMap = make(map[string]bool)
        st = make([]bool, length)
        dfs(s, 0, path, &res)
        return res
    }
    
    func dfs(s string, pathLen int, path []byte, res *[]string) {
        if pathLen == len(s) {
            //fmt.Println(path)
            if _ ,ok := judgeMap[string(path)]; !ok {
                (*res) = append(*res, string(path))
                judgeMap[string(path)] = true
            }
    
        }
        for i := 0; i < len(s); i++ {
            if st[i] == true {
                continue
            }
            st[i] = true
            path = append(path, s[i])
            dfs(s, pathLen+1, path, res)
            path = path[:len(path)-1]
            st[i] = false
        }
    }
    
  • 相关阅读:
    React之JSX语法
    Visual Studio Code 使用 Typings 实现智能提示功能
    React.js 之hello word
    Linux命令详解-cd
    Linux命令详解-ls
    linux常用命令
    LINUX系统配置相关
    netsh
    Visual Studio
    乘法算术表
  • 原文地址:https://www.cnblogs.com/ganshuoos/p/14284546.html
Copyright © 2011-2022 走看看