「java求所有子集」java 子集

博主:adminadmin 2022-11-22 23:50:06 75

今天给各位分享java求所有子集的知识,其中也会对java 子集进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA递归找所有子集

package web;

import java.util.ArrayList;

public class SubsetGenerator

{

int[] indexs = null;

int COUNT = 1;// choose how many to be combination

ArrayListString list = new ArrayListString ();

private String subsets;

public SubsetGenerator( String subsets )

{

this.subsets = subsets;

}

public ArrayListString getSubsets ( int... params )

{

if(params.length == 0)

{

indexs = new int[subsets.length ()];

params = new int[2];

params[0] = 0;

params[1] = -1;

list.add ("");

}

params[1]++;

if(params[1]  COUNT - 1)

{

return list;

}

for( indexs[params[1]] = params[0]; indexs[params[1]]  subsets.length (); indexs[params[1]]++ )

{

getSubsets (indexs[params[1]] + 1, params[1]);

if(params[1] == COUNT - 1)

{

String temp = "";

for( int i = COUNT - 1; i = 0; i-- )

{

temp += subsets.charAt (indexs[params[1] - i]);

}

list.add (temp);

}

}

if(COUNT  subsets.length ()  params[0] == 0)

{

COUNT++;

getSubsets (0, -1);

}

return list;

}

}

package web;

import java.util.ArrayList;

import java.util.Collections;

public class SubsetGeneratorTester

{

public static void main ( String[] args )

{

SubsetGenerator generator = new SubsetGenerator ("rum");

ArrayListString subsets = generator.getSubsets ();

Collections.sort (subsets);

if(!"[, m, r, rm, ru, rum, u, um]".equals (subsets.toString ()))

{

System.err.println ("Expected: [, m, r, rm, ru, rum, u, um]");

}

else

{

System.err.println ("Congratulations !");

System.out.println ("Your result is: " + subsets);

}

}

}

用递归 求出一个string的所有子集 java

其实问题还是比较明显的,比如subsets.add(firstWord);这句话在for循环中重复执行,但是显然firstWord每次都是word.substring(0, 1);而在整个for循环中,你又没有对word重新赋值过。这也就是为什么你的输出有那么多相同的字符串吧

另外,理论上你每次新建一个SubsetGenerator,就不能算递归了

public static ArrayListString getSubSequences(String word) {

ArrayListString list = new ArrayListString();

doGetSubSequences(word, "", list);

return list;

}

private static void doGetSubSequences(String word, String s,

ArrayListString list) {

if (word.length() == 0) {

list.add(s);

return;

}

String tail = word.substring(1);

doGetSubSequences(tail, s, list);

doGetSubSequences(tail, s + word.charAt(0), list);

}

java树级对象递归查找子集问题

package com.demo.dept;

/**

 * @author dongbin.yu

 * @from 2016-05-06

 * @since V1.0

 */

public class Dept {

    private int id;

    private String name;

    private int parentId;

    public int getId() {

        return id;

    }

    public void setId(int id) {

        this.id = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public int getParentId() {

        return parentId;

    }

    public void setParentId(int parentId) {

        this.parentId = parentId;

    }

    public Dept(int id, String name, int parentId) {

        this.id = id;

        this.name = name;

        this.parentId = parentId;

    }

}

package com.demo.dept;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/**

 * @author dongbin.yu

 * @from 2016-05-06

 * @since V1.0

 */

public class DeptTest {

    private static ListDept depts = new ArrayList();

    static{

        depts.add(new Dept(1,"部门1",0));

        depts.add(new Dept(2,"部门2",1));

        depts.add(new Dept(3,"部门3",1));

        depts.add(new Dept(4,"部门4",1));

        depts.add(new Dept(5,"部门5",2));

        depts.add(new Dept(6,"部门6",3));

        depts.add(new Dept(7,"部门7",2));

        depts.add(new Dept(8,"部门8",2));

        depts.add(new Dept(9,"部门9",1));

        depts.add(new Dept(10,"部门10",5));

    }

    public static void main(String[] args) {

        MapInteger, ListInteger deptMap = new HashMap();

        for (Dept dept : depts) {

            deptMap.put(dept.getId(),getChildDept(dept.getId()));

        }

        System.out.println(deptMap);

    }

    private static ListInteger getChildDept(int id){

        ListInteger ids = new ArrayList();

        for (Dept dept : depts) {

            if(dept.getParentId() == id){

                //添加第一次父id符合的

                ids.add(dept.getId());

                //添加嵌套父id符合的

                ids.addAll(getChildDept(dept.getId()));

            }

        }

                Collections.sort(ids);

        return ids;

    }

}

java求所有子集的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 子集、java求所有子集的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。