数据结构与算法分析--Java语言描述(第一章(2))

习题1.6

编写带有下列声明的例程:

public void permute(String str);

public void permute(char[] str, int low, int high);

第一个例程是个驱动程序,它调用第二个例程并显示String str中字符的所有排列。例如,str是"abc", 那么输出的串则是abc,acb,bac,bca,cab,cba,第二个例程使用递归。

    /**
     * 第一个例程是驱动程序,它调用第二个例程并显示String str中字符的所有排列。
     *
     * @param str
     */
    public void permute(String str){
        char[] array = str.toCharArray();
        permute(array,0,array.length - 1);
    }

    /**
     * 使用递归
     *
     * @param str
     * @param low
     * @param high
     */
    public void permute(char[] str,int low,int high){
        //递归结束条件
        if(low == high){
            String s = "";
            for(int i = 0; i < str.length; i++){
                s += str[i];
            }
            System.out.print(s + " ");
        }

        for(int i = low; i < str.length; i++) {
            // 去除重复的结果
            if (is_swap(str, low, i)) {
                swap(str, low, i);
                permute(str, low + 1, high);
                swap(str, low, i);
            }
        }
    }

    /**
     * 判断是否具有重复的结果
     *
     * @param str
     * @param m
     * @param n
     * @return
     */
    public boolean is_swap(char[] str, int m, int n) {
        boolean flag = true;
        for(int i = m;i < n; i++){
            if(str[i] == str[n]){
                flag = false;
            }
        }
        return flag;
    }

    /**
     * 交换数组中下标为i,j的两个元素
     *
     * @param chars
     * @param i
     * @param j
     */
    private void swap(char[] chars, int i, int j) {
        char temp = chars[j];
        chars[j] = chars[i];
        chars[i] = temp;
    }

拓展:输出字符串的所有组合。例如,str="abc",输出所有组合a、b、c、ab、ac、bc、abc。

    /**
     * 输出字符串的所有组合
     *
     * @param str
     */
    public void combine(String str){
        if(str == null || str.length() < 0){
            return;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++){
            combineRecursive(str,0,i,sb);
        }
    }

    /**
     * 从字符数组中挑选出number个字符放入StringBuilder中
     *
     * @param str
     * @param begin
     * @param number
     * @param sb
     */
    private void combineRecursive(String str, int begin, int number, StringBuilder sb) {
        if(number == -1){
            System.out.print(sb + " ");
            return;
        }
        if(begin == str.length()){
            return;
        }
        //向StringBuilder中添加元素
        sb.append(str.charAt(begin));
        combineRecursive(str,begin + 1,number - 1,sb);
        //向StringBuilder中删除元素
        sb.deleteCharAt(sb.length() - 1);
        combineRecursive(str,begin + 1,number,sb);
    }

习题1.13

设计一个泛型类Collection,它存储Object对象的集合(在数组中),以及该集合的当前大小。提供public 方法isEmpty,makeEmpty,insert,remove和isPresent.

/**
 * @description: 设计一个泛型类Collection,它存储Object对象的集合(在数组中),以及该集合的当前大小。
 * 提供public 方法isEmpty,makeEmpty,insert,remove和isPresent.
 *
 * @author: 
 * @date: 2018/7/18
 */

@Getter
@Setter
public class Collection_Test {

    private Object[] objects;

    public boolean isEmpty(){
        if(objects.length == 0 || objects == null){
            return true;
        }
        return false;
    }

    public void makeEmpty(){
        objects = null;
    }

    public void insert(Object object){
        int length = this.objects.length;
        Object[] objects = new Object[length + 1];
        System.arraycopy(this.objects,0,objects,0,length);
        objects[length] = object;
        this.objects = objects;
    }

    public void remove(int index){
        if(index < 0 || this.objects == null || this.objects.length == 0){
            return;
        }
        int length = this.objects.length;

        if(length == 1){
            this.objects = new Object[]{};
        }
        if(index > length - 1){
            return;
        }
        Object[] objects = new Object[length - 1];
        for(int i = 0; i < length; i++){
            if(i == index){
                continue;
            }
            if(i < index){
                objects[i] = this.objects[i];
            }else{
                objects[i-1] = this.objects[i];
            }
        }
        this.objects = objects;
    }

