【计算机算法】【LeetCode】[组合][递归]替换字符串中的通配符?

这篇博客探讨了如何在LeetCode上解决一个关于字符串通配符替换的问题。作者介绍了两种算法:非递归组合,通过计算问号(?)的数量并进行位运算;以及深度优先搜索(DFS)加回溯策略。博客包含详细的分析和两种算法的代码实现,并展示了运行结果。
摘要由CSDN通过智能技术生成

题目

/*

 * 给定字符串(合法字符只包括0,1,?),替换字符串中的通配符?为0或者1,生成所有可能的字符串。

 * Input str = "1??0?101"

 * Output:

 *       10000101

 *       10001101

 *       10100101

 *       10101101

 *       11000101

 *       11001101

 *       11100101

 *       11101101

 */

分析

算法1:非递归组合

  1. 计算?的个数,并记录其位置Index
  2. 求出组合数
  3. 将?所在的位置值,按位运算

算法2:DFS算法+回溯

代码

算法1:非递归组合 

#include <iostream> 
#include <map>
#include <vector>
#include <string>
#include <math.h>
using namespace std;

void Combine(string str, vector<string> *res)
{
    string strR = str;
    // counter ? , and tag the index...
    map<int, int> mapIndex;
    int nLen = str.length();
    for (int i = 0; i < nLen; i++)
    {
        if (str.at(i) == '?')
            mapIndex[i]='0';    
    }
    
    int combinCount = pow(2,mapIndex.size());
    for (int i = 0; i < combinCount; i++)
    {
        int nValue = i;
        int qouteCount = 0;
        for (map<int, int>::iterator it = mapIndex.begin(); it != mapIndex.end();it++)
        {
            int bitFlag = 1<<qouteCount; 
            qouteCount++;
            
            // bit compute
            strR[it->first] = (((nValue) & bitFlag) == (bitFlag)) ? '1': '0' ; 
        }
        res->push_back(strR);
    }
}
int main ()
{
    string str= "1??0?101";
    
    vector<string> res;

    Combine(str, &res);
    
    for (vector<string>::iterator iter =res.begin(); iter != res.end(); iter++ )
    {
        cout<<*iter<<endl; 
    }
    
    return 0;
}

算法2:DFS算法+回溯 

#include <iostream> 
#include <map>
#include <vector>
#include <string>
#include <math.h>
using namespace std;

void helper(int index, string str, vector<string> *res)
{
    if (index ==str.length()) 
    {
        res->push_back(str);
        return;
    }
    if (str.at(index) == '?')
    {
        str[index] = '0';
        helper(index + 1, str, res);    // replace for 0
        str[index] = '1';
        helper(index + 1, str, res);    // repalce for 1
        
        str[index] = '?';               // repalce from self
    }
    else 
    {
        helper(index + 1, str,res);
    }
}     
void DFS(string str, vector<string> *res)
{
    helper(0, str, res);
}

int main ()
{
    string str= "???";
    
    vector<string> res;
    DFS(str, &res);
   // Combine(str, &res);
    
    for (vector<string>::iterator iter =res.begin(); iter != res.end(); iter++ )
    {
        cout<<*iter<<endl; 
    }
    
    return 0;
}

运行结果 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐徐太平KevinCharles

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值