首页 > 编程知识 正文

包含java递归实现无限插入子集的词条

时间:2023-12-27 22:26:52 阅读:325074 作者:GLAT

本文目录一览:

递归实现java无限极菜单

说下我个人的做法吧,不考虑任何效率问题,我是在查询对象的时候,把对象用递归方法先封装成一个集合。就是第一次查的时候,会拿到根,然后就可以开始使用递归去把子类提出来,直到没有儿子。最后只需要把这个集合直接JSONArray 转成json字符串。丢到前台就行了。

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递归方法实现

1、递归做为一种算法在程序设计语言中广泛使用,是指函数/过程/子程序在运行过程中直接或间接调用自身而产生的重入现象。

2、递归算法一般用于解决三类问题:

1)数据的定义是按递归定义的。(Fibonacci(斐波那契)的函数)

2)问题解法按递归算法实现。(回溯)

3)数据的结构形式是按递归定义的。(树的遍历,图的搜索)

JAVA题目:Java题目:用递归写n个元素的子集 static Object[] subs(Object[] chosen, int i, int n)

递归问题都是千篇一律的,将问题分为n类,每一类问题又可以分为n类。

这个问题中,设a为集合,将所有子集合分为两类,有a[0]的和没a[0]的。

有a[0]的分为两类,有a[1]的和没a[1]的。

。。。

有a[n-2]的分为两类,有a[n-1]的和没a[n-1]的。

有a[n-1]的直接返回[a[n-1]]。

没a[n-1]的直接返回[]。

subs(chosen,i,n){//subs为全部集合,chosen为数组,i为数组索引,从1开始,n为数组长度

if(i1){return;}

if(i==n){//当i为n时,也就是扫描完数组了。

if(!chosen)//如果数组为空,也就是没有chosen[n-1]的情况

return [];//返回空数组

}else{

return [chosen[i-1]];//否则为有a[n-1]的情况

}

arr1=subs(delete_arr_index(chosen,i-1),i+1,n);//没chosen[i-1]的情况,当i=1时,也就是求得没有a[0]的所有集合。

arr2=subs(chosen,i+1,n);//有chosen[i-1]的情况,当i=1时,也就是求得有a[0]的所有集合。

arr=arr1+arr2//合并数组,当i=1时,就是获得了全部集合。

return arr;

}

//删除数组中的某个元素

public static void delete_arr_index(str,index) {

//删除php

Listint list = new ArrayListint();

for (int i=0; istr.length; i++) {

list.add(str[i]);

}

list.remove(index);

int[] newStr = list.toArray(new int[1]);

reuturn newStr;

}

在JAVA中什么是递归?有什么用?

Java方法递归是指在一个方法的内部调用自身的过程,以此类推就是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);

}

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。