java方法创建数组 + 简单库函数排序

一维数组

package 数组;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
public class 排序1_快速排序sort {
	static int max = 99;
	public static void main(String[] args) {
		int[] a = new int[max];
		a = creat(a);
		show(a);
//		Arrays.sort(a);
		Arrays.sort(a, 1, 7);
		System.out.printf("\nsort 快速排序后:\n");
		show(a);
	}

	private static void show(int[] a) {
		System.out.println("数组:");
		for(int i = 0; i < a.length; i++) {
			System.out.printf("%-4d ",a[i]);
			if((i+1)%10==0) {
				System.out.printf("\n");
			}
		}
		System.out.println();
	}

	private static int[] creat(int[] a) {
		System.out.println("数组初始化...");
		
		Random rand = new Random();
		int length = rand.nextInt(max);
		System.out.println("length = " + length);
		
		a = new int[length];
		for(int i = 0; i < length; i++) {
			a[i] = rand.nextInt()%max;
		}
		System.out.printf("成功!\n\n");
		return a;
	}
}

[1, 7) 部分排序、升序

降序
Arrays.sort(arr,Collections.reverseOrder());
Arrays.sort(task, Comparator.comparingInt(a -> -a));

升序
Arrays.sort(task); // 默认
Arrays.sort(task, Comparator.comparingInt(a -> a));

二维数组

Arrays.sort(t,0,2,new Comparator<int[]>() {
    public int compare(int[] a, int[] b) {
        return a[1] - b[1];
    }
});

Arrays.sort(t,0,2,new Comparator<int[]>() {
            public int compare(int[] a, int[] b) {
                if (a[1] == b[1])
                    return a[0] - b[0];  // 升序
                return a[1] - b[1];  // 升序
            }
        });

Arrays.sort(t, 0,2,Comparator.comparingInt(o -> o[1]));  // 升序
Arrays.sort(t, 0,2,Comparator.comparingInt(o -> -o[1]));  // 降序
public class t1 {
    public static void main(String[] args) {
        int[][] t = new int[4][2];
        t[0][0] = 1;
        t[0][1] = 3;
        t[1][0] = 4;
        t[1][1] = 2;

        Arrays.sort(t, 0,2,Comparator.comparingInt(o -> o[1]));
        for(int i = 0;i < 2;i++){
            System.out.println(t[i][0] + " "+t[i][1]);
        }
    }
}

https://blog.csdn.net/Inoue1595505/article/details/125205868

    public class Main {
      public static void main(String[] args) {
          //注意,要想改变默认的排列顺序,不能使用基本类型(int,double, char)
          //而要使用它们对应的类
         Integer[] a = {9, 8, 7, 2, 3, 4, 1, 0, 6, 5};
         //定义一个自定义类MyComparator的对象
         Comparator cmp = new MyComparator();
         Arrays.sort(a, cmp);
         for(int i = 0; i < a.length; i ++) {
             System.out.print(a[i] + " ");
         }
     }
 }
 //Comparator是一个接口,所以这里我们自己定义的类MyComparator要implents该接口
 //而不是extends Comparator
 class MyComparator implements Comparator<Integer>{
     @Override
     public int compare(Integer o1, Integer o2) {
         //如果n1小于n2,我们就返回正值,如果n1大于n2我们就返回负值,
         //这样颠倒一下,就可以实现反向排序了
         if(o1 < o2) { 
             return 1;
         }else if(o1 > o2) {
             return -1;
         }else {
            return 0;
         }
     }
 }

Comparator.comparing

指定排序的字段

相反的排序规则
Collections.sort(employees, 
				Comparator.comparing(Employee::getName).reversed());

类 排序

class Node implements Comparable<Node> {
    int left;
    int right;

    public Node(int left, int right) {
        this.left = left;
        this.right = right;
    }

    @Override
    public int compareTo(Node o) {
        if (right == o.right)
            return o.left - left;
        return right - o.right;
    }
}

// https://leetcode.cn/problems/maximum-number-of-non-overlapping-substrings/
public class test2 {
    public static void main(String[] args) {
        String s = "adefaddaccc";
        System.out.println(maxNumOfSubstrings(s));
    }


    public static List<String> maxNumOfSubstrings(String s) {
        Node[] nodes = new Node[26];
        for (int i = 0; i < 26; i++) {
            nodes[i] = new Node(-1, -1);
        }

        int length = s.length();
        for (int i = 0; i < length; i++) {
            int j = s.charAt(i) - 'a';
            if (nodes[j].left == -1) {
                nodes[j].left = i;
            }
            nodes[j].right = i;
        }

        for (int i = 0; i < 26; i++) {
            if (nodes[i].left == -1)
                continue;
            for (int j = nodes[i].left + 1; j < nodes[i].right; j++) {
                int charAt = s.charAt(j) - 'a';
                if (nodes[i].left <= nodes[charAt].left && nodes[i].right >= nodes[charAt].right)
                    continue;
                nodes[i].left = Math.min(nodes[i].left, nodes[charAt].left);
                nodes[i].right = Math.max(nodes[i].right, nodes[charAt].right);
                j = nodes[i].left;
            }
        }
        Arrays.sort(nodes);

        List<String> ans = new ArrayList<>();
        int end = -1;
        for (int i = 0; i < 26; i++) {
            if (nodes[i].left == -1)
                continue;
            if (nodes[i].left > end) {
                ans.add(s.substring(nodes[i].left, nodes[i].right + 1));
                end = nodes[i].right;
            }

        }
        return ans;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_1403034144

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值