Huawei Online Programming Questions Series-3-Clear Random Numbers


Problem Description:

Problem Description
Problem Description

1. The question involves knowledge points.

  • Deduplication.
  • Sort.
  • TreeSetusage.

2. Solve it yourself.

  • To do an infinite input usescanner.hasNextInt()
  • Using the idea of ​​insertion sort, every time you get a new element, first check whether the sorted array already contains the element. If there is, give up, if not, insert it according to the idea of ​​insertion sort.
package com.chaoxiong.niuke.huawei;
import java.util.Scanner;
import java.util.TreeSet;
public class HuaWei_3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
        // 做一个无限循环处理. N 为每次处理的个数. 取到整数,放入数组,使用数组进行处理.
            int N = in.nextInt();
            int[] intArr = new int[N];
            for (int i = 0; i < intArr.length; i++) {
                intArr[i] = in.nextInt();
            }
            cleanSameSort(intArr);//计算过程
        }
    }

    private static void cleanSameSort(int[] intArr) {
        // 去除重复和排序同时使用一个插入排序完成
        int[] newIntArr = new int[intArr.length];
        int newIntArrIndex = 0;//新数组的游标,表示长度.
        for (int key : intArr) {
            if (insert(key, newIntArr, newIntArrIndex)) {//判断是否需要插入,排序好的数组中是否含有待插值
                newIntArrIndex++;
            }
        }
        // 做输出.
        for (int i = 0; i < newIntArrIndex; i++) {
            System.out.println(newIntArr[i]);
        }
    }
    private static boolean insert(int key, int[] newIntArr, int newIntArrIndex) {
        if (newIntArrIndex == 0) {//如果新数组为空,直接插入.
            newIntArr[newIntArrIndex] = key;
            return true;
        }
        // 检测是否插入(新数组中是否包含key)
        if (!isInArr(newIntArr, newIntArrIndex, key)) {
            int i;
            for (i = newIntArrIndex - 1; i >= 0; i--) {
                if (key < newIntArr[i]) {
                    newIntArr[i + 1] = newIntArr[i];
                } else {
                    break;
                }
            }
            newIntArr[i + 1] = key;
            return true;
        } else {
            return false;
        }
    }
    private static boolean isInArr(int[] newIntArr, int newIntArrIndex, int key) {
        for (int i = 0; i < newIntArrIndex; i++) {
            if (newIntArr[i] == key) {
                return true;
            }
        }
        return false;
    }
}

3. Quality answers.

  • Use to TreeSetstore data ( Setfor non-repetitive, TreeSetfor sorting and non-repetitive.)
package com.chaoxiong.niuke.huawei;
import java.util.Scanner;
import java.util.TreeSet;
public class HuaWei_3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int N = in.nextInt();
            // TreeSet的优点就是有序,切不重复.
            TreeSet<Integer>treeSet = new TreeSet<Integer>();//声明一个TreeSet.
            for (int i = 0; i < N; i++) {
                treeSet.add(in.nextInt());
            }
            for(Integer i : treeSet){
                System.out.println(i);
            }
        }
    }

4. Summary of this question.

  • TreeSetAnd PriorityQueuecompare.
    TreeSet: TreeSet in Java is a subclass of Set, TreeSet collection is used to sort object elements , and it can also guarantee the uniqueness of elements .

TreeSet sorts basic data

import java.util.TreeSet;
/**
 * Create by tianchaoxiong on 18-4-20.
 */
public class ceshi {
    public static void main(String[] args) {
        TreeSet<Integer> treeSet =  new TreeSet<Integer>();
        treeSet.add(1);
        treeSet.add(2);
        treeSet.add(2);
        treeSet.add(1);
        for(Integer each:treeSet)
            System.out.println(each);
    }
}

TreeSet sorts custom data

package com.chaoxiong.niuke.huawei;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * Create by tianchaoxiong on 18-4-9.
 */
