Java-排序算法-复盘知识点

刷了24道简单排序题,18道中等排序题之后,给排序算法来个简单的复盘(从明天开始刷动态规划咯)

1.对于找多数元素(出现次数超过一半的元素)可以使用摩尔投票法

2.HashSet的add方法非常实用:如果集合里面不存在这个数,就添加,并且返回true;如果存在,就返回false

3.数组.toCharArray方法可以把字符串变成数组

4. Arrays.equals(str1, str2);可以用来判断两个数组是否相等

5.Arrays.sort()可以把一个数组从小到大排好序,如果需要从大到小排序,可以重写compare方法:

int ans[][]=new int[2][2];
Arrays.sort(ans, new Comparator<int[]>() {
    @Override
    public int compare(int[] ans1, int[] ans2) {
        return ans1[1]-ans2[1];
    }
});

6.

set2.stream().mapToInt(Integer::intValue).toArray();
//这行代码的作用是将一个Set<Integer>集合转换成一个包含相同整数值的int[]数组。
这个过程通过流操作实现,利用了Java 8的函数式编程特性,使得代码更加简洁和易于理解。

7.HashSet的contains方法可以判断集合里面是否含有这个数

8.

map.put(x,(map.getOrDefault(x,0)+1));
//这一句的意思是如果map里面有键是x的,就把值+1;如果没有,就添加一个键为x值为1的对

9.

map.containsKey(x) //判断是否有键为x的对
map.get(x)//获取键为x的值

10.

ans[a] = String.valueOf(score.length - i);
//计算score字符串的长度减去i的值,将这个结果转换成字符串,
//然后将这个字符串赋值给ans数组在索引a位置上的元素
//这种操作在处理字符串、数组和循环时非常常见,特别是在需要根据某些条件生成或修改数组元素时

11.Math.max()求最大值

12.

 List<List<Integer>> list=new ArrayList<>();
 List<Integer> list1 = Arrays.asList(0, 0, 0);
 list.add(list1);
 //之所以会用List<Integer> list1 = Arrays.asList(0, 0, 0);是因为数组初始化器(即大括号{}中的元素列表)只能用于初始化数组,而不能直接用于初始化集合(如List、Set等)
 //要初始化一个包含特定元素的列表,你可以使用Arrays.asList()或者ArrayList的构造函数结合add()方法。

//ArrayList的构造函数结合add()方法
 List<Integer> list1 = new ArrayList<>();  
list1.add(0);  
list1.add(0);  
list1.add(0);

13.

Set<List<Integer>> uniqueList=new LinkedHashSet<>();
//通过LinkedHashSet实现了对List<Integer>列表的去重
for(List<Integer> x:list){
    uniqueList.add(x);
}
List<List<Integer>> list2=new ArrayList<>(uniqueList);//uniqueList中的元素被复制到一个新的ArrayList中

//由于uniqueList是一个Set,它自动处理元素的唯一性。如果尝试添加一个已经存在于集合中的元素(在这个上下文中是List<Integer>对象),那么该元素将不会被添加。
//这意味着,如果list中包含重复的List<Integer>列表(即内容完全相同的列表),那么uniqueList中将只包含这些列表的唯一副本。
//注意,这里的“内容完全相同”是指列表中的元素顺序和值都相同。如果两个列表包含相同的元素但顺序不同,它们将被视为不同的列表。

14.

Math.abs(a-b)
//Math.abs(sum-target) 在编程中是一个表达式,
//用于计算 a(某个总和或数值)与 b(目标值)之间的差的绝对值。

15.

List<String> list = map.getOrDefault(key, new ArrayList<String>());
//从map中获取与key相关联的字符串列表。如果map中包含该key,则返回对应的字符串列表;
//如果不包含,则返回一个新的空字符串列表,并将这个列表赋值给list变量

16.

Map<Integer,Integer>map=new TreeMap<>();
//TreeMap 可以实现内部排序。当你将键值对添加到 TreeMap 时,这些键值对会根据键的自然顺序
//(如果键实现了 Comparable 接口)或者根据创建 TreeMap 时指定的 Comparator 进行排序

17.

