如何用字符串中的两个可能的字符替换一个字符 [英] How to replace one characters with two possible characters in a string

查看:52
本文介绍了如何用字符串中的两个可能的字符替换一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将 '?' 处的 "10?01?1" 字符串替换为 1>0.例如,从字符串 "1?0" -> "110" &&100".如何解决这个问题?递归?谢谢.

I want to know how to replace a string like "10?01?1" at '?' with 1 or 0. E.g., from string "1?0" -> "110" && "100". How to approach this problem? Recursive? Thanks.

EDIT 带有 OP 注释

"1?0?1 ...它应该有四种不同的组合."11011"、"11001"、"10011"、"10001".如何打印所有四种组合?"

"1?0?1 ... it should have four different combinations. "11011", "11001", "10011","10001". How to print all four combinations?"

推荐答案

遇到?"时,将其替换为0"并重复出现,然后将其替换为1"并再次出现.

When you encounter a '?', replace it with a '0' and recur, then replace it with a '1' and recur.

private void permute(String s) { 
    ... 
    if (s.charAt(i) == '?') { 
        permute(<replacing '?' with '0'>);
        permute(<replacing '?' with '1'>); 
    } 
    ...
}

这篇关于如何用字符串中的两个可能的字符替换一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