public class CusTreeSet {
    private String name;
    private int population;
    private int getPopulation() {
        return population;
    }
    @Override
    public String toString() {
        return "TreeSet {" +
                "name='" + name + '\'' +
                ", population=" + population +
                '}';
    }
    private CusTreeSet(String name, int population){
        this.name = name;
        this.population = population;
    }
    public static void main(String[] args) {
        Comparator<CusTreeSet> OrderIsdn =  new Comparator<CusTreeSet>(){
            @Override
            public int compare(CusTreeSet o1, CusTreeSet o2) {
                // 这里只对比了一个属性
                int a = o1.getPopulation();
                int b = o2.getPopulation();
                return Integer.compare(a, b);//从大到小
                // Integer.compare(1, 1) return 0
                // Integer.compare(1, 2) return -1
                // Integer.compare(2, 1) return 1
            }
        };
        TreeSet<CusTreeSet> treeSet =  new TreeSet<CusTreeSet>(OrderIsdn);
        CusTreeSet t1 = new CusTreeSet("t1",1);
        CusTreeSet t3 = new CusTreeSet("t3",3);
        CusTreeSet t2 = new CusTreeSet("t2",2);
        CusTreeSet t4 = new CusTreeSet("t4",3);
        treeSet.add(t1);
        treeSet.add(t3);
        treeSet.add(t2);
        treeSet.add(t4);
        for(CusTreeSet i : treeSet){
            System.out.println(i.toString());
        }
    }
}

PriorityQueue: A small-to-large ordered queue with repetitions.
Sort basic data types (small to large):

package com.chaoxiong.niuke.huawei;
import java.util.PriorityQueue;
import java.util.Queue;
/**
 * Create by tianchaoxiong on 18-4-20.
 */
public class ceshi {
    public static void main(String[] args) {
        Queue<Integer> intQueue = new PriorityQueue<Integer>(10);//可通过构建Comparator实现从大到小
        intQueue.add(2);
        intQueue.add(1);
        intQueue.add(4);
        intQueue.add(2);
        intQueue.add(2);
        while (intQueue.peek() != null) {
            System.out.println(intQueue.poll());
        }
    }
}

Sort basic data types (small to large):

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * Create by tianchaoxiong on 18-4-20.
 */
public class ceshi {
    public static void main(String[] args) {
        Comparator<Integer> OrderBtS = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o2, o1);
            }
        };
        Queue<Integer> intQueue = new PriorityQueue<Integer>(10,OrderBtS);
        intQueue.add(2);
        intQueue.add(1);
        intQueue.add(4);
        intQueue.add(2);
        intQueue.add(2);
        while (intQueue.peek() != null) {
            System.out.println(intQueue.poll());
        }
    }
}

Sort custom data types:

package com.chaoxiong.niuke.huawei;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * Create by tianchaoxiong on 18-4-9.
 */
public class priorityQueue {
    private String name;
    private int population;

    private int getPopulation() {
        return population;
    }

    @Override
    public String toString() {
        return "priorityQueue{" +
                "name='" + name + '\'' +
                ", population=" + population +
                '}';
    }

    private priorityQueue(String name, int population){
        this.name = name;
        this.population = population;
    }

    public static void main(String[] args) {
        Comparator<priorityQueue> OrderIsdn =  new Comparator<priorityQueue>(){
            @Override
            public int compare(priorityQueue o1, priorityQueue o2) {
                int a = o1.getPopulation();
                int b = o2.getPopulation();
                return Integer.compare(b, a);//从大到小
                // Integer.compare(1, 1) return 0
                // Integer.compare(1, 2) return -1
                // Integer.compare(2, 1) return 1
            }
        };
        Queue<priorityQueue> priorityQueue =  new PriorityQueue<com.chaoxiong.niuke.huawei.priorityQueue>(11,OrderIsdn);
        com.chaoxiong.niuke.huawei.priorityQueue t1 = new priorityQueue("t1",1);
        com.chaoxiong.niuke.huawei.priorityQueue t3 = new priorityQueue("t4",3);
        com.chaoxiong.niuke.huawei.priorityQueue t2 = new priorityQueue("t2",2);
        com.chaoxiong.niuke.huawei.priorityQueue t4 = new priorityQueue("t3",3);
        priorityQueue.add(t1);
        priorityQueue.add(t3);
        priorityQueue.add(t2);
        priorityQueue.add(t4);
        while (priorityQueue.peek() != null) {
            System.out.println(priorityQueue.poll());
        }
//        System.out.println(priorityQueue.poll().toString());
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325865939&siteId=291194637