for (Map.Entry<Integer, Integer> entry : map.entrySet()) {//可以实现Map<Integer, Integer>的遍历
    int key = entry.getKey(); 
    int count = entry.getValue();
    }

18.复习一下我的主页里面的《Java中等题-对链表进行插入排序》

class Solution {
    public ListNode insertionSortList(ListNode head) {
        if(head==null){
            return head;
        }
        ListNode drummy=new ListNode(0);
        drummy.next=head;
        ListNode lastSort=head,curr=head.next;
        while(curr!=null){
            if(lastSort.val<=curr.val){
                lastSort=lastSort.next;
            }else{
                ListNode pre=drummy;
                while(pre.next.val<=curr.val){
                    pre=pre.next;
                }
                lastSort.next=curr.next;
                curr.next=pre.next;
                pre.next=curr;
            }
            curr=lastSort.next;

        }
        return drummy.next;




    }
}

19.复习一下我主页里面的《Java中等题-排序链表》

class Solution {
    public ListNode sortList(ListNode head) {
        if(head==null||head.next==null){ //如果链表为空或者只有一个数,那就直接返回
            return head;
        }
        ListNode slow=head,fast=head;
        while(fast.next!=null&&fast.next.next!=null){//使用快慢指针法找到中点
            fast=fast.next.next;
            slow=slow.next;
        }
        fast=slow.next;//用fast来指向第二段链表的开头
        slow.next=null;//把链表分成两段
        ListNode left=sortList(head);
        ListNode right=sortList(fast);
        ListNode vhead=new ListNode(-1);
        ListNode curr=vhead;
 
        while(left!=null&&right!=null){
            if(left.val<= right.val){
                curr.next=left;
                left=left.next;
            }else{
                curr.next=right;
                right=right.next;
            }
            curr=curr.next;//不要漏掉
 
        }
        curr.next=left!=null?left:right;//把剩下的那段直接接上去
        return vhead.next;
 
    }
}

20.Math.pow 是 Java 中的一个静态方法,用于计算第一个参数的第二个参数次幂的值
由于 Math.pow 返回的是 double 类型的值,即使你传入的参数是整数,结果也会是 double 类型的

21. 复习手搓大根堆,可以看我的主页的《Java中等题-数组中的第k个最大元素(力扣)》

class Solution {
    public int findKthLargest(int[] nums, int k) {
        int heapSize=nums.length;
        buildHeap(nums,heapSize);
        for(int i=nums.length-1;i>=nums.length-k+1;i--){
            swap(nums,i,0);
            heapSize--;
            maxHeap(nums,heapSize,0);
        }
        return nums[0];
}
public static void buildHeap(int[] nums,int heapSize){
    for(int i=heapSize/2;i>=0;i--){
        maxHeap(nums,heapSize,i);
    }
 
}
public static void maxHeap(int[] nums,int heapSize,int maxSize){
    int l=maxSize*2+1;
    int r=maxSize*2+2;
    int i=maxSize;
    if(l<heapSize&&nums[l]>nums[i]){
        i=l;
    }
    if(r<heapSize&&nums[r]>nums[i]){
        i=r;
    }
    if(i!=maxSize){
        swap(nums,i,maxSize);
        maxHeap(nums,heapSize,i);
    }
}
public static void swap(int[] nums,int a,int b){
    int temp=nums[a];
    nums[a]=nums[b];
    nums[b]=temp;
}
}

22.

PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
    @Override
    // 比较器实现,根据元素在map中的频率来比较两个元素  
    public int compare(Integer a, Integer b) {
        return map.get(a) - map.get(b);
    }
});

23.Arrays.fill(dp, 1);//初始化dp的每个元素为1

24.复习大根堆的经典使用场景,看我的主页的《Java中等题-有序矩阵中第k小的元素(力扣)》

25.

int res[]=Arrays.copyOfRange(temp,0,p);
//int res[] = Arrays.copyOfRange(temp, 0, p); 这行代码在Java中的意思是从一个已存在的整型数组temp中复制一部分元素到一个新的整型数组res中
int from:起始索引(包含),即从哪里开始复制。索引从0开始。
int to:结束索引(不包含),即复制到哪里停止。注意,这个索引处的元素不会被复制。

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值