    public boolean isPresent(Object object){
        if(objects == null || objects.length == 0){
            return false;
        }
        for(Object obj : objects){
            if(obj.equals(object)){
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Collection_Test test = new Collection_Test();
        test.setObjects(new Object[]{"2018-7-19","星期四",26,"test",23,"haha"});

//        boolean empty = test.isEmpty();
//        System.out.println(empty);

//        test.makeEmpty();

//        test.insert("天气晴");
        test.remove(0);
        for(Object object : test.getObjects()){
            System.out.print(object + " ");
        }
    }

习题1.14

设计一个泛型类OrderdCollection,它存储Comparable的对象的集合(在数组中),以及该集合的当前大小。提供public方法isEmpty,makeEmpty,insert,remove,findMin和findMax。


/**
 * @description: 设计一个泛型类OrderdCollection,它存储Comparable的对象的集合(在数组中),以及该集合的当前大小。
 * 提供public方法isEmpty,makeEmpty,insert,remove,findMin和findMax。
 * @author: 
 * @date: 2018/7/19
 */

@Getter
@Setter
public class OrderedCollection_Test {
    private Comparable[] comparables;

    public boolean isEmpty(){
        if(comparables == null || comparables.length == 0){
            return true;
        }
        return false;
    }

    public void makeEmpty(){
        comparables = new Comparable[]{};
    }

    public void insert(Comparable comparable){
        int length = this.comparables.length;
        Comparable[] comparables = new Comparable[length + 1];
        System.arraycopy(this.comparables,0,comparables,0,length);
        comparables[length] = comparable;
        this.comparables = comparables;
    }

    public void remove(int index){
        if(index < 0 || this.comparables == null || this.comparables.length == 0){
            return;
        }
        int length = this.comparables.length;
        if(length == 1){
            comparables = new Comparable[]{};
        }
        if(index > length - 1){
            return;
        }
        Comparable[] objects = new Comparable[length - 1];

        for(int i = 0; i < length; i++){
            if(i == index){
                continue;
            }
            if(i < index){
                objects[i] = comparables[i];
            }else{
                objects[i - 1] = comparables[i];
            }
        }
        this.comparables = objects;
    }


    public Comparable findMax(){
        if(comparables == null || comparables.length == 0){
            return null;
        }
        int maxIndex = 0;
        for(int i = 0; i < comparables.length; i++){
            if(comparables[i].compareTo(comparables[maxIndex]) > 0){
                maxIndex = i;
            }
        }
        return comparables[maxIndex];
    }

    public Comparable findMin(){
        if(comparables == null || comparables.length == 0){
            return null;
        }
        int minIndex = 0;
        for(int i = 0; i < comparables.length; i++){
            if(comparables[i].compareTo(comparables[minIndex]) < 0){
                minIndex = i;
            }
        }
        return comparables[minIndex];
    }

    public static void main(String[] args) {
        OrderedCollection_Test test = new OrderedCollection_Test();
        test.setComparables(new Comparable[]{1,3,5,6,9});

//        test.insert(12);
        test.remove(2);
//        System.out.println(test.findMax());
//        System.out.println(test.findMin());
        for(Comparable com : test.getComparables()){
            System.out.print(com + " ");
        }
    }
}

习题1.15

定义一个Rectangle类,该类提供getLength和getWidth方法。利用图1-18中的findMax例程编写一种main方法,该方法创建一个Rectangle数组并首先找出依面积最大的Rectangle对象,然后找出依周长最大的Rectangle对象。

/**
 * @description: 定义一个Rectangle类,该类提供getLength和getWidth方法。
 * 利用图1-18中的findMax例程编写一种main方法,该方法创建一个Rectangle数组并首先找出依面积最大的Rectangle对象,然后找出依周长最大的Rectangle对象。
 *
 * @author: 
 * @date: 2018/7/16
 */

@Getter
@Setter
public class Rectangle_Test {
    private double length;

    private double width;

    public double getArea() {
        return length * width;
    }

    public double getPerimeter() {
        return (length + width) * 2;
    }

    public Rectangle_Test(double length,double width){
        this.length = length;
        this.width = width;
    }

    @Override
    public String toString() {
        return "length: " + length + "width: " + width;
    }

    public static<T> T findMax(T[] a, Comparator<? super T> cmp){
        int maxIndex = 0;
        for(int i = 0; i < a.length; i++){
            if(cmp.compare(a[i],a[maxIndex]) > 0){
                maxIndex = i;
            }
        }
        return a[maxIndex];
    }

    static class PerimeterComparator implements Comparator<Rectangle_Test>{

        @Override
        public int compare(Rectangle_Test t1, Rectangle_Test t2) {
            if(t1.getPerimeter()== t2.getPerimeter()){
                return 0;
            } else if(t1.getPerimeter() > t2.getPerimeter()){
                return 1;
            } else{
                return -1;
            }
        }
    }

    static class AreaComparator implements Comparator<Rectangle_Test>{

        @Override
        public int compare(Rectangle_Test t1, Rectangle_Test t2) {
            if(t1.getArea() == t2.getArea()){
                return 0;
            } else if(t1.getArea() > t2.getArea()){
                return 1;
            } else{
                return -1;
            }
        }
    }

    public static void main(String[] args){
        Rectangle_Test[] rec = {
                new Rectangle_Test(1, 2), new Rectangle_Test(2, 3), new Rectangle_Test(3, 4),
                new Rectangle_Test(2, 9), new Rectangle_Test(2, 5), new Rectangle_Test(5, 5)
        };

        System.out.println(findMax(rec,new AreaComparator()));
        System.out.println(findMax(rec,new PerimeterComparator()));
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值