剑指offer之字符串的全排列

简介: 剑指offer之字符串的全排列

1 问题

求字符串的全排列,比如字符串abc,它的全排列如下

abc, acb, bac, bca, cad, cba


2 思路

我们先固定第一个字符,这里的第一个字符肯定是这个字符串里面字符串的全子集(不包含重复),abc字符串,我们a和a交换,这里也就是固定a字符的,然后我们只需要求出后面bc字符的全排列,这个时候我们可以用递归,我们依然b和b进行交换,c和c进行交换,到了末尾我们递归就结束,记得return.我们可以得出bc全排列的一种既bc,然后我们b和c需要交换位置,变成了cb,然后我们b和b进行了一次交换,然后我们可以得到bc全排列的一种既cb,所以固定a字符,我们可以得到全排列字符串abc acb,所以依次类推,我们可以固定b字符和c字符求出所以字符串的全排列


3 代码实现

c++版本

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Permutation{
private:
    void permutation(string str, int begin);
public:
    vector<string> permutation(string str);
    vector<string> result;
};
vector<string> Permutation::permutation(string str)
{
    if (str.length() == 0)
    {
  return result;
    }
    permutation(str, 0);
    sort(result.begin(), result.end());   
    return result;
}
void Permutation::permutation(string str, int begin)
{
    if (begin == str.length())
    {
  result.push_back(str);
  return;
    }
    for (int i = begin; i < str.length(); ++i)
    {
  if (i != begin && str[i] == str[begin])
            continue;
  //这里开始交换第一个元素和后面的元素,也就是起到固定的作用
        swap(str[begin], str[i]);
  //递归后面的子字符串
        permutation(str, begin + 1);
  //交换了要记得还原
  swap(str[i], str[begin]);
   }
} 
int main()
{
    Permutation h;
    h.permutation("abc");
    vector<string> result = h.result;
    for(int i = 0; i < result.size(); ++i)
    {
        std::cout << result[i] << std::endl;  
    }
    return 0;
}

c版本

#include <stdio.h>
void permutation(char *str);
void permutation_core(char *str, char *begin);
void permutation(char *str)
{
    if (str == NULL) 
    {
        printf("str is NULL\n");
        return;
    }
    permutation_core(str, str);
}
void permutation_core(char *str, char *begin)
{
   if (*begin == '\0')
   {
       printf("%s\n", str);
   }
   else
   {
    for (char *ch = begin; *ch != '\0'; ++ch)
    {
      char temp = *ch;
            *ch = *begin;
            *begin = temp;
            permutation_core(str, begin + 1);
            temp = *ch;
            *ch = *begin;
            *begin = temp; 
        }
   } 
}
int main()
{
  //这里不能用char *str = "adc",因为我们需要交换字符串里面的字符,需要操作栈内存
    char str[] = "abc";  
    permutation(str);
    return 0;
}

这样写,如果有重复的字符就有问题,比如ada,我们优化如下,去掉重

#include <stdio.h>
#include <string.h>
void permutation(char *str);
void swap(char *str, int start, int end);
void permutation_core(char *str, int begin, int end);
void permutation(char *str)
{
    if (str == NULL) 
    {
        printf("str is NULL\n");
        return;
    }
  int len = strlen(str);
  printf("len is %d\n", len);
    permutation_core(str, 0, len);
}
void permutation_core(char *str, int begin, int end)
{
  if (begin == end)
  {
    printf("%s\n", str);
  }
  else
  {
    for (int i = begin; i < end; ++i) 
    {
      if (i != begin && str[i] == str[begin])
        continue;
      swap(str, i, begin);
      permutation_core(str, begin + 1, end);  
      swap(str, i, begin);
    }
  }
}
void swap(char str[], int start, int end)
{
    char ch = str[start];
    str[start] = str[end];
    str[end] =  ch;
}
int main()
{
    //这里不能用char *str = "adc",因为我们需要交换字符串里面的字符,需要操作栈内存
    char str[] = "abc";  
    permutation(str);
    return 0;
}

4 运行结果

c++版本运行结果

abc
acb
bac
bca
cab
cba

c版本运行结果

abc
acb
bac
bca
cba
cab




相关文章
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8077 2
|
11天前
|
域名解析 人工智能 Linux
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10267 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1547 5
|
9天前
|
云安全 数据采集 人工智能
|
16天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2697 8
|
9天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2383 0