编程逻辑问题

发布于 2024-10-09 18:20:50 字数 404 浏览 0 评论 0原文

我需要一个可以给我返回数组的所有可能组合的函数。

示例:

$source = array('a', 'b', 'c');
$target = thisiswhatisearch($source);

现在 $target 应该如下所示:

array('a','b','c','ab','ac','cb','abc')

我不需要 aabbcc

我也不需要 bacaacb.. 因为顺序对我来说并不重要。

感谢您的任何帮助。

I need a function that can give me all possible combinations of a array back.

Example:

$source = array('a', 'b', 'c');
$target = thisiswhatisearch($source);

Now the $target should look like:

array('a','b','c','ab','ac','cb','abc')

I dont need the aa, bb, cc.

I also dont need the the ba, ca, acb.. because the order isn't important to me.

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故笙诉离歌 2024-10-16 18:20:50

试图与语言无关,但我猜它的 C 语言如下:

function combinations(array arr)
{
  combos = array();
  for (int i=1; i<2**arr.size(); i++)
  {
    int x = i;
    int c = 0;
    str = "";
    while(x>0)
      {
        int rem = x % 2;
        if(rem == 1)
          str += arr[c];
        x = x / 2;
        c++;
      }
    combos.add(str);

   }
return combos;
}

Tried to be language agnostic but i guess its C like:

function combinations(array arr)
{
  combos = array();
  for (int i=1; i<2**arr.size(); i++)
  {
    int x = i;
    int c = 0;
    str = "";
    while(x>0)
      {
        int rem = x % 2;
        if(rem == 1)
          str += arr[c];
        x = x / 2;
        c++;
      }
    combos.add(str);

   }
return combos;
}
柒七 2024-10-16 18:20:50

Combination 的 Wikipedia 条目有一个 链接到执行此操作的 C 代码

The Wikipedia entry for Combination has a link to C code that does this.

筱武穆 2024-10-16 18:20:50

这是一个疯狂的解决方案。它可能不是最快和最干净的,但它确实有效。它是用 Java 编写的,因为我打开了它:

public class Combination {

public static void main(String[] args) {
    String[] source = {"a","b","c"};
    List<String> result = combineMe(new ArrayList<String>(Arrays.asList(source)));
    for (String string : result) {
        System.out.println(string);
    }
}

public static List<String> combineMe(List<String> source) {
    List<String> result = new ArrayList<String>();
    if (source.size()==0) {
        result.add("");
        return result;
    }else{
        String tmp = source.remove(0);
        source = combineMe(source);
        for (String string : source) {
            result.add(("" + string).trim());
            result.add(tmp + string);
        }
    }
    return result;
}
}

结果列表中的第一个条目是假条目,需要在最后删除

That's an out of the mind solution. It is probably not the fastest and cleanest one, but it kind of works. It's in Java, because I had it open:

public class Combination {

public static void main(String[] args) {
    String[] source = {"a","b","c"};
    List<String> result = combineMe(new ArrayList<String>(Arrays.asList(source)));
    for (String string : result) {
        System.out.println(string);
    }
}

public static List<String> combineMe(List<String> source) {
    List<String> result = new ArrayList<String>();
    if (source.size()==0) {
        result.add("");
        return result;
    }else{
        String tmp = source.remove(0);
        source = combineMe(source);
        for (String string : source) {
            result.add(("" + string).trim());
            result.add(tmp + string);
        }
    }
    return result;
}
}

The first entry in the resulting list is a fake one, and needs to be removed at the end

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文